Documentation ¶
Index ¶
- func NewRedis(redisGroup *msredis.RedisGroup) gcache.Adapter
- type Redis
- func (c *Redis) Clear(ctx context.Context) error
- func (c *Redis) Close(ctx context.Context) error
- func (c *Redis) Contains(ctx context.Context, key interface{}) (bool, error)
- func (c *Redis) Data(ctx context.Context) (map[interface{}]interface{}, error)
- func (c *Redis) Get(ctx context.Context, key interface{}) (interface{}, error)
- func (c *Redis) GetExpire(ctx context.Context, key interface{}) (time.Duration, error)
- func (c *Redis) GetOrSet(ctx context.Context, key interface{}, value interface{}, ...) (interface{}, error)
- func (c *Redis) GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), ...) (interface{}, error)
- func (c *Redis) GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), ...) (interface{}, error)
- func (c *Redis) Keys(ctx context.Context) ([]interface{}, error)
- func (c *Redis) Remove(ctx context.Context, keys ...interface{}) (value interface{}, err error)
- func (c *Redis) Set(ctx context.Context, key interface{}, value interface{}, ...) error
- func (c *Redis) SetIfNotExist(ctx context.Context, key interface{}, value interface{}, ...) (bool, error)
- func (c *Redis) Sets(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error
- func (c *Redis) Size(ctx context.Context) (size int, err error)
- func (c *Redis) Update(ctx context.Context, key interface{}, value interface{}) (oldValue interface{}, exist bool, err error)
- func (c *Redis) UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error)
- func (c *Redis) Values(ctx context.Context) ([]interface{}, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Redis ¶
type Redis struct {
// contains filtered or unexported fields
}
Redis is the gcache adapter implements using Redis server.
func (*Redis) Clear ¶
Clear clears all data of the cache. Note that this function is sensitive and should be carefully used.
func (*Redis) Get ¶
Get retrieves and returns the associated value of given <key>. It returns nil if it does not exist or its value is nil.
func (*Redis) GetExpire ¶
GetExpire retrieves and returns the expiration of <key> in the cache.
It returns 0 if the <key> does not expire. It returns -1 if the <key> does not exist in the cache.
func (*Redis) GetOrSet ¶
func (c *Redis) GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (interface{}, error)
GetOrSet retrieves and returns the value of <key>, or sets <key>-<value> pair and returns <value> if <key> does not exist in the cache. The key-value pair expires after <duration>.
It does not expire if <duration> == 0. It deletes the <key> if <duration> < 0 or given <value> is nil, but it does nothing if <value> is a function and the function result is nil.
func (*Redis) GetOrSetFunc ¶
func (c *Redis) GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error)
GetOrSetFunc retrieves and returns the value of <key>, or sets <key> with result of function <f> and returns its result if <key> does not exist in the cache. The key-value pair expires after <duration>.
It does not expire if <duration> == 0. It deletes the <key> if <duration> < 0 or given <value> is nil, but it does nothing if <value> is a function and the function result is nil.
func (*Redis) GetOrSetFuncLock ¶
func (c *Redis) GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error)
GetOrSetFuncLock retrieves and returns the value of <key>, or sets <key> with result of function <f> and returns its result if <key> does not exist in the cache. The key-value pair expires after <duration>.
It does not expire if <duration> == 0. It does nothing if function <f> returns nil.
Note that the function <f> should be executed within writing mutex lock for concurrent safety purpose.
func (*Redis) Remove ¶
Remove deletes the one or more keys from cache, and returns its value. If multiple keys are given, it returns the value of the deleted last item.
func (*Redis) Set ¶
func (c *Redis) Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error
Set sets cache with <key>-<value> pair, which is expired after <duration>. It does not expire if <duration> == 0. It deletes the <key> if <duration> < 0 or given <value> is nil.
func (*Redis) SetIfNotExist ¶
func (c *Redis) SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (bool, error)
SetIfNotExist sets cache with <key>-<value> pair which is expired after <duration> if <key> does not exist in the cache. It returns true the <key> dose not exist in the cache and it sets <value> successfully to the cache, or else it returns false.
The parameter <value> can be type of <func() interface{}>, but it dose nothing if its result is nil.
It does not expire if <duration> == 0. It deletes the <key> if <duration> < 0 or given <value> is nil.
func (*Redis) Sets ¶
func (c *Redis) Sets(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error
Sets batch sets cache with key-value pairs by <data>, which is expired after <duration>.
It does not expire if <duration> == 0. It deletes the keys of <data> if <duration> < 0 or given <value> is nil.
func (*Redis) Update ¶
func (c *Redis) Update(ctx context.Context, key interface{}, value interface{}) (oldValue interface{}, exist bool, err error)
Update updates the value of <key> without changing its expiration and returns the old value. The returned value <exist> is false if the <key> does not exist in the cache.
It deletes the <key> if given <value> is nil. It does nothing if <key> does not exist in the cache.
func (*Redis) UpdateExpire ¶
func (c *Redis) UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error)
UpdateExpire updates the expiration of <key> and returns the old expiration duration value.
It returns -1 if the <key> does not exist in the cache. It deletes the <key> if <duration> < 0.