add caching
This commit is contained in:
@@ -9,65 +9,76 @@ import (
|
||||
|
||||
type LookupService struct {
|
||||
repository *repository.LookupRepository
|
||||
cache *model.LookupCache
|
||||
}
|
||||
|
||||
func NewLookupService(repository *repository.LookupRepository) *LookupService {
|
||||
return &LookupService{
|
||||
repository: repository,
|
||||
cache: model.NewLookupCache(),
|
||||
}
|
||||
}
|
||||
|
||||
// GetTracks
|
||||
// Gets Tracks rows from Lookup table.
|
||||
//
|
||||
// Args:
|
||||
// context.Context: Application context
|
||||
// Returns:
|
||||
// string: Application version
|
||||
func (as LookupService) GetTracks(ctx *fiber.Ctx) *[]model.Track {
|
||||
return as.repository.GetTracks(ctx.UserContext())
|
||||
func (s *LookupService) GetTracks(ctx *fiber.Ctx) (interface{}, error) {
|
||||
if cached, exists := s.cache.Get("tracks"); exists {
|
||||
return cached, nil
|
||||
}
|
||||
|
||||
tracks := s.repository.GetTracks(ctx.UserContext())
|
||||
s.cache.Set("tracks", tracks)
|
||||
return tracks, nil
|
||||
}
|
||||
|
||||
// GetCarModels
|
||||
// Gets CarModels rows from Lookup table.
|
||||
//
|
||||
// Args:
|
||||
// context.Context: Application context
|
||||
// Returns:
|
||||
// model.LookupModel: Lookup object from database.
|
||||
func (as LookupService) GetCarModels(ctx *fiber.Ctx) *[]model.CarModel {
|
||||
return as.repository.GetCarModels(ctx.UserContext())
|
||||
func (s *LookupService) GetCarModels(ctx *fiber.Ctx) (interface{}, error) {
|
||||
if cached, exists := s.cache.Get("cars"); exists {
|
||||
return cached, nil
|
||||
}
|
||||
|
||||
cars := s.repository.GetCarModels(ctx.UserContext())
|
||||
s.cache.Set("cars", cars)
|
||||
return cars, nil
|
||||
}
|
||||
|
||||
// GetDriverCategories
|
||||
// Gets DriverCategories rows from Lookup table.
|
||||
//
|
||||
// Args:
|
||||
// context.Context: Application context
|
||||
// Returns:
|
||||
// model.LookupModel: Lookup object from database.
|
||||
func (as LookupService) GetDriverCategories(ctx *fiber.Ctx) *[]model.DriverCategory {
|
||||
return as.repository.GetDriverCategories(ctx.UserContext())
|
||||
func (s *LookupService) GetDriverCategories(ctx *fiber.Ctx) (interface{}, error) {
|
||||
if cached, exists := s.cache.Get("drivers"); exists {
|
||||
return cached, nil
|
||||
}
|
||||
|
||||
categories := s.repository.GetDriverCategories(ctx.UserContext())
|
||||
s.cache.Set("drivers", categories)
|
||||
return categories, nil
|
||||
}
|
||||
|
||||
// GetCupCategories
|
||||
// Gets CupCategories rows from Lookup table.
|
||||
//
|
||||
// Args:
|
||||
// context.Context: Application context
|
||||
// Returns:
|
||||
// model.LookupModel: Lookup object from database.
|
||||
func (as LookupService) GetCupCategories(ctx *fiber.Ctx) *[]model.CupCategory {
|
||||
return as.repository.GetCupCategories(ctx.UserContext())
|
||||
func (s *LookupService) GetCupCategories(ctx *fiber.Ctx) (interface{}, error) {
|
||||
if cached, exists := s.cache.Get("cups"); exists {
|
||||
return cached, nil
|
||||
}
|
||||
|
||||
categories := s.repository.GetCupCategories(ctx.UserContext())
|
||||
s.cache.Set("cups", categories)
|
||||
return categories, nil
|
||||
}
|
||||
|
||||
// GetSessionTypes
|
||||
// Gets SessionTypes rows from Lookup table.
|
||||
//
|
||||
// Args:
|
||||
// context.Context: Application context
|
||||
// Returns:
|
||||
// model.LookupModel: Lookup object from database.
|
||||
func (as LookupService) GetSessionTypes(ctx *fiber.Ctx) *[]model.SessionType {
|
||||
return as.repository.GetSessionTypes(ctx.UserContext())
|
||||
func (s *LookupService) GetSessionTypes(ctx *fiber.Ctx) (interface{}, error) {
|
||||
if cached, exists := s.cache.Get("sessions"); exists {
|
||||
return cached, nil
|
||||
}
|
||||
|
||||
types := s.repository.GetSessionTypes(ctx.UserContext())
|
||||
s.cache.Set("sessions", types)
|
||||
return types, nil
|
||||
}
|
||||
|
||||
// ClearCache clears all cached lookup data
|
||||
func (s *LookupService) ClearCache() {
|
||||
s.cache.Clear()
|
||||
}
|
||||
|
||||
// PreloadCache loads all lookup data into cache
|
||||
func (s *LookupService) PreloadCache(ctx *fiber.Ctx) {
|
||||
s.GetTracks(ctx)
|
||||
s.GetCarModels(ctx)
|
||||
s.GetDriverCategories(ctx)
|
||||
s.GetCupCategories(ctx)
|
||||
s.GetSessionTypes(ctx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user