fix: align admin behavior and Kratos integrations

This commit is contained in:
2026-08-14 14:14:51 +08:00
parent 8c4c6ba661
commit e0b0e1d8c8
123 changed files with 4334 additions and 1877 deletions
+29 -3
View File
@@ -22,14 +22,14 @@ type cacheStore struct {
memory map[string]memoryCacheEntry
}
const maxMemoryCacheEntries = 10000
func NewCache(data *Data) biz.Cache {
return &cacheStore{data: data, memory: make(map[string]memoryCacheEntry)}
}
func (s *cacheStore) client() *redis.Client {
s.data.mu.RLock()
defer s.data.mu.RUnlock()
return s.data.redis
return s.data.redis.load()
}
func (s *cacheStore) Get(ctx context.Context, key string) (string, bool, error) {
@@ -62,6 +62,7 @@ func (s *cacheStore) Set(ctx context.Context, key, value string, expiration time
}
s.mu.Lock()
defer s.mu.Unlock()
s.makeMemoryRoom(key)
entry := memoryCacheEntry{value: value}
if expiration > 0 {
entry.expiresAt = time.Now().Add(expiration)
@@ -95,6 +96,7 @@ func (s *cacheStore) Increment(ctx context.Context, key string, expiration time.
}
s.mu.Lock()
defer s.mu.Unlock()
s.makeMemoryRoom(key)
entry, ok := s.memory[key]
if ok && !entry.expiresAt.IsZero() && time.Now().After(entry.expiresAt) {
ok = false
@@ -111,3 +113,27 @@ func (s *cacheStore) Increment(ctx context.Context, key string, expiration time.
s.memory[key] = entry
return value, nil
}
func (s *cacheStore) makeMemoryRoom(incoming string) {
if len(s.memory) < maxMemoryCacheEntries {
return
}
now := time.Now()
for key, entry := range s.memory {
if !entry.expiresAt.IsZero() && now.After(entry.expiresAt) {
delete(s.memory, key)
}
}
if len(s.memory) < maxMemoryCacheEntries {
return
}
if _, exists := s.memory[incoming]; exists {
return
}
// The in-memory store is only a Redis fallback. A bounded arbitrary eviction
// is preferable to unbounded growth under IP churn.
for key := range s.memory {
delete(s.memory, key)
break
}
}