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.
gocache/go_cache.go

44 lines
814 B

2 years ago
package gocache
2 years ago
import (
"time"
)
2 years ago
2 years ago
// GoCacheConfig 配置
type GoCacheConfig struct {
expiration time.Duration // 过期时间
}
2 years ago
// GoCache https://github.com/patrickmn/go-cache
type GoCache struct {
2 years ago
GoCacheConfig
2 years ago
db *Go // 驱动
GetterInterface GttInterfaceFunc // 不存在的操作
}
2 years ago
// NewCache 实例化
2 years ago
func (c *Go) NewCache(config *GoCacheConfig) *GoCache {
app := &GoCache{}
app.expiration = config.expiration
app.db = c
return app
2 years ago
}
2 years ago
// GetInterface 缓存操作
2 years ago
func (gc *GoCache) GetInterface(key string) (ret interface{}) {
f := func() interface{} {
return gc.GetterInterface()
}
2 years ago
// 如果不存在则调用GetterInterface
2 years ago
ret, found := gc.db.Get(key)
if found == false {
2 years ago
gc.db.Set(key, f(), gc.expiration)
2 years ago
ret, _ = gc.db.Get(key)
}
return
}