108 lines
2.2 KiB
Go
108 lines
2.2 KiB
Go
package data
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
"sync"
|
|
"time"
|
|
|
|
"kra/internal/biz"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type memoryCacheEntry struct {
|
|
value string
|
|
expiresAt time.Time
|
|
}
|
|
|
|
type cacheStore struct {
|
|
client *redis.Client
|
|
mu sync.Mutex
|
|
memory map[string]memoryCacheEntry
|
|
}
|
|
|
|
func NewCache(data *Data) biz.Cache {
|
|
return &cacheStore{client: data.redis, memory: make(map[string]memoryCacheEntry)}
|
|
}
|
|
|
|
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 err == nil {
|
|
return value, true, nil
|
|
}
|
|
if err != redis.Nil {
|
|
return "", false, err
|
|
}
|
|
return "", false, nil
|
|
}
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
entry, ok := s.memory[key]
|
|
if !ok {
|
|
return "", false, nil
|
|
}
|
|
if !entry.expiresAt.IsZero() && time.Now().After(entry.expiresAt) {
|
|
delete(s.memory, key)
|
|
return "", false, nil
|
|
}
|
|
return entry.value, true, nil
|
|
}
|
|
|
|
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()
|
|
}
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
entry := memoryCacheEntry{value: value}
|
|
if expiration > 0 {
|
|
entry.expiresAt = time.Now().Add(expiration)
|
|
}
|
|
s.memory[key] = entry
|
|
return nil
|
|
}
|
|
|
|
func (s *cacheStore) Delete(ctx context.Context, key string) error {
|
|
if s.client != nil {
|
|
return s.client.Del(ctx, key).Err()
|
|
}
|
|
s.mu.Lock()
|
|
delete(s.memory, key)
|
|
s.mu.Unlock()
|
|
return nil
|
|
}
|
|
|
|
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 err != nil {
|
|
return 0, err
|
|
}
|
|
if value == 1 && expiration > 0 {
|
|
if err := s.client.Expire(ctx, key, expiration).Err(); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
return value, nil
|
|
}
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
entry, ok := s.memory[key]
|
|
if ok && !entry.expiresAt.IsZero() && time.Now().After(entry.expiresAt) {
|
|
ok = false
|
|
}
|
|
value := int64(0)
|
|
if ok {
|
|
value, _ = strconv.ParseInt(entry.value, 10, 64)
|
|
}
|
|
value++
|
|
entry.value = strconv.FormatInt(value, 10)
|
|
if !ok && expiration > 0 {
|
|
entry.expiresAt = time.Now().Add(expiration)
|
|
}
|
|
s.memory[key] = entry
|
|
return value, nil
|
|
}
|