1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-30 04:51:29 +00:00
Beego/cache/cache.go

75 lines
2.1 KiB
Go
Raw Normal View History

2014-04-12 05:18:18 +00:00
// Beego (http://beego.me/)
2014-07-03 15:40:21 +00:00
//
2014-04-12 05:18:18 +00:00
// @description beego is an open-source, high-performance web framework for the Go programming language.
2014-07-03 15:40:21 +00:00
//
2014-04-12 05:18:18 +00:00
// @link http://github.com/astaxie/beego for the canonical source repository
2014-07-03 15:40:21 +00:00
//
2014-04-12 05:18:18 +00:00
// @license http://github.com/astaxie/beego/blob/master/LICENSE
2014-07-03 15:40:21 +00:00
//
2014-04-12 05:18:18 +00:00
// @authors astaxie
2013-04-22 10:56:30 +00:00
package cache
import (
"fmt"
)
2013-12-22 05:35:02 +00:00
// 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)
2013-04-22 10:56:30 +00:00
type Cache interface {
2013-12-22 05:35:02 +00:00
// get cached value by key.
2013-04-22 10:56:30 +00:00
Get(key string) interface{}
2013-12-22 05:35:02 +00:00
// set cached value with key and expire time.
Put(key string, val interface{}, timeout int64) error
2013-12-22 05:35:02 +00:00
// delete cached value by key.
2013-04-22 10:56:30 +00:00
Delete(key string) error
2013-12-22 05:35:02 +00:00
// increase cached int value by key, as a counter.
2013-07-16 11:05:44 +00:00
Incr(key string) error
2013-12-22 05:35:02 +00:00
// decrease cached int value by key, as a counter.
2013-07-16 11:05:44 +00:00
Decr(key string) error
// check if cached value exists or not.
2013-04-22 10:56:30 +00:00
IsExist(key string) bool
2013-12-22 05:35:02 +00:00
// clear all cache.
2013-04-22 10:56:30 +00:00
ClearAll() error
// start gc routine based on config string settings.
2013-04-22 10:56:30 +00:00
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")
}
2014-07-10 05:04:18 +00:00
if _, ok := adapters[name]; ok {
2013-04-22 10:56:30 +00:00
panic("cache: Register called twice for adapter " + name)
}
adapters[name] = adapter
}
// Create a new cache driver by adapter name and config string.
2013-12-22 05:35:02 +00:00
// config need to be correct JSON as string: {"interval":360}.
// it will start gc automatically.
2014-07-10 05:04:18 +00:00
func NewCache(adapterName, config string) (adapter Cache, e error) {
2013-04-22 10:56:30 +00:00
adapter, ok := adapters[adapterName]
if !ok {
2014-07-10 05:04:18 +00:00
e = fmt.Errorf("cache: unknown adapter name %q (forgot to import?)", adapterName)
return
2013-04-22 10:56:30 +00:00
}
err := adapter.StartAndGC(config)
if err != nil {
2014-07-10 05:04:18 +00:00
adapter = nil
}
2014-07-10 05:04:18 +00:00
return
2013-04-22 10:56:30 +00:00
}