cache

package
v0.0.0-...-5765f40 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 31, 2018 License: Apache-2.0 Imports: 5 Imported by: 0

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

View Source
const (
	//AntClockTick means the clock time of recycling the expired cache in memory.
	AntClockTick = 60 //60 seconds
)

Variables

This section is empty.

Functions

func Decr

func Decr(key string, args ...interface{}) error

Decrease a key's value

func Delete

func Delete(key string, args ...string) error

Delete a data from cache

func Exist

func Exist(key string, args ...interface{}) bool

Query if key exists

func Expire

func Expire(key string, time int64) error

Set a key's expiration time

func Get

func Get(key string, args ...string) interface{}

Get data from cache

func Incr

func Incr(key string, args ...interface{}) error

Increase a key's value

func Ioctrl

func Ioctrl(cmd string, args ...interface{}) (interface{}, error)

Custom io operations

func Register

func Register(uri string, factory Factory) error

Register register cache engine and make it ready to use

func Set

func Set(key string, args ...interface{}) error

Set data to cache

func TTL

func TTL(key string) int64

Query a key's ttl

func UnRegister

func UnRegister(engine string) error

UnRegister unregister a cache engine and cleanup context

Types

type Ant

type Ant struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Ant is a light weight memory cache, all data will be cached in memory, and will be recycled if application stopped

func (*Ant) Decr

func (ant *Ant) Decr(key string, args ...interface{}) error

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) Delete

func (ant *Ant) Delete(key string, args ...string) error

Delete delete a data from cache, multi-key available

func (*Ant) Destroy

func (ant *Ant) Destroy() error

func (*Ant) Exist

func (ant *Ant) Exist(key string, args ...interface{}) bool

Exist query if key exists, only single key supported, args will be ignored

func (*Ant) Expire

func (ant *Ant) Expire(key string, exp int64) error

Expire set a key's expiration time

func (*Ant) Factory

func (ant *Ant) Factory() Factory

Return a factory instance

func (*Ant) Get

func (ant *Ant) Get(key string, args ...string) interface{}

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

func (ant *Ant) Incr(key string, args ...interface{}) error

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

func (*Ant) Ioctrl

func (ant *Ant) Ioctrl(cmd string, args ...interface{}) (interface{}, error)

Custom io operations, not supported

func (*Ant) Make

func (ant *Ant) Make(uri string) Cache

func (*Ant) Set

func (ant *Ant) Set(key string, args ...interface{}) error

Set set data to cache

Set("key", value)                   --> cache key with value
Set("key", value, "nx|xx")          --> cache key with value if key not exist or exist
Set("key", value, "ex", expiration) --> cache key with value and expiration time

func (*Ant) TTL

func (ant *Ant) TTL(key string) int64

TTL query a key's ttl, if expiration time not set return 0, if time expired return -2, otherwise, return left time

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

func (r *Redis) Decr(key string, args ...interface{}) error

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

func (r *Redis) Delete(key string, keys ...string) error

Delete delete a key or field

Delete("key")          --> DEL key
Delete("key", "field") --> HDEL key field

func (*Redis) Destroy

func (r *Redis) Destroy() error

Destroy cleanup context if not used

func (*Redis) Exist

func (r *Redis) Exist(key string, args ...interface{}) bool

Exist check if key or field existed

Exist("key")                     --> EXISTS key
Exist("key", "field"[, "field"]) --> EXISTS key field [field]

func (*Redis) Expire

func (r *Redis) Expire(key string, time int64) error

Expire set expiration of a key

func (*Redis) Factory

func (r *Redis) Factory() Factory

Factory returns interface Factory

func (*Redis) Get

func (r *Redis) Get(key string, args ...string) interface{}

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

func (r *Redis) Incr(key string, args ...interface{}) error

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) Ioctrl

func (r *Redis) Ioctrl(cmd string, args ...interface{}) (result interface{}, err error)

Ioctrl handle all io operations of redis, it just a wrap of redis.Conn.Do()

func (*Redis) Make

func (r *Redis) Make(uri string) Cache

Make create a Redis instance, and return Cache

func (*Redis) Set

func (r *Redis) Set(key string, args ...interface{}) error

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

func (*Redis) TTL

func (r *Redis) TTL(key string) int64

TTL query a key's ttl

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL