Documentation ¶
Overview ¶
Package cache is a simple cache machnism for zebra, it support redis for now, and you can add your own cache engine with implement Factory and Cache.
Index ¶
- Constants
- func Decr(key string, args ...interface{}) error
- func Delete(key string, args ...string) error
- func Exist(key string, args ...interface{}) bool
- func Expire(key string, time int64) error
- func Get(key string, args ...string) interface{}
- func Incr(key string, args ...interface{}) error
- func Ioctrl(cmd string, args ...interface{}) (interface{}, error)
- func Register(uri string, factory Factory) error
- func Set(key string, args ...interface{}) error
- func TTL(key string) int64
- func UnRegister(engine string) error
- type Ant
- func (ant *Ant) Decr(key string, args ...interface{}) error
- func (ant *Ant) Delete(key string, args ...string) error
- func (ant *Ant) Destroy() error
- func (ant *Ant) Exist(key string, args ...interface{}) bool
- func (ant *Ant) Expire(key string, exp int64) error
- func (ant *Ant) Factory() Factory
- func (ant *Ant) Get(key string, args ...string) interface{}
- func (ant *Ant) Incr(key string, args ...interface{}) error
- func (ant *Ant) Ioctrl(cmd string, args ...interface{}) (interface{}, error)
- func (ant *Ant) Make(uri string) Cache
- func (ant *Ant) Set(key string, args ...interface{}) error
- func (ant *Ant) TTL(key string) int64
- type Cache
- type Factory
- type Redis
- func (r *Redis) Decr(key string, args ...interface{}) error
- func (r *Redis) Delete(key string, keys ...string) error
- func (r *Redis) Destroy() error
- func (r *Redis) Exist(key string, args ...interface{}) bool
- func (r *Redis) Expire(key string, time int64) error
- func (r *Redis) Factory() Factory
- func (r *Redis) Get(key string, args ...string) interface{}
- func (r *Redis) Incr(key string, args ...interface{}) error
- func (r *Redis) Ioctrl(cmd string, args ...interface{}) (result interface{}, err error)
- func (r *Redis) Make(uri string) Cache
- func (r *Redis) Set(key string, args ...interface{}) error
- func (r *Redis) TTL(key string) int64
Constants ¶
const ( //AntClockTick means the clock time of recycling the expired cache in memory. AntClockTick = 60 //60 seconds )
Variables ¶
This section is empty.
Functions ¶
func UnRegister ¶
UnRegister unregister a cache engine and cleanup context
Types ¶
type Ant ¶
Ant is a light weight memory cache, all data will be cached in memory, and will be recycled if application stopped
func (*Ant) Decr ¶
Decr decrease a key's value, if key not exist, ant will auto create a key with 0 value, and decrease it
Decr("key") --> decrease value by 1 Decry("key", 10) --> decrease value by 10
func (*Ant) Get ¶
Get get data from cache, get accept one or more than one key, if multi-key specified, a slice []interface{} will return, otherwise, interface{} will return
func (*Ant) Incr ¶
Incr increase a key's value, if key not exist, ant will auto create a key with 0 value, and increase it
Incr("key") --> increase value by 1 Incry("key", 10) --> increase value by 10
type Cache ¶
type Cache interface { //Set data to cache Set(key string, args ...interface{}) error //Get data from cache Get(key string, args ...string) interface{} //Delete a data from cache Delete(key string, args ...string) error //Query if key exists Exist(key string, args ...interface{}) bool //Increase a key's value Incr(key string, args ...interface{}) error //Decrease a key's value Decr(key string, args ...interface{}) error //Set a key's expiration time Expire(key string, time int64) error //Query a key's ttl TTL(key string) int64 //Custom io operations Ioctrl(cmd string, args ...interface{}) (interface{}, error) //Return a factory instance Factory() Factory }
Cache is a interface which is used to interact with cache, you MUST implement this to use zebra's cache mechanism
type Factory ¶
type Factory interface { //Make initialise a Cache instance Make(uri string) Cache //Destroy cleanup cache context Destroy() error }
Factory is a interface which is used init and cleanup cache context, you MUST implement this to use zebra's cache mechanism
type Redis ¶
type Redis struct {
// contains filtered or unexported fields
}
Redis engine for zebra Cache
Implement interface Factory and Cache, interface Factory used for init and destroy engine, and interface Cache used for manipulating the cache, all function but Ioctrl only support for strings and hash, if you want use other data structure or complex operations, please refer to Ioctrl, this function is just a wrap of redis.Do()
func (*Redis) Decr ¶
Decr decrease key's value
Decr("key") --> DECR key Decr("key", 10) --> DECRBY key 10 Decr("key", "field", 10) --> HDECRBY key field 10
func (*Redis) Delete ¶
Delete delete a key or field
Delete("key") --> DEL key Delete("key", "field") --> HDEL key field
func (*Redis) Exist ¶
Exist check if key or field existed
Exist("key") --> EXISTS key Exist("key", "field"[, "field"]) --> EXISTS key field [field]
func (*Redis) Get ¶
Get cached from redis server
Get will auto parse args to determin which data structure to use.
For strings Get("key") --> GET key (key is key of strings structure) For hash Get("key", "field"[,"field"]) --> HMGET key filed [field]
func (*Redis) Incr ¶
Incr increase key's value
Incr("key") --> INCR key Incr("key", 10) --> INCRBY key 10 Incr("key", "field", 10) --> HINCRBY key field 10
func (*Redis) Set ¶
Set data to redis with key and args.
Set will auto parse args to determin which data structure to use. you cannot use "ex", "px", "nx", "xx" as keys, they are reserved for engine using.
For strings Set("key", 345, "value") --> SETEX key 345 value Set("key", "value", "nx|xx") --> SET key value nx|xx Set("key", "value", "ex|px" 345) --> SET key value ex|px 345 For hash Set("key", "field", "value") --> HSET key field value Set("key", "field", "valude"[,"filed", "value"]) --> HMSET key field value
Note: You cannot set a value to different data structure