Documentation
¶
Overview ¶
Package cache provides tools for caching data.
Index ¶
- Variables
- type CommonProvider
- func (p *CommonProvider) Close() error
- func (p *CommonProvider) Decr(ctx context.Context, key string) (int64, error)
- func (p *CommonProvider) DecrBy(ctx context.Context, key string, decrement int64) (int64, error)
- func (p *CommonProvider) Del(ctx context.Context, keys ...string) error
- func (p *CommonProvider) DelByPattern(ctx context.Context, pattern string, batchSize *int64) error
- func (p *CommonProvider) ExpireAt(ctx context.Context, key string, at time.Time) error
- func (p *CommonProvider) Get(ctx context.Context, key string) (string, error)
- func (p *CommonProvider) GetDel(ctx context.Context, key string) (string, error)
- func (p *CommonProvider) GetEx(ctx context.Context, key string, expiration time.Duration) (string, error)
- func (p *CommonProvider) Incr(ctx context.Context, key string) (int64, error)
- func (p *CommonProvider) IncrBy(ctx context.Context, key string, value int64) (int64, error)
- func (p *CommonProvider) MGet(ctx context.Context, keys ...string) ([]*string, error)
- func (p *CommonProvider) Ping(ctx context.Context) (string, error)
- func (p *CommonProvider) Set(ctx context.Context, key string, value any, expiration time.Duration) error
- func (p *CommonProvider) SetNX(ctx context.Context, key string, value any, expiration time.Duration) error
- func (p *CommonProvider) TTL(ctx context.Context, key string) (time.Duration, error)
- func (p *CommonProvider) TrySet(ctx context.Context, key string, value any, expiration time.Duration) (bool, error)
- func (p *CommonProvider) ZAdd(ctx context.Context, key string, members ...Z) error
- func (p *CommonProvider) ZRangeByScore(ctx context.Context, key string, by ZRangeBy) ([]string, error)
- func (p *CommonProvider) ZRem(ctx context.Context, key string, members ...string) (int64, error)
- func (p *CommonProvider) ZScore(ctx context.Context, key, member string) (float64, error)
- type Option
- func WithClientName(clientName string) Option
- func WithConnectionMaxIdleTime(connectionMaxIdleTime time.Duration) Option
- func WithConnectionMaxLifetime(connectionMaxLifetime time.Duration) Option
- func WithContextTimeoutEnabled(contextTimeoutEnabled bool) Option
- func WithDB(db int) Option
- func WithDialTimeout(dialTimeout time.Duration) Option
- func WithHost(host string) Option
- func WithMaxActiveConnections(maxActiveConnections int) Option
- func WithMaxIdleConnections(maxIdleConnections int) Option
- func WithMaxRetries(maxRetries int) Option
- func WithMaxRetryBackoff(maxRetryBackoff time.Duration) Option
- func WithMinIdleConnections(minIdleConnections int) Option
- func WithMinRetryBackoff(minRetryBackoff time.Duration) Option
- func WithPassword(password string) Option
- func WithPoolFIFO(poolFIFO bool) Option
- func WithPoolSize(poolSize int) Option
- func WithPoolTimeout(poolTimeout time.Duration) Option
- func WithPort(port int) Option
- func WithReadTimeout(readTimeout time.Duration) Option
- func WithUsername(username string) Option
- func WithWriteTimeout(writeTimeout time.Duration) Option
- type Provider
- type SortedSetProvider
- type Z
- type ZRangeBy
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = redis.Nil
ErrNotFound is returned by read operations, when key or sorted set member does not exist.
It is an alias of the driver's sentinel on purpose: consumers stop importing redis driver just to tell "no such key" from a real failure, while code, which already checks redis.Nil, keeps working.
Functions ¶
This section is empty.
Types ¶
type CommonProvider ¶
type CommonProvider struct {
// contains filtered or unexported fields
}
func New ¶
func New(opts ...Option) (*CommonProvider, error)
func (*CommonProvider) Close ¶
func (p *CommonProvider) Close() error
Close closes connection to cache.
func (*CommonProvider) Del ¶
func (p *CommonProvider) Del(ctx context.Context, keys ...string) error
Del deletes keys.
func (*CommonProvider) DelByPattern ¶ added in v1.11.1
DelByPattern deletes all keys, which matches provided pattern.
func (*CommonProvider) ExpireAt ¶ added in v1.16.1
ExpireAt sets the moment, at which key expires.
Needed by keys, created by commands, which do not take expiration themselves: Incr creates a counter without any, and such counter stays in cache forever. Moment in the past deletes key. Missing key is not an error.
func (*CommonProvider) GetEx ¶
func (p *CommonProvider) GetEx( ctx context.Context, key string, expiration time.Duration, ) (string, error)
GetEx gets key and expires it, if ttl is expired.
func (*CommonProvider) MGet ¶ added in v1.16.1
MGet gets values of all provided keys at once.
Result follows the order of keys, and nil at a position means, that this key is missing. One round trip instead of a Get per key: values, read separately, belong to different moments in time, and decisions made on them may be based on a state, which never existed.
func (*CommonProvider) Ping ¶
func (p *CommonProvider) Ping(ctx context.Context) (string, error)
Ping checks status.
func (*CommonProvider) Set ¶
func (p *CommonProvider) Set( ctx context.Context, key string, value any, expiration time.Duration, ) error
Set sets key.
func (*CommonProvider) SetNX ¶
func (p *CommonProvider) SetNX( ctx context.Context, key string, value any, expiration time.Duration, ) error
SetNX sets key, if not already exists.
func (*CommonProvider) TTL ¶ added in v1.16.1
TTL returns remaining time to live of key.
Negative results repeat the ones of Redis itself: -1 means key without expiration, -2 means missing key.
func (*CommonProvider) TrySet ¶ added in v1.16.1
func (p *CommonProvider) TrySet( ctx context.Context, key string, value any, expiration time.Duration, ) (bool, error)
TrySet sets key, if it does not already exist, and reports whether it was set.
Unlike SetNX, which hides the outcome behind error alone, it can be used as a lock: out of many concurrent attempts to take the same key exactly one gets true, and the rest get false.
func (*CommonProvider) ZAdd ¶ added in v1.16.1
ZAdd adds members to sorted set of key, creating it, if needed.
Member is addressed by its value, so repeated add updates score of an already existing member instead of adding a second one.
func (*CommonProvider) ZRangeByScore ¶ added in v1.16.1
func (p *CommonProvider) ZRangeByScore( ctx context.Context, key string, by ZRangeBy, ) ([]string, error)
ZRangeByScore returns members of sorted set, whose scores fall into provided range, ordered by score.
Count is what makes it usable for queues of due work: a range without limit returns everything, that piled up, while a batch is handled and repeated.
type Option ¶
type Option func(options *options) error
Option represents golang functional option pattern func for configuration.
func WithClientName ¶
func WithDialTimeout ¶
func WithMaxIdleConnections ¶
func WithMaxRetries ¶
func WithMaxRetryBackoff ¶
func WithMinIdleConnections ¶
func WithMinRetryBackoff ¶
func WithPassword ¶
func WithPoolFIFO ¶
func WithPoolSize ¶
func WithPoolTimeout ¶
func WithReadTimeout ¶
func WithUsername ¶
func WithWriteTimeout ¶
type Provider ¶
type Provider interface {
SortedSetProvider
// Set sets key.
Set(ctx context.Context, key string, value any, expiration time.Duration) error
// SetNX sets key, if not already exists.
SetNX(ctx context.Context, key string, value any, expiration time.Duration) error
// TrySet sets key, if not already exists, and reports whether it was set.
// Unlike SetNX it can be used as a lock: out of many concurrent attempts
// exactly one gets true.
TrySet(ctx context.Context, key string, value any, expiration time.Duration) (bool, error)
// Get gets key.
Get(ctx context.Context, key string) (string, error)
// GetEx gets key and expires it, if ttl is expired.
GetEx(ctx context.Context, key string, expiration time.Duration) (string, error)
// GetDel gets key and deletes it.
GetDel(ctx context.Context, key string) (string, error)
// MGet gets values of all provided keys at once. Result follows the order of
// keys, and nil at a position means, that this key is missing.
MGet(ctx context.Context, keys ...string) ([]*string, error)
// Incr increments key.
Incr(ctx context.Context, key string) (int64, error)
// IncrBy increments key by value (numeric such as +1, +2 and so on).
IncrBy(ctx context.Context, key string, value int64) (int64, error)
// Decr decrements key.
Decr(ctx context.Context, key string) (int64, error)
// DecrBy decrements key by value (numeric such as -1, -2 and so on).
DecrBy(ctx context.Context, key string, decrement int64) (int64, error)
// Del deletes key.
Del(ctx context.Context, keys ...string) error
// DelByPattern deletes all keys, which matches provided pattern.
DelByPattern(ctx context.Context, pattern string, batchSize *int64) error
// ExpireAt sets the moment, at which key expires. Needed by keys, created by
// commands, which do not take expiration themselves, such as Incr.
ExpireAt(ctx context.Context, key string, at time.Time) error
// TTL returns remaining time to live of key: -1 for key without expiration,
// -2 for missing key.
TTL(ctx context.Context, key string) (time.Duration, error)
// Ping checks status.
Ping(ctx context.Context) (string, error)
Close() error
}
Provider provides methods for setting cache and getting cached data.
type SortedSetProvider ¶ added in v1.16.1
type SortedSetProvider interface {
// ZAdd adds members to sorted set of key. Repeated add updates score of an
// already existing member instead of adding a second one.
ZAdd(ctx context.Context, key string, members ...Z) error
// ZRem removes members from sorted set of key and returns, how many of them
// were actually removed.
ZRem(ctx context.Context, key string, members ...string) (int64, error)
// ZScore returns score of sorted set member. ErrNotFound, if there is no such
// member or no such key.
ZScore(ctx context.Context, key, member string) (float64, error)
// ZRangeByScore returns members of sorted set, whose scores fall into provided
// range, ordered by score.
ZRangeByScore(ctx context.Context, key string, by ZRangeBy) ([]string, error)
}
SortedSetProvider provides methods for sorted sets — the only structure here, whose members are kept ordered by a numeric score.
Separate from Provider, because a consumer, which keeps a queue of due work, needs these four methods and none of the string ones.
type Z ¶ added in v1.16.1
type Z struct {
// Score is a weight, which orders member inside sorted set.
Score float64
// Member is a value, stored in sorted set. Unique inside its key: repeated
// add of the same member updates its score instead of adding a second one.
Member string
}
Z is a sorted set member together with its score.
type ZRangeBy ¶ added in v1.16.1
type ZRangeBy struct {
// Min is an inclusive lower bound: a number, "-inf" or "(5" for exclusive one.
Min string
// Max is an inclusive upper bound: a number, "+inf" or "(5" for exclusive one.
Max string
// Offset is a number of matched members to skip. Applied together with Count.
Offset int64
// Count limits, how many members are returned. Zero means "all of them".
Count int64
}
ZRangeBy is a score range for sorted set lookups.