1 Star 0 Fork 0

xlango / beego

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
cache.go 2.12 KB
一键复制 编辑 原始数据 按行查看 历史
astaxie 提交于 2014-04-12 13:18 . update all files License
// Beego (http://beego.me/)
// @description beego is an open-source, high-performance web framework for the Go programming language.
// @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE
// @authors astaxie
package cache
import (
"fmt"
)
// Cache interface contains all behaviors for cache adapter.
// usage:
// cache.Register("file",cache.NewFileCache()) // this operation is run in init method of file.go.
// c := cache.NewCache("file","{....}")
// c.Put("key",value,3600)
// v := c.Get("key")
//
// c.Incr("counter") // now is 1
// c.Incr("counter") // now is 2
// count := c.Get("counter").(int)
type Cache interface {
// get cached value by key.
Get(key string) interface{}
// set cached value with key and expire time.
Put(key string, val interface{}, timeout int64) error
// delete cached value by key.
Delete(key string) error
// increase cached int value by key, as a counter.
Incr(key string) error
// decrease cached int value by key, as a counter.
Decr(key string) error
// check cached value is existed or not.
IsExist(key string) bool
// clear all cache.
ClearAll() error
// start gc routine via config string setting.
StartAndGC(config string) error
}
var adapters = make(map[string]Cache)
// Register makes a cache adapter available by the adapter name.
// If Register is called twice with the same name or if driver is nil,
// it panics.
func Register(name string, adapter Cache) {
if adapter == nil {
panic("cache: Register adapter is nil")
}
if _, dup := adapters[name]; dup {
panic("cache: Register called twice for adapter " + name)
}
adapters[name] = adapter
}
// Create a new cache driver by adapter and config string.
// config need to be correct JSON as string: {"interval":360}.
// it will start gc automatically.
func NewCache(adapterName, config string) (Cache, error) {
adapter, ok := adapters[adapterName]
if !ok {
return nil, fmt.Errorf("cache: unknown adaptername %q (forgotten import?)", adapterName)
}
err := adapter.StartAndGC(config)
if err != nil {
return nil, err
}
return adapter, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/xhylgogo/beego.git
git@gitee.com:xhylgogo/beego.git
xhylgogo
beego
beego
v1.2.0

搜索帮助

344bd9b3 5694891 D2dac590 5694891