Redis
基本使用
封装了redis操作,目前只支持string类型,hash,list以后会逐渐完善
案例
缓存使用
封装了redis缓存操作,通过DbGetter来决定缓存什么,使代码高度解耦
可以设置校验策略(内置正则校验策略)
自定义策略需要实现如下接口
type CachePolicy interface {
Before(key string ) //之前执行
IfNil(key string,v interface{}) //当空值是操作
SetOperation(opt *RedisStringOperation) //设置处理器 目前支持string
}
内置正则策略
//缓存穿透 策略
type CrossPolicy struct {
KeyRegx string //检查key的正则
Expire time.Duration //可以配置查找失败过期时间
opt *RedisStringOperation
}
func NewCrossPolicy(keyRegx string,expire time.Duration) *CrossPolicy {
return &CrossPolicy{KeyRegx: keyRegx,Expire:expire}
}
func (this *CrossPolicy) Before(key string ) {
if !regexp.MustCompile(this.KeyRegx).MatchString(key){
panic("error cache key")
}
}
func(this *CrossPolicy) IfNil(key string,v interface{}) {
this.opt.Set(key,v,WithExpire(this.Expire)).Unwrap()
}
func(this *CrossPolicy) SetOperation(opt *RedisStringOperation){
this.opt=opt
}
案例
最后更新于
这有帮助吗?