Documentation
¶
Index ¶
- func NewAdapterRedis(redisClient *mredis.Redis) mcache.Adapter
- func NewAdapterRedisWithOptions(redisClient *mredis.Redis, options ...Option) mcache.Adapter
- type AdapterRedis
- func (c *AdapterRedis) Clear(ctx context.Context) error
- func (c *AdapterRedis) Close(_ context.Context) error
- func (c *AdapterRedis) Contains(ctx context.Context, key string) (bool, error)
- func (c *AdapterRedis) Data(ctx context.Context) (data map[string]interface{}, err error)
- func (c *AdapterRedis) Get(ctx context.Context, key string) (*mvar.Var, error)
- func (c *AdapterRedis) GetExpire(ctx context.Context, key string) (time.Duration, error)
- func (c *AdapterRedis) GetOrSet(ctx context.Context, key string, value interface{}, duration time.Duration) (result *mvar.Var, err error)
- func (c *AdapterRedis) GetOrSetFunc(ctx context.Context, key string, f mcache.Func, duration time.Duration) (result *mvar.Var, err error)
- func (c *AdapterRedis) GetOrSetFuncLock(ctx context.Context, key string, f mcache.Func, duration time.Duration) (result *mvar.Var, err error)
- func (c *AdapterRedis) Keys(ctx context.Context) (keys []string, err error)
- func (c *AdapterRedis) Remove(ctx context.Context, keys ...string) (lastValue *mvar.Var, err error)
- func (c *AdapterRedis) Set(ctx context.Context, key string, value interface{}, duration time.Duration) error
- func (c *AdapterRedis) SetIfNotExist(ctx context.Context, key string, value interface{}, duration time.Duration) (ok bool, err error)
- func (c *AdapterRedis) SetIfNotExistFunc(ctx context.Context, key string, f mcache.Func, duration time.Duration) (ok bool, err error)
- func (c *AdapterRedis) SetIfNotExistFuncLock(ctx context.Context, key string, f mcache.Func, duration time.Duration) (ok bool, err error)
- func (c *AdapterRedis) SetMap(ctx context.Context, data map[string]interface{}, duration time.Duration) error
- func (c *AdapterRedis) Size(ctx context.Context) (size int, err error)
- func (c *AdapterRedis) Update(ctx context.Context, key string, value interface{}) (oldValue *mvar.Var, exist bool, err error)
- func (c *AdapterRedis) UpdateExpire(ctx context.Context, key string, duration time.Duration) (oldDuration time.Duration, err error)
- func (c *AdapterRedis) Values(ctx context.Context) (values []interface{}, err error)
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewAdapterRedis ¶
NewAdapterRedis creates a Redis adapter with default options.
Types ¶
type AdapterRedis ¶
type AdapterRedis struct {
// contains filtered or unexported fields
}
AdapterRedis implements mcache.Adapter with Redis.
func (*AdapterRedis) Clear ¶
func (c *AdapterRedis) Clear(ctx context.Context) error
Clear clears all data of the cache.
func (*AdapterRedis) Close ¶
func (c *AdapterRedis) Close(_ context.Context) error
Close closes the cache.
func (*AdapterRedis) Contains ¶
Contains checks and returns true if `key` exists in the cache, or else returns false.
func (*AdapterRedis) Data ¶
func (c *AdapterRedis) Data(ctx context.Context) (data map[string]interface{}, err error)
Data returns a copy of all key-value pairs in the selected Redis database. It incrementally scans keys, but may still be expensive for a large database.
func (*AdapterRedis) Get ¶
Get retrieves and returns the associated value of given `key`. It returns nil if it does not exist, or its value is nil, or it's expired.
func (*AdapterRedis) GetExpire ¶
GetExpire retrieves and returns the expiration of `key` in the cache.
func (*AdapterRedis) GetOrSet ¶
func (c *AdapterRedis) GetOrSet(ctx context.Context, key string, value interface{}, duration time.Duration) (result *mvar.Var, err error)
GetOrSet retrieves and returns the value of `key`, or sets `key`-`value` pair and returns `value` if `key` does not exist.
func (*AdapterRedis) GetOrSetFunc ¶
func (c *AdapterRedis) GetOrSetFunc(ctx context.Context, key string, f mcache.Func, duration time.Duration) (result *mvar.Var, err 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.
func (*AdapterRedis) GetOrSetFuncLock ¶
func (c *AdapterRedis) GetOrSetFuncLock(ctx context.Context, key string, f mcache.Func, duration time.Duration) (result *mvar.Var, err 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.
func (*AdapterRedis) Keys ¶
func (c *AdapterRedis) Keys(ctx context.Context) (keys []string, err error)
Keys returns all keys in the selected Redis database using incremental SCAN calls.
func (*AdapterRedis) Set ¶
func (c *AdapterRedis) Set(ctx context.Context, key string, 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 (*AdapterRedis) SetIfNotExist ¶
func (c *AdapterRedis) SetIfNotExist(ctx context.Context, key string, value interface{}, duration time.Duration) (ok bool, err error)
SetIfNotExist sets cache with `key`-`value` pair if `key` does not exist in the cache. It is an atomic operation.
func (*AdapterRedis) SetIfNotExistFunc ¶
func (c *AdapterRedis) SetIfNotExistFunc(ctx context.Context, key string, f mcache.Func, duration time.Duration) (ok bool, err error)
SetIfNotExistFunc sets `key` with result of function `f` if `key` does not exist in the cache.
func (*AdapterRedis) SetIfNotExistFuncLock ¶
func (c *AdapterRedis) SetIfNotExistFuncLock(ctx context.Context, key string, f mcache.Func, duration time.Duration) (ok bool, err error)
SetIfNotExistFuncLock sets `key` with result of function `f` if `key` does not exist in the cache. Note that the function `f` is executed within redis lock.
func (*AdapterRedis) SetMap ¶
func (c *AdapterRedis) SetMap(ctx context.Context, data map[string]interface{}, duration time.Duration) error
SetMap batch sets cache with key-value pairs by `data` map, which is expired after `duration`.
func (*AdapterRedis) Size ¶
func (c *AdapterRedis) Size(ctx context.Context) (size int, err error)
Size returns the number of items in the cache. Note that this method is not accurate in redis cluster mode and may be slow if the cache size is large.
func (*AdapterRedis) Update ¶
func (c *AdapterRedis) Update(ctx context.Context, key string, value interface{}) (oldValue *mvar.Var, exist bool, err error)
Update updates the value of `key` without changing its expiration and returns the old value.
type Option ¶ added in v0.3.0
type Option func(*AdapterRedis)
Option configures a Redis cache adapter.
func WithKeyPrefix ¶ added in v0.3.0
WithKeyPrefix limits cache operations to keys with the given prefix. Use a delimiter such as "myapp:cache:" to keep logical keys unambiguous.
func WithLockRetryInterval ¶ added in v0.3.0
WithLockRetryInterval sets how often a waiter retries an occupied lock.
func WithLockTTL ¶ added in v0.3.0
WithLockTTL sets how long a distributed cache lock remains valid.
func WithLockWaitTimeout ¶ added in v0.3.0
WithLockWaitTimeout limits how long GetOrSetFuncLock waits for a value or lock.