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_sring_cache.go

38 lines
907 B

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