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.
|
|
|
|
package gocache
|
|
|
|
|
|
|
|
|
|
// RistrettoCache https://github.com/dgraph-io/ristretto
|
|
|
|
|
type RistrettoCache struct {
|
|
|
|
|
db *Ristretto // 驱动
|
|
|
|
|
GetterInterface GttInterfaceFunc // 不存在的操作
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewCache 实例化
|
|
|
|
|
func (r *Ristretto) NewCache() *RistrettoCache {
|
|
|
|
|
return &RistrettoCache{db: r}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetInterface 缓存操作
|
|
|
|
|
func (rc *RistrettoCache) GetInterface(key string) (ret interface{}) {
|
|
|
|
|
|
|
|
|
|
f := func() interface{} {
|
|
|
|
|
return rc.GetterInterface()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果不存在,则调用GetterInterface
|
|
|
|
|
ret, found := rc.db.Get(key)
|
|
|
|
|
|
|
|
|
|
if found == false {
|
|
|
|
|
rc.db.Set(key, f(), 1)
|
|
|
|
|
ret, _ = rc.db.Get(key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|