parent
ad44d7efec
commit
6de1c147b8
@ -0,0 +1,43 @@
|
||||
package goredis
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type DBGttInterfaceFunc func() interface{}
|
||||
|
||||
// SimpleInterfaceCache 缓存
|
||||
type SimpleInterfaceCache struct {
|
||||
Operation *StringOperation // 操作类
|
||||
Expire time.Duration // 过期时间
|
||||
DBGetter DBGttInterfaceFunc // 缓存不存在的操作 DB
|
||||
}
|
||||
|
||||
// NewSimpleInterfaceCache 构造函数
|
||||
func (app *App) NewSimpleInterfaceCache(operation *StringOperation, 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 string) {
|
||||
f := func() string {
|
||||
obj := c.DBGetter()
|
||||
b, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
ret = c.Operation.Get(key).UnwrapOrElse(f)
|
||||
c.SetCache(key, ret)
|
||||
return
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package goredis
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type DBGttJsonFunc func() interface{}
|
||||
|
||||
// SimpleJsonCache 缓存
|
||||
type SimpleJsonCache struct {
|
||||
Operation *StringOperation // 操作类
|
||||
Expire time.Duration // 过期时间
|
||||
DBGetter DBGttJsonFunc // 缓存不存在的操作 DB
|
||||
}
|
||||
|
||||
// NewSimpleJsonCache 构造函数
|
||||
func (app *App) NewSimpleJsonCache(operation *StringOperation, expire time.Duration) *SimpleJsonCache {
|
||||
return &SimpleJsonCache{
|
||||
Operation: operation, // 操作类
|
||||
Expire: expire, // 过期时间
|
||||
}
|
||||
}
|
||||
|
||||
// SetCache 设置缓存
|
||||
func (c *SimpleJsonCache) SetCache(key string, value interface{}) {
|
||||
c.Operation.Set(key, value, WithExpire(c.Expire)).Unwrap()
|
||||
}
|
||||
|
||||
// GetCache 获取缓存
|
||||
func (c *SimpleJsonCache) GetCache(key string) (ret interface{}) {
|
||||
f := func() string {
|
||||
obj := c.DBGetter()
|
||||
b, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
ret = c.Operation.Get(key).UnwrapOrElse(f)
|
||||
c.SetCache(key, ret)
|
||||
return
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package goredis
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type DBGttStringFunc func() string
|
||||
|
||||
// SimpleStringCache 缓存
|
||||
type SimpleStringCache struct {
|
||||
Operation *StringOperation // 操作类
|
||||
Expire time.Duration // 过期时间
|
||||
DBGetter DBGttStringFunc // 缓存不存在的操作 DB
|
||||
}
|
||||
|
||||
// NewSimpleStringCache 构造函数
|
||||
func (app *App) 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
|
||||
}
|
Loading…
Reference in new issue