You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

52 lines
1015 B

package cache
import (
"context"
"github.com/redis/go-redis/v9"
"time"
)
type RedisCache struct {
keyPrefix string
rdb *redis.Client
}
func NewRedisCache(keyPrefix string, rdb *redis.Client) *RedisCache {
return &RedisCache{keyPrefix, rdb}
}
func (s *RedisCache) Set(ctx context.Context, key string, value any) error {
// redis.writer.go#WriteArg()
return s.rdb.Set(ctx, s.keyPrefix+key, value, 0).Err()
}
func (s *RedisCache) Load(ctx context.Context, key string, target any) error {
err := s.rdb.Get(ctx, s.keyPrefix+key).Scan(target)
if err == redis.Nil { // key 不存在
return NotExists
}
if err != nil {
return err
}
return nil
}
func (s *RedisCache) Del(ctx context.Context, keys ...string) error {
if keys == nil || len(keys) == 0 {
return nil
}
for i, k := range keys {
keys[i] = s.keyPrefix + k
}
return s.rdb.Del(ctx, keys...).Err()
}
// 重试次数
var retryTimes = 5
// 重试频率
var retryInterval = time.Millisecond * 50
func (s *RedisCache) Lock() {
}