feat: complete Kra admin backend migration
This commit is contained in:
+17
-11
@@ -17,18 +17,24 @@ type memoryCacheEntry struct {
|
||||
}
|
||||
|
||||
type cacheStore struct {
|
||||
client *redis.Client
|
||||
data *Data
|
||||
mu sync.Mutex
|
||||
memory map[string]memoryCacheEntry
|
||||
}
|
||||
|
||||
func NewCache(data *Data) biz.Cache {
|
||||
return &cacheStore{client: data.redis, memory: make(map[string]memoryCacheEntry)}
|
||||
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
|
||||
}
|
||||
|
||||
func (s *cacheStore) Get(ctx context.Context, key string) (string, bool, error) {
|
||||
if s.client != nil {
|
||||
value, err := s.client.Get(ctx, key).Result()
|
||||
if client := s.client(); client != nil {
|
||||
value, err := client.Get(ctx, key).Result()
|
||||
if err == nil {
|
||||
return value, true, nil
|
||||
}
|
||||
@@ -51,8 +57,8 @@ func (s *cacheStore) Get(ctx context.Context, key string) (string, bool, error)
|
||||
}
|
||||
|
||||
func (s *cacheStore) Set(ctx context.Context, key, value string, expiration time.Duration) error {
|
||||
if s.client != nil {
|
||||
return s.client.Set(ctx, key, value, expiration).Err()
|
||||
if client := s.client(); client != nil {
|
||||
return client.Set(ctx, key, value, expiration).Err()
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
@@ -65,8 +71,8 @@ func (s *cacheStore) Set(ctx context.Context, key, value string, expiration time
|
||||
}
|
||||
|
||||
func (s *cacheStore) Delete(ctx context.Context, key string) error {
|
||||
if s.client != nil {
|
||||
return s.client.Del(ctx, key).Err()
|
||||
if client := s.client(); client != nil {
|
||||
return client.Del(ctx, key).Err()
|
||||
}
|
||||
s.mu.Lock()
|
||||
delete(s.memory, key)
|
||||
@@ -75,13 +81,13 @@ func (s *cacheStore) Delete(ctx context.Context, key string) error {
|
||||
}
|
||||
|
||||
func (s *cacheStore) Increment(ctx context.Context, key string, expiration time.Duration) (int64, error) {
|
||||
if s.client != nil {
|
||||
value, err := s.client.Incr(ctx, key).Result()
|
||||
if client := s.client(); client != nil {
|
||||
value, err := client.Incr(ctx, key).Result()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if value == 1 && expiration > 0 {
|
||||
if err := s.client.Expire(ctx, key, expiration).Err(); err != nil {
|
||||
if err := client.Expire(ctx, key, expiration).Err(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user