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.
goredis/simple_interface_cache.go

40 lines
989 B

package goredis
import (
"log"
"time"
)
type DBGttInterfaceFunc func() interface{}
// SimpleInterfaceCache 缓存
type SimpleInterfaceCache struct {
Operation *SimpleOperation // 操作类
Expire time.Duration // 过期时间
DBGetter DBGttInterfaceFunc // 缓存不存在的操作 DB
}
// NewSimpleInterfaceCache 构造函数
func (client *Client) NewSimpleInterfaceCache(operation *SimpleOperation, expire time.Duration) *SimpleInterfaceCache {
return &SimpleInterfaceCache{
Operation: operation, // 操作类
Expire: expire, // 过期时间
}
}
// SetCache 设置缓存
func (c *SimpleInterfaceCache) SetCache(key string, value interface{}) {
c.Operation.Set(key, value, WithExpire(c.Expire)).Unwrap()
}
// GetCache 获取缓存
func (c *SimpleInterfaceCache) GetCache(key string) (ret interface{}) {
f := func() interface{} {
return c.DBGetter()
}
ret = c.Operation.Get(key).UnwrapOrElse(f)
c.SetCache(key, ret)
log.Println(ret)
return ret
}