Documentation
¶
Overview ¶
Package cache provide a Cache interface and some implement engine Usage:
import(
"github.com/astaxie/beego/cache"
)
bm, err := cache.NewCache("memory", `{"interval":60}`)
Use it like this:
bm.Put("astaxie", 1, 10 * time.Second) bm.Get("astaxie") bm.IsExist("astaxie") bm.Delete("astaxie") more docs http://beego.me/docs/module/cache.md
Index ¶
- Variables
- func GetBool(v interface{}) bool
- func GetFloat64(v interface{}) float64
- func GetInt(v interface{}) int
- func GetInt64(v interface{}) int64
- func GetString(v interface{}) string
- func Register(name string, adapter Instance)
- type Cache
- type Instance
- type MemoryCache
- func (bc *MemoryCache) ClearAll() error
- func (bc *MemoryCache) Decr(key string) error
- func (bc *MemoryCache) Delete(name string) error
- func (bc *MemoryCache) Get(name string) interface{}
- func (bc *MemoryCache) GetAllKeys() []string
- func (bc *MemoryCache) GetMulti(names []string) []interface{}
- func (bc *MemoryCache) Incr(key string) error
- func (bc *MemoryCache) IsExist(name string) bool
- func (bc *MemoryCache) Put(name string, value interface{}, lifespan time.Duration) error
- func (bc *MemoryCache) Size() int
- func (bc *MemoryCache) StartAndGC(config string) error
- func (bc *MemoryCache) Vacuum()
- type MemoryItem
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultEvery means the clock time of recycling the expired cache items in memory. DefaultEvery = 60 // 1 minute DefaultMaxSize = 10 )
Functions ¶
Types ¶
type Cache ¶
type Cache interface { // get cached value by key. Get(key string) interface{} // GetMulti is a batch version of Get. GetMulti(keys []string) []interface{} // set cached value with key and expire time. Put(key string, val interface{}, timeout time.Duration) 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 if cached value exists or not. IsExist(key string) bool // clear all cache. ClearAll() error // start gc routine based on config string settings. StartAndGC(config string) error // get all item's key. GetAllKeys() []string // get cache's size. Size() int // when you call delete, it may not really remove data from cache, so call Vacuum to remove it from cache. Vacuum() }
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,err := cache.NewCache("file","{....}") c.Put("key",value, 3600 * time.Second) v := c.Get("key") c.Incr("counter") // now is 1 c.Incr("counter") // now is 2 count := c.Get("counter").(int)
type MemoryCache ¶
type MemoryCache struct { sync.RWMutex Every int // run an expiration check Every clock time // contains filtered or unexported fields }
MemoryCache is Memory cache adapter. it contains a RW locker for safe map storage.
func (*MemoryCache) ClearAll ¶
func (bc *MemoryCache) ClearAll() error
ClearAll will delete all cache in memory.
func (*MemoryCache) Decr ¶
func (bc *MemoryCache) Decr(key string) error
Decr decrease counter in memory.
func (*MemoryCache) Delete ¶
func (bc *MemoryCache) Delete(name string) error
Delete cache in memory.
func (*MemoryCache) Get ¶
func (bc *MemoryCache) Get(name string) interface{}
Get cache from memory. if non-existed or expired, return nil.
func (*MemoryCache) GetAllKeys ¶
func (bc *MemoryCache) GetAllKeys() []string
GetAllKeys get all item's key.
func (*MemoryCache) GetMulti ¶
func (bc *MemoryCache) GetMulti(names []string) []interface{}
GetMulti gets caches from memory. if non-existed or expired, return nil.
func (*MemoryCache) Incr ¶
func (bc *MemoryCache) Incr(key string) error
Incr increase cache counter in memory. it supports int,int32,int64,uint,uint32,uint64.
func (*MemoryCache) IsExist ¶
func (bc *MemoryCache) IsExist(name string) bool
IsExist check cache exist in memory.
func (*MemoryCache) Put ¶
func (bc *MemoryCache) Put(name string, value interface{}, lifespan time.Duration) error
Put cache to memory. if lifespan is 0, it will be forever till restart.
func (*MemoryCache) Size ¶
func (bc *MemoryCache) Size() int
func (*MemoryCache) StartAndGC ¶
func (bc *MemoryCache) StartAndGC(config string) error
StartAndGC start memory cache. it will check expiration in every clock time.
func (*MemoryCache) Vacuum ¶
func (bc *MemoryCache) Vacuum()
type MemoryItem ¶
type MemoryItem struct {
// contains filtered or unexported fields
}
MemoryItem store memory cache item.