From b2ee16457dd3281a54d241264f2836ee049be392 Mon Sep 17 00:00:00 2001 From: AzenKain Date: Fri, 8 May 2026 21:15:54 +0700 Subject: [PATCH] feat: implement chat repository with caching support and add new commit service for history management --- internal/repositories/chatRepository.go | 22 +++++++++++++++++----- internal/services/commitService.go | 2 +- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/internal/repositories/chatRepository.go b/internal/repositories/chatRepository.go index 356f98d..e373cc4 100644 --- a/internal/repositories/chatRepository.go +++ b/internal/repositories/chatRepository.go @@ -246,7 +246,6 @@ func (r *chatRepository) CreateConversation(ctx context.Context, params sqlc.Cre CreatedAt: convert.TimeToPtr(row.CreatedAt), UpdatedAt: convert.TimeToPtr(row.UpdatedAt), } - _ = r.c.Set(ctx, fmt.Sprintf("conversation:id:%s", entity.ID), entity, constants.NormalCacheDuration) return entity, nil } @@ -264,7 +263,7 @@ func (r *chatRepository) UpdateConversationStatus(ctx context.Context, params sq CreatedAt: convert.TimeToPtr(row.CreatedAt), UpdatedAt: convert.TimeToPtr(row.UpdatedAt), } - _ = r.c.Set(ctx, fmt.Sprintf("conversation:id:%s", entity.ID), entity, constants.NormalCacheDuration) + _ = r.c.Del(ctx, fmt.Sprintf("conversation:id:%s", entity.ID)) return entity, nil } @@ -280,7 +279,7 @@ func (r *chatRepository) CreateMessage(ctx context.Context, params sqlc.CreateMe Content: row.Content, CreatedAt: convert.TimeToPtr(row.CreatedAt), } - _ = r.c.Set(ctx, fmt.Sprintf("message:id:%s", entity.ID), entity, constants.NormalCacheDuration) + return entity, nil } @@ -328,6 +327,7 @@ func (r *chatRepository) CreateChatbotHistory(ctx context.Context, params sqlc.C if err != nil { return nil, err } + entity := &models.ChatbotHistoryEntity{ ID: convert.UUIDToString(row.ID), UserID: convert.UUIDToString(row.UserID), @@ -335,12 +335,24 @@ func (r *chatRepository) CreateChatbotHistory(ctx context.Context, params sqlc.C Answer: row.Answer, CreatedAt: convert.TimeToPtr(row.CreatedAt), } - _ = r.c.Set(ctx, fmt.Sprintf("chatbot_history:id:%s", entity.ID), entity, constants.NormalCacheDuration) + + go func() { + userId := convert.UUIDToString(params.UserID) + if userId != "" { + _ = r.c.DelByPattern(context.Background(), fmt.Sprintf("chatbot_history:userId:%s:*", userId)) + } + }() + return entity, nil } func (r *chatRepository) GetChatbotHistory(ctx context.Context, params sqlc.GetChatbotHistoryParams) ([]*models.ChatbotHistoryEntity, error) { - queryKey := r.generateQueryKey("chatbot_history:user", params) + queryKey := fmt.Sprintf( + "chatbot_history:userId:%s:limit:%d:cursor:%s", + convert.UUIDToString(params.UserID), + params.Limit, + convert.UUIDToString(params.CursorID), + ) var cachedIDs []string if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 { return r.getChatbotHistoriesByIDsWithFallback(ctx, cachedIDs) diff --git a/internal/services/commitService.go b/internal/services/commitService.go index 1014c58..6d21959 100644 --- a/internal/services/commitService.go +++ b/internal/services/commitService.go @@ -135,7 +135,7 @@ func (s *commitService) CreateCommit(ctx context.Context, userID string, project return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to commit transaction") } - _ = s.c.Del(ctx, fmt.Sprintf("project:id:%s", projectID)) + _ = s.c.Del(ctx, fmt.Sprintf("project:id:%s", projectID), fmt.Sprintf("commit:project:%s", projectID)) return commit.ToResponse(), nil }