redis

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAdapterRedis

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

NewAdapterRedis creates a Redis adapter with default options.

func NewAdapterRedisWithOptions added in v0.3.0

func NewAdapterRedisWithOptions(redisClient *mredis.Redis, options ...Option) mcache.Adapter

NewAdapterRedisWithOptions creates a Redis adapter with explicit 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

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 selected Redis database. It incrementally scans keys, but may still be expensive for a large database.

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 selected Redis database using incremental SCAN calls.

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 selected Redis database. It incrementally scans keys, but may still be expensive for a large database.

type Option added in v0.3.0

type Option func(*AdapterRedis)

Option configures a Redis cache adapter.

func WithKeyPrefix added in v0.3.0

func WithKeyPrefix(prefix string) Option

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

func WithLockRetryInterval(interval time.Duration) Option

WithLockRetryInterval sets how often a waiter retries an occupied lock.

func WithLockTTL added in v0.3.0

func WithLockTTL(ttl time.Duration) Option

WithLockTTL sets how long a distributed cache lock remains valid.

func WithLockWaitTimeout added in v0.3.0

func WithLockWaitTimeout(timeout time.Duration) Option

WithLockWaitTimeout limits how long GetOrSetFuncLock waits for a value or lock.

Jump to

Keyboard shortcuts

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