diff --git a/cmd/api/server.go b/cmd/api/server.go index ce6089c..6d05efd 100644 --- a/cmd/api/server.go +++ b/cmd/api/server.go @@ -47,13 +47,15 @@ func NewHttpServer() *FiberServer { server.App.Use(swagger.New(cfg)) - logger := zerolog.New(zerolog.ConsoleWriter{ - Out: os.Stderr, - TimeFormat: time.RFC3339, - }).With().Timestamp().Logger() - server.App.Use(middleware.New(middleware.Config{ - Logger: &logger, - })) + if os.Getenv("DISABLE_REQUEST_LOG") != "true" { + logger := zerolog.New(zerolog.ConsoleWriter{ + Out: os.Stderr, + TimeFormat: time.RFC3339, + }).With().Timestamp().Logger() + server.App.Use(middleware.New(middleware.Config{ + Logger: &logger, + })) + } return server } diff --git a/docker-compose.yml b/docker-compose.yml index 8686586..1e98c84 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -24,7 +24,7 @@ services: image: redis:8.6.2-alpine container_name: history_cache restart: unless-stopped - command: ["redis-server", "--appendonly", "yes"] + command: ["redis-server", "--appendonly", "no"] volumes: - history_cache_data:/data networks: diff --git a/internal/repositories/battleReplayRepository.go b/internal/repositories/battleReplayRepository.go index 05eed96..94feb5d 100644 --- a/internal/repositories/battleReplayRepository.go +++ b/internal/repositories/battleReplayRepository.go @@ -143,7 +143,11 @@ func (r *battleReplayRepository) GetByID(ctx context.Context, id pgtype.UUID) (* func (r *battleReplayRepository) GetByGeometryID(ctx context.Context, geometryID pgtype.UUID) ([]*models.BattleReplayEntity, error) { cacheKey := fmt.Sprintf("battle_replay:geometry:%s", convert.UUIDToString(geometryID)) var cachedIDs []string - if err := r.c.Get(ctx, cacheKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, cacheKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.BattleReplayEntity{}, nil + } return r.getByIDsWithFallback(ctx, cachedIDs) } @@ -208,7 +212,11 @@ func (r *battleReplayRepository) GetByGeometryIDs(ctx context.Context, geometryI func (r *battleReplayRepository) GetByProjectID(ctx context.Context, projectID pgtype.UUID) ([]*models.BattleReplayEntity, error) { cacheKey := fmt.Sprintf("battle_replay:project:%s", convert.UUIDToString(projectID)) var cachedIDs []string - if err := r.c.Get(ctx, cacheKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, cacheKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.BattleReplayEntity{}, nil + } return r.getByIDsWithFallback(ctx, cachedIDs) } diff --git a/internal/repositories/chatRepository.go b/internal/repositories/chatRepository.go index e373cc4..9170838 100644 --- a/internal/repositories/chatRepository.go +++ b/internal/repositories/chatRepository.go @@ -286,7 +286,11 @@ func (r *chatRepository) CreateMessage(ctx context.Context, params sqlc.CreateMe func (r *chatRepository) GetMessagesByConversation(ctx context.Context, params sqlc.GetMessagesByConversationParams) ([]*models.MessageEntity, error) { queryKey := r.generateQueryKey("message:conversation", params) var cachedIDs []string - if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, queryKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.MessageEntity{}, nil + } return r.getMessagesByIDsWithFallback(ctx, cachedIDs) } @@ -315,9 +319,7 @@ func (r *chatRepository) GetMessagesByConversation(ctx context.Context, params s if len(toCache) > 0 { _ = r.c.MSet(ctx, toCache, constants.NormalCacheDuration) } - if len(ids) > 0 { - _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) - } + _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) return results, nil } @@ -354,7 +356,11 @@ func (r *chatRepository) GetChatbotHistory(ctx context.Context, params sqlc.GetC convert.UUIDToString(params.CursorID), ) var cachedIDs []string - if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, queryKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.ChatbotHistoryEntity{}, nil + } return r.getChatbotHistoriesByIDsWithFallback(ctx, cachedIDs) } @@ -383,9 +389,7 @@ func (r *chatRepository) GetChatbotHistory(ctx context.Context, params sqlc.GetC if len(toCache) > 0 { _ = r.c.MSet(ctx, toCache, constants.NormalCacheDuration) } - if len(ids) > 0 { - _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) - } + _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) return results, nil } diff --git a/internal/repositories/commitRepository.go b/internal/repositories/commitRepository.go index bc0f6d1..05040ae 100644 --- a/internal/repositories/commitRepository.go +++ b/internal/repositories/commitRepository.go @@ -165,7 +165,11 @@ func (r *commitRepository) getByIDsWithFallback(ctx context.Context, ids []strin func (r *commitRepository) GetByProjectID(ctx context.Context, projectID pgtype.UUID) ([]*models.CommitEntity, error) { queryKey := fmt.Sprintf("commit:project:%s", convert.UUIDToString(projectID)) var cachedIDs []string - if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, queryKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.CommitEntity{}, nil + } return r.getByIDsWithFallback(ctx, cachedIDs) } @@ -175,8 +179,10 @@ func (r *commitRepository) GetByProjectID(ctx context.Context, projectID pgtype. } entities := make([]*models.CommitEntity, 0, len(rows)) - for _, row := range rows { - entities = append(entities, &models.CommitEntity{ + ids := make([]string, len(rows)) + commitToCache := make(map[string]any) + for i, row := range rows { + item := &models.CommitEntity{ ID: convert.UUIDToString(row.ID), ProjectID: convert.UUIDToString(row.ProjectID), SnapshotJson: row.SnapshotJson, @@ -185,15 +191,26 @@ func (r *commitRepository) GetByProjectID(ctx context.Context, projectID pgtype. EditSummary: convert.TextToString(row.EditSummary), IsDeleted: row.IsDeleted, CreatedAt: convert.TimeToPtr(row.CreatedAt), - }) + } + entities = append(entities, item) + ids[i] = item.ID + commitToCache[fmt.Sprintf("commit:id:%s", item.ID)] = item } + if len(commitToCache) > 0 { + _ = r.c.MSet(ctx, commitToCache, constants.NormalCacheDuration) + } + _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) return entities, nil } func (r *commitRepository) Search(ctx context.Context, params sqlc.SearchCommitsParams) ([]*models.CommitEntity, error) { queryKey := r.generateQueryKey("commit:search", params) var cachedIDs []string - if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, queryKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.CommitEntity{}, nil + } return r.getByIDsWithFallback(ctx, cachedIDs) } rows, err := r.q.SearchCommits(ctx, params) @@ -223,9 +240,7 @@ func (r *commitRepository) Search(ctx context.Context, params sqlc.SearchCommits if len(commitToCache) > 0 { _ = r.c.MSet(ctx, commitToCache, constants.NormalCacheDuration) } - if len(ids) > 0 { - _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) - } + _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) return commits, nil } diff --git a/internal/repositories/entityRepository.go b/internal/repositories/entityRepository.go index 3f13ca9..7598b7a 100644 --- a/internal/repositories/entityRepository.go +++ b/internal/repositories/entityRepository.go @@ -163,7 +163,11 @@ func (r *entityRepository) GetByID(ctx context.Context, id pgtype.UUID) (*models func (r *entityRepository) Search(ctx context.Context, params sqlc.SearchEntitiesParams) ([]*models.EntityEntity, error) { queryKey := r.generateQueryKey("entity:search", params) var cachedIDs []string - if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, queryKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.EntityEntity{}, nil + } return r.getByIDsWithFallback(ctx, cachedIDs) } @@ -198,9 +202,7 @@ func (r *entityRepository) Search(ctx context.Context, params sqlc.SearchEntitie _ = r.c.MSet(ctx, entityToCache, constants.NormalCacheDuration) } - if len(ids) > 0 { - _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) - } + _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) return entities, nil } @@ -263,7 +265,11 @@ func (r *entityRepository) Delete(ctx context.Context, id pgtype.UUID) error { func (r *entityRepository) GetByProjectID(ctx context.Context, projectID pgtype.UUID) ([]*models.EntityEntity, error) { cacheKey := fmt.Sprintf("entity:project:%s", convert.UUIDToString(projectID)) var cachedIDs []string - if err := r.c.Get(ctx, cacheKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, cacheKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.EntityEntity{}, nil + } return r.getByIDsWithFallback(ctx, cachedIDs) } diff --git a/internal/repositories/geometryRepository.go b/internal/repositories/geometryRepository.go index 2b76985..0e0146c 100644 --- a/internal/repositories/geometryRepository.go +++ b/internal/repositories/geometryRepository.go @@ -182,7 +182,11 @@ func (r *geometryRepository) GetByID(ctx context.Context, id pgtype.UUID) (*mode func (r *geometryRepository) Search(ctx context.Context, params sqlc.SearchGeometriesParams) ([]*models.GeometryEntity, error) { queryKey := r.generateQueryKey("geometry:search", params) var cachedIDs []string - if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, queryKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.GeometryEntity{}, nil + } return r.getByIDsWithFallback(ctx, cachedIDs) } @@ -221,9 +225,7 @@ func (r *geometryRepository) Search(ctx context.Context, params sqlc.SearchGeome if len(geometryToCache) > 0 { _ = r.c.MSet(ctx, geometryToCache, constants.NormalCacheDuration) } - if len(ids) > 0 { - _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) - } + _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) return geometries, nil } @@ -311,7 +313,11 @@ func (r *geometryRepository) BulkDeleteEntityGeometriesByEntityId(ctx context.Co func (r *geometryRepository) GetByProjectID(ctx context.Context, projectID pgtype.UUID) ([]*models.GeometryEntity, error) { cacheKey := fmt.Sprintf("geometry:project:%s", convert.UUIDToString(projectID)) var cachedIDs []string - if err := r.c.Get(ctx, cacheKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, cacheKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.GeometryEntity{}, nil + } return r.getByIDsWithFallback(ctx, cachedIDs) } @@ -359,7 +365,11 @@ func (r *geometryRepository) GetByProjectID(ctx context.Context, projectID pgtyp func (r *geometryRepository) GetGeometriesByBoundWith(ctx context.Context, boundWith pgtype.UUID) ([]*models.GeometryEntity, error) { cacheKey := fmt.Sprintf("geometry:bound_with:%s", convert.UUIDToString(boundWith)) var cachedIDs []string - if err := r.c.Get(ctx, cacheKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, cacheKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.GeometryEntity{}, nil + } return r.getByIDsWithFallback(ctx, cachedIDs) } @@ -511,7 +521,11 @@ func (r *geometryRepository) SearchByEntityName(ctx context.Context, params sqlc queryKey := r.generateQueryKey("geometry:search:entity", params) var cachedPairs []string - if err := r.c.Get(ctx, queryKey, &cachedPairs); err == nil && len(cachedPairs) > 0 { + err := r.c.Get(ctx, queryKey, &cachedPairs) + if err == nil { + if len(cachedPairs) == 0 { + return []*models.EntityGeometriesSearchEntity{}, nil + } return r.getSearchByIDsWithFallback(ctx, cachedPairs) } @@ -544,9 +558,7 @@ func (r *geometryRepository) SearchByEntityName(ctx context.Context, params sqlc if len(geometryToCache) > 0 { _ = r.c.MSet(ctx, geometryToCache, constants.NormalCacheDuration) } - if len(pairs) > 0 { - _ = r.c.Set(ctx, queryKey, pairs, constants.ListCacheDuration) - } + _ = r.c.Set(ctx, queryKey, pairs, constants.ListCacheDuration) return geometries, nil } diff --git a/internal/repositories/mediaRepository.go b/internal/repositories/mediaRepository.go index feb7ee1..f7189ac 100644 --- a/internal/repositories/mediaRepository.go +++ b/internal/repositories/mediaRepository.go @@ -216,7 +216,11 @@ func (r *mediaRepository) BulkDelete(ctx context.Context, ids []pgtype.UUID) err func (r *mediaRepository) Search(ctx context.Context, params sqlc.SearchMediasParams) ([]*models.MediaEntity, error) { queryKey := r.generateQueryKey("media:search", params) var cachedIDs []string - if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, queryKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.MediaEntity{}, nil + } listItem, err := r.getByIDsWithFallback(ctx, cachedIDs) if err != nil { return nil, err @@ -259,9 +263,7 @@ func (r *mediaRepository) Search(ctx context.Context, params sqlc.SearchMediasPa _ = r.c.MSet(ctx, mediasToCache, constants.NormalCacheDuration) } - if len(ids) > 0 { - _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) - } + _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) return medias, nil } @@ -284,7 +286,11 @@ func (r *mediaRepository) Count(ctx context.Context, params sqlc.CountMediasPara func (r *mediaRepository) GetByUserID(ctx context.Context, userId pgtype.UUID) ([]*models.MediaEntity, error) { queryKey := fmt.Sprintf("media:userId:%s", convert.UUIDToString(userId)) var cachedIDs []string - if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, queryKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.MediaEntity{}, nil + } listItem, err := r.getByIDsWithFallback(ctx, cachedIDs) if err != nil { return nil, err @@ -327,9 +333,7 @@ func (r *mediaRepository) GetByUserID(ctx context.Context, userId pgtype.UUID) ( _ = r.c.MSet(ctx, mediasToCache, constants.NormalCacheDuration) } - if len(ids) > 0 { - _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) - } + _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) return medias, nil } diff --git a/internal/repositories/projectRepository.go b/internal/repositories/projectRepository.go index c967c50..4b94ebc 100644 --- a/internal/repositories/projectRepository.go +++ b/internal/repositories/projectRepository.go @@ -172,7 +172,11 @@ func (r *projectRepository) GetByID(ctx context.Context, id pgtype.UUID) (*model func (r *projectRepository) GetByUserID(ctx context.Context, params sqlc.GetProjectsByUserIdParams) ([]*models.ProjectEntity, error) { queryKey := r.generateQueryKey("project:user", params) var cachedIDs []string - if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, queryKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.ProjectEntity{}, nil + } return r.getByIDsWithFallback(ctx, cachedIDs) } @@ -211,9 +215,7 @@ func (r *projectRepository) GetByUserID(ctx context.Context, params sqlc.GetProj if len(projectToCache) > 0 { _ = r.c.MSet(ctx, projectToCache, constants.NormalCacheDuration) } - if len(ids) > 0 { - _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) - } + _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) return projects, nil } @@ -221,7 +223,11 @@ func (r *projectRepository) GetByUserID(ctx context.Context, params sqlc.GetProj func (r *projectRepository) Search(ctx context.Context, params sqlc.SearchProjectsParams) ([]*models.ProjectEntity, error) { queryKey := r.generateQueryKey("project:search", params) var cachedIDs []string - if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, queryKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.ProjectEntity{}, nil + } return r.getByIDsWithFallback(ctx, cachedIDs) } @@ -259,9 +265,7 @@ func (r *projectRepository) Search(ctx context.Context, params sqlc.SearchProjec if len(projectToCache) > 0 { _ = r.c.MSet(ctx, projectToCache, constants.NormalCacheDuration) } - if len(ids) > 0 { - _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) - } + _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) return projects, nil } diff --git a/internal/repositories/roleRepository.go b/internal/repositories/roleRepository.go index 3e83c83..71050b2 100644 --- a/internal/repositories/roleRepository.go +++ b/internal/repositories/roleRepository.go @@ -215,7 +215,11 @@ func (r *roleRepository) Update(ctx context.Context, params sqlc.UpdateRoleParam func (r *roleRepository) All(ctx context.Context) ([]*models.RoleEntity, error) { queryKey := "role:all" var cachedIDs []string - if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, queryKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.RoleEntity{}, nil + } listItem, err := r.getByIDsWithFallback(ctx, cachedIDs) if err != nil { return nil, err @@ -254,9 +258,7 @@ func (r *roleRepository) All(ctx context.Context) ([]*models.RoleEntity, error) _ = r.c.MSet(ctx, roleToCache, constants.NormalCacheDuration) } - if len(ids) > 0 { - _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) - } + _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) return roles, nil } diff --git a/internal/repositories/statisticRepo.go b/internal/repositories/statisticRepo.go index 85e9394..d37ed7d 100644 --- a/internal/repositories/statisticRepo.go +++ b/internal/repositories/statisticRepo.go @@ -138,7 +138,11 @@ func (r *statisticRepository) Search(ctx context.Context, params sqlc.SearchSyst queryKey := r.generateQueryKey("statistic:search", params) var cachedIDs []string - if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, queryKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.StatisticEntity{}, nil + } return r.getByIDsWithFallback(ctx, cachedIDs) } @@ -161,9 +165,7 @@ func (r *statisticRepository) Search(ctx context.Context, params sqlc.SearchSyst if len(statsToCache) > 0 { _ = r.c.MSet(ctx, statsToCache, constants.NormalCacheDuration) } - if len(ids) > 0 { - _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) - } + _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) return stats, nil } diff --git a/internal/repositories/submissionRepository.go b/internal/repositories/submissionRepository.go index 0ce230d..6cfc16a 100644 --- a/internal/repositories/submissionRepository.go +++ b/internal/repositories/submissionRepository.go @@ -166,7 +166,11 @@ func (r *submissionRepository) GetByIDs(ctx context.Context, ids []string) ([]*m func (r *submissionRepository) Search(ctx context.Context, params sqlc.SearchSubmissionsParams) ([]*models.SubmissionEntity, error) { queryKey := r.generateQueryKey("verification:search", params) var cachedIDs []string - if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, queryKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.SubmissionEntity{}, nil + } listItem, err := r.getByIDsWithFallback(ctx, cachedIDs) if err != nil { return nil, err @@ -216,9 +220,7 @@ func (r *submissionRepository) Search(ctx context.Context, params sqlc.SearchSub _ = r.c.MSet(ctx, itemToCache, constants.NormalCacheDuration) } - if len(ids) > 0 { - _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) - } + _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) return items, nil } diff --git a/internal/repositories/userRepository.go b/internal/repositories/userRepository.go index 8d4b850..e4228ba 100644 --- a/internal/repositories/userRepository.go +++ b/internal/repositories/userRepository.go @@ -313,7 +313,11 @@ func (r *userRepository) Search(ctx context.Context, params sqlc.SearchUsersPara queryKey := r.generateQueryKey("user:search", params) var cachedIDs []string - if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, queryKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.UserEntity{}, nil + } return r.getByIDsWithFallback(ctx, cachedIDs) } @@ -354,9 +358,7 @@ func (r *userRepository) Search(ctx context.Context, params sqlc.SearchUsersPara if len(usersToCache) > 0 { _ = r.c.MSet(ctx, usersToCache, constants.NormalCacheDuration) } - if len(ids) > 0 { - _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) - } + _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) return users, nil } diff --git a/internal/repositories/verificationRepository.go b/internal/repositories/verificationRepository.go index 2640cd8..0a7c7a4 100644 --- a/internal/repositories/verificationRepository.go +++ b/internal/repositories/verificationRepository.go @@ -278,7 +278,11 @@ func (v *verificationRepository) DeleteVerificationMedia(ctx context.Context, pa func (v *verificationRepository) GetByUserID(ctx context.Context, userId pgtype.UUID) ([]*models.UserVerificationEntity, error) { queryKey := fmt.Sprintf("verification:userId:%s", convert.UUIDToString(userId)) var cachedIDs []string - if err := v.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := v.c.Get(ctx, queryKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.UserVerificationEntity{}, nil + } listItem, err := v.getByIDsWithFallback(ctx, cachedIDs) if err != nil { return nil, err @@ -332,9 +336,7 @@ func (v *verificationRepository) GetByUserID(ctx context.Context, userId pgtype. _ = v.c.MSet(ctx, itemToCache, constants.NormalCacheDuration) } - if len(ids) > 0 { - _ = v.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) - } + _ = v.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) return items, nil } @@ -342,7 +344,11 @@ func (v *verificationRepository) GetByUserID(ctx context.Context, userId pgtype. func (v *verificationRepository) Search(ctx context.Context, params sqlc.SearchUserVerificationsParams) ([]*models.UserVerificationEntity, error) { queryKey := v.generateQueryKey("verification:search", params) var cachedIDs []string - if err := v.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := v.c.Get(ctx, queryKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.UserVerificationEntity{}, nil + } listItem, err := v.getByIDsWithFallback(ctx, cachedIDs) if err != nil { return nil, err @@ -397,9 +403,7 @@ func (v *verificationRepository) Search(ctx context.Context, params sqlc.SearchU _ = v.c.MSet(ctx, itemToCache, constants.NormalCacheDuration) } - if len(ids) > 0 { - _ = v.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) - } + _ = v.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) return items, nil } diff --git a/internal/repositories/wikiRepository.go b/internal/repositories/wikiRepository.go index 71c48de..9b850c0 100644 --- a/internal/repositories/wikiRepository.go +++ b/internal/repositories/wikiRepository.go @@ -190,7 +190,11 @@ func (r *wikiRepository) GetByID(ctx context.Context, id pgtype.UUID) (*models.W func (r *wikiRepository) Search(ctx context.Context, params sqlc.SearchWikisParams) ([]*models.WikiEntity, error) { queryKey := r.generateQueryKey("wiki:search", params) var cachedIDs []string - if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, queryKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.WikiEntity{}, nil + } return r.getByIDsWithFallback(ctx, cachedIDs) } @@ -242,9 +246,7 @@ func (r *wikiRepository) Search(ctx context.Context, params sqlc.SearchWikisPara if len(wikiToCache) > 0 { _ = r.c.MSet(ctx, wikiToCache, constants.NormalCacheDuration) } - if len(ids) > 0 { - _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) - } + _ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration) return wikis, nil } @@ -315,7 +317,11 @@ func (r *wikiRepository) BulkDeleteEntityWikisByEntityId(ctx context.Context, en func (r *wikiRepository) GetByProjectID(ctx context.Context, projectID pgtype.UUID) ([]*models.WikiEntity, error) { cacheKey := fmt.Sprintf("wiki:project:%s", convert.UUIDToString(projectID)) var cachedIDs []string - if err := r.c.Get(ctx, cacheKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { + err := r.c.Get(ctx, cacheKey, &cachedIDs) + if err == nil { + if len(cachedIDs) == 0 { + return []*models.WikiEntity{}, nil + } return r.getByIDsWithFallback(ctx, cachedIDs) } @@ -368,9 +374,7 @@ func (r *wikiRepository) GetByProjectID(ctx context.Context, projectID pgtype.UU if len(wikiToCache) > 0 { _ = r.c.MSet(ctx, wikiToCache, constants.NormalCacheDuration) } - if len(ids) > 0 { - _ = r.c.Set(ctx, cacheKey, ids, constants.ListCacheDuration) - } + _ = r.c.Set(ctx, cacheKey, ids, constants.ListCacheDuration) return wikis, nil } diff --git a/pkg/cache/redis.go b/pkg/cache/redis.go index 957ad00..4b7ceb2 100644 --- a/pkg/cache/redis.go +++ b/pkg/cache/redis.go @@ -35,7 +35,8 @@ func NewRedisClient() (Cache, error) { rdb := redis.NewClient(&redis.Options{ Addr: uri, - MinIdleConns: 10, + PoolSize: 500, + MinIdleConns: 50, DialTimeout: 5 * time.Second, ReadTimeout: 3 * time.Second, WriteTimeout: 3 * time.Second, diff --git a/pkg/database/db.go b/pkg/database/db.go index 6c6f7e9..731fb99 100644 --- a/pkg/database/db.go +++ b/pkg/database/db.go @@ -20,6 +20,8 @@ func NewPostgresqlDB() (*pgxpool.Pool, error) { if err != nil { return nil, err } + poolConfig.MaxConns = 100 + poolConfig.MinConns = 10 var pool *pgxpool.Pool