代码拉取完成,页面将自动刷新
package goredis
import (
"encoding/json"
"time"
)
const (
Serilizer_JSON = "json"
)
type DBGettFunc func() interface{}
type SimpleCache struct {
Operation *StringOperation
Expire time.Duration
DBGeter DBGettFunc
Serilizer string
}
func NewSimpleCache(operation *StringOperation, expire time.Duration, serilizer string) *SimpleCache {
return &SimpleCache{Operation: operation, Expire: expire, Serilizer: serilizer}
}
// 设置缓存
func (this *SimpleCache) SetCache(key string, value interface{}, expire time.Duration) {
this.Operation.Set(key, value, WithExpire(expire)).UnWrap()
}
// 获取缓存
func (this *SimpleCache) GetCache(key string) (ret interface{}) {
if this.Serilizer == Serilizer_JSON {
//转成字符串
f := func() string {
if this.DBGeter == nil {
return ""
} else {
obj := this.DBGeter()
b, err := json.Marshal(obj)
if err != nil {
return ""
}
return string(b)
}
}
ret = this.Operation.Get(key).UnwrapElse(f)
this.SetCache(key, ret, this.Expire)
}
return
}
// /锁
func (this *SimpleCache) Lock(key string, exp time.Duration) bool {
return this.Operation.SetNx(key, 1, WithExpire(exp)).UnWrap().(bool)
}
// 解锁
func (this *SimpleCache) Unlock(key string) {
this.Operation.Del(key)
}
// 删除缓存
func (this *SimpleCache) Del(key string) {
this.Operation.Del(key)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。