redis

package
v0.1.21 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAdapterRedis

func NewAdapterRedis(redis *mredis.Redis) mcache.Adapter

NewAdapterRedis creates and returns a new redis adapter for mcache.

Types

type AdapterRedis

type AdapterRedis struct {
	// contains filtered or unexported fields
}

AdapterRedis is the mcache adapter implements using Redis server.

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(ctx context.Context) error

Close closes the cache.

func (*AdapterRedis) Contains

func (c *AdapterRedis) Contains(ctx context.Context, key string) (bool, error)

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 cache as map type. Note that this method may be slow and memory-consuming if the cache size is large, as it uses `KEYS *` to retrieve all keys. It is not recommended to use this method in production.

func (*AdapterRedis) Get

func (c *AdapterRedis) Get(ctx context.Context, key string) (*mvar.Var, error)

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

func (c *AdapterRedis) GetExpire(ctx context.Context, key string) (time.Duration, error)

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 cache as slice. Note that this method may be slow if the cache size is large, as it uses `KEYS *` to retrieve all keys. It is not recommended to use this method in production.

func (*AdapterRedis) Remove

func (c *AdapterRedis) Remove(ctx context.Context, keys ...string) (lastValue *mvar.Var, err error)

Remove deletes one or more keys from cache.

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.

func (*AdapterRedis) UpdateExpire

func (c *AdapterRedis) UpdateExpire(ctx context.Context, key string, duration time.Duration) (oldDuration time.Duration, err error)

UpdateExpire updates the expiration of `key` and returns the old expiration duration value.

func (*AdapterRedis) Values

func (c *AdapterRedis) Values(ctx context.Context) (values []interface{}, err error)

Values returns all values in the cache as slice. Note that this method may be slow and memory-consuming if the cache size is large, as it uses `KEYS *` to retrieve all keys. It is not recommended to use this method in production.

Jump to

Keyboard shortcuts

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