Files
backend/internal/cache/repository_adapter.go
lan 6c14309624
All checks were successful
Build Backend / build (push) Successful in 2m2s
Build Backend / build-docker (push) Successful in 1m23s
feat(cache): implement database fallback for conversation sequence management
Improve the reliability of conversation sequence generation by implementing a fallback mechanism to the database when Redis is unavailable or when keys are missing.

- Add `GetNextSeq` to `MessageRepository` and implement it in `MessageRepositoryAdapter`.
- Update `ConversationCache` to fallback to DB in `GetConvMaxSeq`, `GetConvMaxSeqBatch`, and `GetNextSeq`.
- Ensure Redis TTL is refreshed after every `INCR` operation to prevent sequence counter expiration.
- Refactor service layers to handle sequence generation errors more explicitly.
- Add `IsEnabled` helper to `LayeredCache` for cleaner availability checks.
2026-05-15 13:08:22 +08:00

82 lines
3.1 KiB
Go

package cache
import (
"with_you/internal/model"
"with_you/internal/repository"
)
// ConversationRepositoryAdapter 适配 MessageRepository 到 ConversationRepository 接口
type ConversationRepositoryAdapter struct {
repo repository.MessageRepository
}
// NewConversationRepositoryAdapter 创建适配器
func NewConversationRepositoryAdapter(repo repository.MessageRepository) ConversationRepository {
return &ConversationRepositoryAdapter{repo: repo}
}
// GetConversationByID 实现 ConversationRepository 接口
func (a *ConversationRepositoryAdapter) GetConversationByID(convID string) (*model.Conversation, error) {
return a.repo.GetConversation(convID)
}
// GetConversationsByUserID 实现 ConversationRepository 接口
func (a *ConversationRepositoryAdapter) GetConversationsByUserID(userID string, page, pageSize int) ([]*model.Conversation, int64, error) {
return a.repo.GetConversations(userID, page, pageSize)
}
// GetParticipant 实现 ConversationRepository 接口
func (a *ConversationRepositoryAdapter) GetParticipant(convID, userID string) (*model.ConversationParticipant, error) {
return a.repo.GetParticipant(convID, userID)
}
// GetParticipants 实现 ConversationRepository 接口
func (a *ConversationRepositoryAdapter) GetParticipants(convID string) ([]*model.ConversationParticipant, error) {
return a.repo.GetConversationParticipants(convID)
}
// GetUnreadCount 实现 ConversationRepository 接口
func (a *ConversationRepositoryAdapter) GetUnreadCount(userID, convID string) (int64, error) {
return a.repo.GetUnreadCount(convID, userID)
}
// MessageRepositoryAdapter 适配 MessageRepository 到 MessageRepository 接口
type MessageRepositoryAdapter struct {
repo repository.MessageRepository
}
// NewMessageRepositoryAdapter 创建适配器
func NewMessageRepositoryAdapter(repo repository.MessageRepository) MessageRepository {
return &MessageRepositoryAdapter{repo: repo}
}
// GetMessages 实现 MessageRepository 接口
func (a *MessageRepositoryAdapter) GetMessages(convID string, page, pageSize int) ([]*model.Message, int64, error) {
return a.repo.GetMessages(convID, page, pageSize)
}
// GetMessagesAfterSeq 实现 MessageRepository 接口
func (a *MessageRepositoryAdapter) GetMessagesAfterSeq(convID string, afterSeq int64, limit int) ([]*model.Message, error) {
return a.repo.GetMessagesAfterSeq(convID, afterSeq, limit)
}
// GetMessagesBeforeSeq 实现 MessageRepository 接口
func (a *MessageRepositoryAdapter) GetMessagesBeforeSeq(convID string, beforeSeq int64, limit int) ([]*model.Message, error) {
return a.repo.GetMessagesBeforeSeq(convID, beforeSeq, limit)
}
// CreateMessage 实现 MessageRepository 接口
func (a *MessageRepositoryAdapter) CreateMessage(msg *model.Message) error {
return a.repo.CreateMessage(msg)
}
// UpdateConversationLastSeq 实现 MessageRepository 接口
func (a *MessageRepositoryAdapter) UpdateConversationLastSeq(convID string, seq int64) error {
return a.repo.UpdateConversationLastSeq(convID, seq)
}
// GetNextSeq 实现 MessageRepository 接口
func (a *MessageRepositoryAdapter) GetNextSeq(convID string) (int64, error) {
return a.repo.GetNextSeq(convID)
}