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.
This commit is contained in:
7
internal/cache/layered_cache.go
vendored
7
internal/cache/layered_cache.go
vendored
@@ -352,8 +352,13 @@ func (c *LayeredCache) ZCard(ctx context.Context, key string) (int64, error) {
|
||||
return c.redis.ZCard(ctx, key)
|
||||
}
|
||||
|
||||
// IsEnabled 返回 Redis 是否可用
|
||||
func (c *LayeredCache) IsEnabled() bool {
|
||||
return c.enabled && c.redis != nil
|
||||
}
|
||||
|
||||
func (c *LayeredCache) Incr(ctx context.Context, key string) (int64, error) {
|
||||
if !c.enabled || c.redis == nil {
|
||||
if !c.IsEnabled() {
|
||||
return 0, ErrKeyNotFound
|
||||
}
|
||||
c.local.Delete(key)
|
||||
|
||||
Reference in New Issue
Block a user