package projects import ( "context" "fmt" "github.com/eko/gocache/cache" "go.uber.org/zap" ) type Service struct { projectsRepository Repository logger *zap.Logger cache *cache.MetricCache } func NewService(logger *zap.Logger, projectsRepository Repository, cache *cache.MetricCache) *Service { return &Service{ logger: logger, projectsRepository: projectsRepository, cache: cache} } func (s *Service) CreateProject(ctx context.Context, userId int, name string) (int, error) { s.logger.Debug("creating project", zap.String("name", name), zap.Int("creatorId", userId)) projectId, err := s.projectsRepository.Create(ctx, NewCreateProject(name, userId)) if err != nil { s.logger.Warn(err.Error()) return -1, err } _ = s.cache.Delete(fmt.Sprintf("projects_userId_%d", userId)) return projectId, nil } func (s *Service) Get(ctx context.Context, userId int) ([]*Project, error) { s.logger.Debug("getting projects", zap.Int("userId", userId)) loadFunc := func(key interface{}) (interface{}, error) { s.logger.Debug("getting projects from repository", zap.Int("userId", userId)) return s.projectsRepository.GetForMemberId(ctx, userId) } cacheEntry := cache.NewLoadable( loadFunc, s.cache) entry, err := cacheEntry.Get(fmt.Sprintf("projects_userId_%d", userId)) if err != nil { return nil, err } projects := entry.([]*Project) return projects, nil }