cache

package
v1.17.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package cache provides tools for caching data.

Index

Constants

This section is empty.

Variables

View Source
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) Decr

func (p *CommonProvider) Decr(ctx context.Context, key string) (int64, error)

Decr decrements key.

func (*CommonProvider) DecrBy

func (p *CommonProvider) DecrBy(ctx context.Context, key string, decrement int64) (int64, error)

DecrBy decrements key by value (numeric such as -1, -2 and so on).

func (*CommonProvider) Del

func (p *CommonProvider) Del(ctx context.Context, keys ...string) error

Del deletes keys.

func (*CommonProvider) DelByPattern added in v1.11.1

func (p *CommonProvider) DelByPattern(ctx context.Context, pattern string, batchSize *int64) error

DelByPattern deletes all keys, which matches provided pattern.

func (*CommonProvider) ExpireAt added in v1.16.1

func (p *CommonProvider) ExpireAt(ctx context.Context, key string, at time.Time) error

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) Get

func (p *CommonProvider) Get(ctx context.Context, key string) (string, error)

Get gets key.

func (*CommonProvider) GetDel

func (p *CommonProvider) GetDel(ctx context.Context, key string) (string, error)

GetDel gets key and deletes it.

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) Incr

func (p *CommonProvider) Incr(ctx context.Context, key string) (int64, error)

Incr increments key.

func (*CommonProvider) IncrBy

func (p *CommonProvider) IncrBy(ctx context.Context, key string, value int64) (int64, error)

IncrBy increments key by value (numeric such as +1, +2 and so on).

func (*CommonProvider) MGet added in v1.16.1

func (p *CommonProvider) MGet(ctx context.Context, keys ...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. 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

func (p *CommonProvider) TTL(ctx context.Context, key string) (time.Duration, error)

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

func (p *CommonProvider) ZAdd(ctx context.Context, key string, members ...Z) error

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.

func (*CommonProvider) ZRem added in v1.16.1

func (p *CommonProvider) ZRem(ctx context.Context, key string, members ...string) (int64, error)

ZRem removes members from sorted set of key and returns, how many of them were actually removed.

The number matters: it is the one, who got 1, that owns the member. Two readers, which saw the same entry, are told apart only this way.

func (*CommonProvider) ZScore added in v1.16.1

func (p *CommonProvider) ZScore(ctx context.Context, key, member string) (float64, error)

ZScore returns score of sorted set member.

ErrNotFound, if there is no such member or no such key at all.

type Option

type Option func(options *options) error

Option represents golang functional option pattern func for configuration.

func WithClientName

func WithClientName(clientName string) Option

func WithConnectionMaxIdleTime

func WithConnectionMaxIdleTime(connectionMaxIdleTime time.Duration) Option

func WithConnectionMaxLifetime

func WithConnectionMaxLifetime(connectionMaxLifetime time.Duration) Option

func WithContextTimeoutEnabled

func WithContextTimeoutEnabled(contextTimeoutEnabled bool) Option

func WithDB

func WithDB(db int) Option

func WithDialTimeout

func WithDialTimeout(dialTimeout time.Duration) Option

func WithHost

func WithHost(host string) Option

func WithMaxActiveConnections

func WithMaxActiveConnections(maxActiveConnections int) Option

func WithMaxIdleConnections

func WithMaxIdleConnections(maxIdleConnections int) Option

func WithMaxRetries

func WithMaxRetries(maxRetries int) Option

func WithMaxRetryBackoff

func WithMaxRetryBackoff(maxRetryBackoff time.Duration) Option

func WithMinIdleConnections

func WithMinIdleConnections(minIdleConnections int) Option

func WithMinRetryBackoff

func WithMinRetryBackoff(minRetryBackoff time.Duration) Option

func WithPassword

func WithPassword(password string) Option

func WithPoolFIFO

func WithPoolFIFO(poolFIFO bool) Option

func WithPoolSize

func WithPoolSize(poolSize int) Option

func WithPoolTimeout

func WithPoolTimeout(poolTimeout time.Duration) Option

func WithPort

func WithPort(port int) Option

func WithReadTimeout

func WithReadTimeout(readTimeout time.Duration) Option

func WithUsername

func WithUsername(username string) Option

func WithWriteTimeout

func WithWriteTimeout(writeTimeout time.Duration) Option

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.

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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