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.
dorm/redis_cache.go

90 lines
2.3 KiB

package dorm
import (
"context"
"encoding/json"
"time"
)
// RedisCacheConfig 配置
type RedisCacheConfig struct {
DefaultExpiration time.Duration // 过期时间
}
// RedisClientCache https://github.com/go-redis/redis
type RedisClientCache struct {
config *RedisCacheConfig
operation *RedisClient // 操作
GetterString func() string // 不存在的操作
GetterInterface func() interface{} // 不存在的操作
}
// NewCache 实例化
func (c *RedisClient) NewCache(config *RedisCacheConfig) *RedisClientCache {
cc := &RedisClientCache{config: config}
cc.operation = c
return cc
}
// NewCacheDefaultExpiration 实例化
func (c *RedisClient) NewCacheDefaultExpiration() *RedisClientCache {
cc := &RedisClientCache{}
cc.config.DefaultExpiration = time.Minute * 30 // 默认过期时间
cc.operation = c
return cc
}
// GetString 缓存操作
func (rc *RedisClientCache) GetString(ctx context.Context, key string) (ret string) {
f := func() string {
return rc.GetterString()
}
// 如果不存在则调用GetterString
ret, err := rc.operation.Get(ctx, key).Result()
if err != nil {
rc.operation.Set(ctx, key, f(), rc.config.DefaultExpiration)
ret, _ = rc.operation.Get(ctx, key).Result()
}
return
}
// GetInterface 缓存操作
func (rc *RedisClientCache) GetInterface(ctx context.Context, key string, result interface{}) {
f := func() string {
marshal, _ := json.Marshal(rc.GetterInterface())
return string(marshal)
}
// 如果不存在则调用GetterInterface
ret, err := rc.operation.Get(ctx, key).Result()
if err != nil {
rc.operation.Set(ctx, key, f(), rc.config.DefaultExpiration)
ret, _ = rc.operation.Get(ctx, key).Result()
}
err = json.Unmarshal([]byte(ret), result)
return
}
// GetInterfaceKey 获取key值
func (rc *RedisClientCache) GetInterfaceKey(ctx context.Context, key string, result interface{}) error {
ret, err := rc.operation.Get(ctx, key).Result()
if err != nil {
return err
}
err = json.Unmarshal([]byte(ret), result)
return nil
}
// SetInterfaceKey 设置key值
func (rc *RedisClientCache) SetInterfaceKey(ctx context.Context, key string, value interface{}) (string, error) {
marshal, _ := json.Marshal(value)
return rc.operation.Set(ctx, key, marshal, rc.config.DefaultExpiration).Result()
}