Documentation ¶
Index ¶
- Variables
- func ClearAll() error
- func Decr(key string) error
- func Delete(key string) error
- func FileGetContents(filename string) (data []byte, e error)
- func FilePutContents(filename string, content []byte) error
- func Get(key string) interface{}
- func GetBool(v interface{}) bool
- func GetFloat64(v interface{}) float64
- func GetInt(v interface{}) int
- func GetInt64(v interface{}) int64
- func GetMulti(keys []string) []interface{}
- func GetString(v interface{}) string
- func GobDecode(data []byte, to *FileCacheItem) error
- func GobEncode(data interface{}) ([]byte, error)
- func Incr(key string) error
- func InitCache(adapterName, config string) (err error)
- func IsExist(key string) bool
- func Put(key string, val interface{}, ttl time.Duration) error
- func Register(name string, adapter Instance)
- func StartAndGC(cfg string) error
- type Cache
- type FileCache
- func (fc *FileCache) ClearAll() error
- func (fc *FileCache) Decr(key string) error
- func (fc *FileCache) Delete(key string) error
- func (fc *FileCache) Get(key string) interface{}
- func (fc *FileCache) GetMulti(keys []string) []interface{}
- func (fc *FileCache) Incr(key string) error
- func (fc *FileCache) Init()
- func (fc *FileCache) IsExist(key string) bool
- func (fc *FileCache) Put(key string, val interface{}, timeout time.Duration) error
- func (fc *FileCache) StartAndGC(config string) error
- type FileCacheItem
- 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) 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) StartAndGC(config string) error
- type MemoryItem
Constants ¶
This section is empty.
Variables ¶
var ( FileCachePath = "cache" // cache directory FileCacheFileSuffix = ".bin" // cache file suffix FileCacheDirectoryLevel = 2 // cache file deep level if auto generated cache files. FileCacheEmbedExpiry time.Duration // cache expire time, default is no expire forever. )
FileCache Config
var ( // DefaultEvery means the clock time of recycling the expired cache items in memory. DefaultEvery = 60 // 1 minute )
Functions ¶
func FileGetContents ¶
FileGetContents Get bytes to file. if non-exist, create this file.
func FilePutContents ¶
FilePutContents Put bytes to file. if non-exist, create this file.
func GobDecode ¶
func GobDecode(data []byte, to *FileCacheItem) error
GobDecode Gob decodes file cache item.
func Register ¶
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 StartAndGC ¶
Types ¶
type Cache ¶
type Cache interface { Get(key string) interface{} // get cached value by key. GetMulti(keys []string) []interface{} // GetMulti is a batch version of Get. Put(key string, val interface{}, timeout time.Duration) error // set cached value with key and expire time. Delete(key string) error // delete cached value by key. Incr(key string) error // increase cached int value by key, as a counter. Decr(key string) error // decrease cached int value by key, as a counter. IsExist(key string) bool // check if cached value exists or not. ClearAll() error // clear all cache. StartAndGC(config string) error // start gc routine based on config string settings. }
func NewCache ¶
NewCache Create a new cache driver by adapter name and config string. config need to be correct JSON as string: {"interval":360}. it will start gc automatically.
func NewFileCache ¶
func NewFileCache() Cache
NewFileCache Create new file cache with no config. the level and expiry need set in method StartAndGC as config string.
type FileCache ¶
FileCache is cache adapter for file storage.
func (*FileCache) GetMulti ¶
GetMulti gets values from file cache. if non-exist or expired, return empty string.
func (*FileCache) Incr ¶
Incr will increase cached int value. fc value is saving forever unless Delete.
func (*FileCache) Init ¶
func (fc *FileCache) Init()
Init will make new dir for file cache if not exist.
func (*FileCache) Put ¶
Put value into file cache. timeout means how long to keep this file, unit of ms. if timeout equals FileCacheEmbedExpiry(default is 0), cache this item forever.
func (*FileCache) StartAndGC ¶
StartAndGC will start and begin gc for file cache. the config need to be like {CachePath:"/cache","FileSuffix":".bin","DirectoryLevel":2,"EmbedExpiry":0}
type FileCacheItem ¶
FileCacheItem is basic unit of file cache adapter. it contains data and expire time.
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) 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) StartAndGC ¶
func (bc *MemoryCache) StartAndGC(config string) error
StartAndGC start memory cache. it will check expiration in every clock time.
type MemoryItem ¶
type MemoryItem struct {
// contains filtered or unexported fields
}
MemoryItem store memory cache item.