优化结构

This commit is contained in:
Yvan
2026-08-22 22:34:33 +08:00
parent 0126995090
commit b18baf1547
26 changed files with 501 additions and 80 deletions
+13 -3
View File
@@ -31,7 +31,12 @@ func New(provider RedisProvider) system.Cache {
return &Store{provider: provider, memory: make(map[string]memoryEntry)}
}
func (s *Store) client() redis.UniversalClient { return s.provider.RedisClient() }
func (s *Store) client() redis.UniversalClient {
if s == nil || s.provider == nil {
return nil
}
return s.provider.RedisClient()
}
func (s *Store) Get(ctx context.Context, key string) (string, bool, error) {
if client := s.client(); client != nil {
@@ -101,6 +106,7 @@ func (s *Store) Increment(ctx context.Context, key string, expiration time.Durat
entry, ok := s.memory[key]
if ok && !entry.expiresAt.IsZero() && time.Now().After(entry.expiresAt) {
ok = false
entry = memoryEntry{}
}
value := int64(0)
if ok {
@@ -108,8 +114,12 @@ func (s *Store) Increment(ctx context.Context, key string, expiration time.Durat
}
value++
entry.value = strconv.FormatInt(value, 10)
if !ok && expiration > 0 {
entry.expiresAt = time.Now().Add(expiration)
if !ok {
if expiration > 0 {
entry.expiresAt = time.Now().Add(expiration)
} else {
entry.expiresAt = time.Time{}
}
}
s.memory[key] = entry
return value, nil