cache

package
v1.34.3 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2024 License: MPL-2.0 Imports: 4 Imported by: 1

Documentation

Overview

Package cache provides the ability to define distributed Redis cache clusters and functionality to use them in a fully type-safe manner.

For more information see https://encore.dev/docs/develop/caching

Index

Constants

This section is empty.

Variables

View Source
var (
	// NeverExpire is a WriteOption indicating the key should never expire.
	NeverExpire = expiryTime(neverExpire)

	// KeepTTL is a WriteOption indicating the key's TTL should be kept the same.
	KeepTTL = expiryTime(keepTTL)
)
View Source
var KeyExists = errors.New("key already exists")

KeyExists is the error reported when a key already exists and the requested operation is specified to only apply to keys that do not already exist. It must be checked against with errors.Is.

View Source
var Miss = errors.New("cache miss")

Miss is the error value reported when a key is missing from the cache. It must be checked against with errors.Is.

Functions

This section is empty.

Types

type BasicType

type BasicType interface {
	string | int | int64 | float64
}

BasicType is a constraint for basic types that can be used as element types in Redis lists and sets.

type Cluster

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

Cluster represents a Redis cache cluster.

func NewCluster

func NewCluster(name string, cfg ClusterConfig) (_ *Cluster)

NewCluster declares a new cache cluster.

See https://encore.dev/docs/develop/caching for more information.

type ClusterConfig

type ClusterConfig struct {
	// EvictionPolicy decides how the cache evicts existing keys
	// to make room for new data when the max memory limit is reached.
	//
	// If not specified the cache defaults to AllKeysLRU.
	EvictionPolicy EvictionPolicy
}

ClusterConfig represents the configuration of cache clusters.

type EvictionPolicy

type EvictionPolicy string

An EvictionPolicy describes how the cache evicts keys to make room for new data when the maximum memory limit is reached.

See https://redis.io/docs/manual/eviction/#eviction-policies for more information.

const (
	// AllKeysLRU keeps most recently used keys and removes least recently used (LRU) keys.
	// This is a good default choice for most cache use cases if you're not sure.
	AllKeysLRU EvictionPolicy = "allkeys-lru"

	// AllKeysLFU keeps frequently used keys and removes least frequently used (LFU) keys.
	AllKeysLFU EvictionPolicy = "allkeys-lfu"

	// AllKeysRandom randomly removes keys as needed.
	AllKeysRandom EvictionPolicy = "allkeys-random"

	// VolatileLRU removes least recently used keys with an expiration set.
	// It behaves like NoEviction if there are no keys to evict with an expiration set.
	VolatileLRU EvictionPolicy = "volatile-lru"

	// VolatileLFU removes least frequently used keys with an expiration set.
	// It behaves like NoEviction if there are no keys to evict with an expiration set.
	VolatileLFU EvictionPolicy = "volatile-lfu"

	// VolatileTTL removes keys with an expiration set and the shortest time to live (TTL).
	// It behaves like NoEviction if there are no keys to evict with an expiration set.
	VolatileTTL EvictionPolicy = "volatile-ttl"

	// VolatileRandom randomly removes keys with an expiration set.
	// It behaves like NoEviction if there are no keys to evict with an expiration set.
	VolatileRandom EvictionPolicy = "volatile-random"

	// NoEviction does not evict any keys, and instead returns an error to the client
	// when the max memory limit is reached.
	NoEviction EvictionPolicy = "noeviction"
)

The eviction policies Encore supports. See https://redis.io/docs/manual/eviction/#eviction-policies for more information.

type ExpiryFunc

type ExpiryFunc func(now time.Time) time.Time

ExpiryFunc is a function that reports when a key should expire given the current time. It can be used as a WriteOption to customize the expiration for that particular operation.

func ExpireDailyAt

func ExpireDailyAt(hour, minute, second int, loc *time.Location) (_ ExpiryFunc)

ExpireDailyAt returns an ExpiryFunc that expires keys daily at the given time of day in loc. ExpireDailyAt panics if loc is nil.

func ExpireIn

func ExpireIn(dur time.Duration) (_ ExpiryFunc)

ExpireIn returns an ExpiryFunc that expires keys after a constant duration.

type FloatKeyspace

type FloatKeyspace[K any] struct {
	// contains filtered or unexported fields
}

FloatKeyspace is a cache keyspace that stores float64 values.

func NewFloatKeyspace

func NewFloatKeyspace[K any](cluster *Cluster, cfg KeyspaceConfig) (_ *FloatKeyspace[K])

NewFloatKeyspace creates a keyspace that stores float64 values in the given cluster.

The type parameter K specifies the key type, which can either be a named struct type or a basic type (string, int, etc).

func (*FloatKeyspace[K]) Decrement added in v1.7.1

func (*FloatKeyspace[K]) Decrement(ctx context.Context, key K, delta float64) (newVal float64, err error)

Decrement decrements the number stored in key by delta, and returns the new value.

If the key does not exist it is first created with a value of 0 before decrementing.

Negative values can be used to increase the value, but typically you want to use the Increment method for that.

See https://redis.io/commands/incrbyfloat/ for more information.

func (*FloatKeyspace[K]) Delete

func (*FloatKeyspace[K]) Delete(ctx context.Context, keys ...K) (deleted int, err error)

Delete deletes the specified keys.

If a key does not exist it is ignored.

It reports the number of keys that were deleted.

See https://redis.io/commands/del/ for more information.

func (*FloatKeyspace[K]) Get

func (*FloatKeyspace[K]) Get(ctx context.Context, key K) (_ float64, _ error)

Get gets the value stored at key. If the key does not exist, it returns an error matching Miss.

See https://redis.io/commands/get/ for more information.

func (*FloatKeyspace[K]) GetAndDelete

func (*FloatKeyspace[K]) GetAndDelete(ctx context.Context, key K) (oldVal float64, err error)

GetAndDelete deletes the key and returns the previously stored value. If the key does not already exist, it does nothing and returns 0, nil.

See https://redis.io/commands/getdel/ for more information.

func (*FloatKeyspace[K]) GetAndSet

func (*FloatKeyspace[K]) GetAndSet(ctx context.Context, key K, val float64) (oldVal float64, err error)

GetAndSet updates the value of key to val and returns the previously stored value. If the key does not already exist, it sets it and returns 0, nil.

See https://redis.io/commands/getset/ for more information.

func (*FloatKeyspace[K]) Increment

func (*FloatKeyspace[K]) Increment(ctx context.Context, key K, delta float64) (newVal float64, err error)

Increment increments the number stored in key by delta, and returns the new value.

If the key does not exist it is first created with a value of 0 before incrementing.

Negative values can be used to decrease the value, but typically you want to use the Decrement method for that.

See https://redis.io/commands/incrbyfloat/ for more information.

func (*FloatKeyspace[K]) Replace

func (*FloatKeyspace[K]) Replace(ctx context.Context, key K, val float64) (_ error)

Replace replaces the existing value stored at key to val. If the key does not already exist, it reports an error matching Miss.

See https://redis.io/commands/set/ for more information.

func (*FloatKeyspace[K]) Set

func (*FloatKeyspace[K]) Set(ctx context.Context, key K, val float64) (_ error)

Set updates the value stored at key to val.

See https://redis.io/commands/set/ for more information.

func (*FloatKeyspace[K]) SetIfNotExists

func (*FloatKeyspace[K]) SetIfNotExists(ctx context.Context, key K, val float64) (_ error)

SetIfNotExists sets the value stored at key to val, but only if the key does not exist beforehand. If the key already exists, it reports an error matching KeyExists.

See https://redis.io/commands/setnx/ for more information.

func (*FloatKeyspace[K]) With

func (*FloatKeyspace[K]) With(opts ...WriteOption) (_ *FloatKeyspace[K])

With returns a reference to the same keyspace but with customized write options. The primary use case is for overriding the expiration time for certain cache operations.

It is intended to be used with method chaining:

myKeyspace.With(cache.ExpireIn(3 * time.Second)).Set(...)

type IntKeyspace

type IntKeyspace[K any] struct {
	// contains filtered or unexported fields
}

IntKeyspace is a cache keyspace that stores int64 values.

func NewIntKeyspace

func NewIntKeyspace[K any](cluster *Cluster, cfg KeyspaceConfig) (_ *IntKeyspace[K])

NewIntKeyspace creates a keyspace that stores int64 values in the given cluster.

The type parameter K specifies the key type, which can either be a named struct type or a basic type (string, int, etc).

func (*IntKeyspace[K]) Decrement added in v1.7.1

func (*IntKeyspace[K]) Decrement(ctx context.Context, key K, delta int64) (newVal int64, err error)

Decrement decrements the number stored in key by delta, and returns the new value.

If the key does not exist it is first created with a value of 0 before decrementing.

Negative values can be used to increase the value, but typically you want to use the Increment method for that.

See https://redis.io/commands/decrby/ for more information.

func (*IntKeyspace[K]) Delete

func (*IntKeyspace[K]) Delete(ctx context.Context, keys ...K) (deleted int, err error)

Delete deletes the specified keys.

If a key does not exist it is ignored.

It reports the number of keys that were deleted.

See https://redis.io/commands/del/ for more information.

func (*IntKeyspace[K]) Get

func (*IntKeyspace[K]) Get(ctx context.Context, key K) (_ int64, _ error)

Get gets the value stored at key. If the key does not exist, it returns an error matching Miss.

See https://redis.io/commands/get/ for more information.

func (*IntKeyspace[K]) GetAndDelete

func (*IntKeyspace[K]) GetAndDelete(ctx context.Context, key K) (oldVal int64, err error)

GetAndDelete deletes the key and returns the previously stored value. If the key does not already exist, it does nothing and returns 0, nil.

See https://redis.io/commands/getdel/ for more information.

func (*IntKeyspace[K]) GetAndSet

func (*IntKeyspace[K]) GetAndSet(ctx context.Context, key K, val int64) (oldVal int64, err error)

GetAndSet updates the value of key to val and returns the previously stored value. If the key does not already exist, it sets it and returns 0, nil.

See https://redis.io/commands/getset/ for more information.

func (*IntKeyspace[K]) Increment

func (*IntKeyspace[K]) Increment(ctx context.Context, key K, delta int64) (newVal int64, err error)

Increment increments the number stored in key by delta, and returns the new value.

If the key does not exist it is first created with a value of 0 before incrementing.

Negative values can be used to decrease the value, but typically you want to use the Decrement method for that.

See https://redis.io/commands/incrby/ for more information.

func (*IntKeyspace[K]) Replace

func (*IntKeyspace[K]) Replace(ctx context.Context, key K, val int64) (_ error)

Replace replaces the existing value stored at key to val. If the key does not already exist, it reports an error matching Miss.

See https://redis.io/commands/set/ for more information.

func (*IntKeyspace[K]) Set

func (*IntKeyspace[K]) Set(ctx context.Context, key K, val int64) (_ error)

Set updates the value stored at key to val.

See https://redis.io/commands/set/ for more information.

func (*IntKeyspace[K]) SetIfNotExists

func (*IntKeyspace[K]) SetIfNotExists(ctx context.Context, key K, val int64) (_ error)

SetIfNotExists sets the value stored at key to val, but only if the key does not exist beforehand. If the key already exists, it reports an error matching KeyExists.

See https://redis.io/commands/setnx/ for more information.

func (*IntKeyspace[K]) With

func (*IntKeyspace[K]) With(opts ...WriteOption) (_ *IntKeyspace[K])

With returns a reference to the same keyspace but with customized write options. The primary use case is for overriding the expiration time for certain cache operations.

It is intended to be used with method chaining:

myKeyspace.With(cache.ExpireIn(3 * time.Second)).Set(...)

type KeyspaceConfig

type KeyspaceConfig struct {
	// KeyPattern is a string literal representing the
	// cache key pattern for this keyspace.
	KeyPattern constStr

	// DefaultExpiry specifies the default key expiry for cache items
	// in this keyspace.
	//
	// When set, all write operations set (for new keys)
	// or update (for existing keys) the expiration time.
	//
	// When updating the expiration time Encore always
	// performs the combined operation atomically.
	//
	// If nil, cache items have no expiry date by default.
	//
	// The default behavior can be overridden by passing in
	// an ExpiryFunc or KeepTTL as a WriteOption to a specific operation.
	DefaultExpiry ExpiryFunc
}

KeyspaceConfig specifies the configuration options for a cache keyspace.

type ListKeyspace

type ListKeyspace[K any, V BasicType] struct {
	// contains filtered or unexported fields
}

ListKeyspace represents a set of cache keys, each containing an ordered list of values of type V.

func NewListKeyspace

func NewListKeyspace[K any, V BasicType](cluster *Cluster, cfg KeyspaceConfig) (_ *ListKeyspace[K, V])

NewListKeyspace creates a keyspace that stores ordered lists in the given cluster.

The type parameter K specifies the key type, which can either be a named struct type or a basic type (string, int, etc).

The type parameter V specifies the value type, which is the type of the elements in each list. It must be a basic type (string, int, int64, or float64).

func (*ListKeyspace[K, V]) Delete

func (*ListKeyspace[K, V]) Delete(ctx context.Context, keys ...K) (deleted int, err error)

Delete deletes the specified keys.

If a key does not exist it is ignored.

It reports the number of keys that were deleted.

See https://redis.io/commands/del/ for more information.

func (*ListKeyspace[K, V]) Get

func (*ListKeyspace[K, V]) Get(ctx context.Context, key K, idx int64) (val V, err error)

Get returns the value of list element with the given idx.

Negative indices can be used to indicate offsets from the end of the list, where -1 is the last element of the list, -2 the penultimate element, and so on.

For out of bounds indices or the key not existing in the cache, an error matching Miss is returned.

See https://redis.io/commands/lget/ for more information.

func (*ListKeyspace[K, V]) GetRange

func (*ListKeyspace[K, V]) GetRange(ctx context.Context, key K, start, stop int64) (_ []V, _ error)

GetRange returns all the elements in the list stored at key between start and stop.

Negative indices can be used to indicate offsets from the end of the list, where -1 is the last element of the list, -2 the penultimate element, and so on.

If the key does not exist it returns an empty list.

See https://redis.io/commands/lrange/ for more information.

func (*ListKeyspace[K, V]) InsertAfter

func (*ListKeyspace[K, V]) InsertAfter(ctx context.Context, key K, needle, newVal V) (newLen int64, err error)

InsertAfter inserts newVal into the list stored at key, at the position just after needle.

It reports the new list length.

If the list stored at key does not contain needle the value is not inserted, and an error matching Miss is returned.

See https://redis.io/commands/linsert/ for more information.

func (*ListKeyspace[K, V]) InsertBefore

func (*ListKeyspace[K, V]) InsertBefore(ctx context.Context, key K, needle, newVal V) (newLen int64, err error)

InsertBefore inserts newVal into the list stored at key, at the position just before needle.

If the list stored at key does not contain needle the value is not inserted, and an error matching Miss is returned.

It returns the new list length.

See https://redis.io/commands/linsert/ for more information.

func (*ListKeyspace[K, V]) Items

func (*ListKeyspace[K, V]) Items(ctx context.Context, key K) (_ []V, _ error)

Items returns all the elements in the list stored at key.

If the key does not exist it returns an empty list.

See https://redis.io/commands/lrange/ for more information.

func (*ListKeyspace[K, V]) Len

func (*ListKeyspace[K, V]) Len(ctx context.Context, key K) (length int64, err error)

Len reports the length of the list stored at key.

Non-existing keys are considered as empty lists.

See https://redis.io/commands/llen/ for more information.

func (*ListKeyspace[K, V]) Move

func (*ListKeyspace[K, V]) Move(ctx context.Context, src, dst K, fromPos, toPos ListPos) (moved V, err error)

Move atomically moves an element from the list stored at src to the list stored at dst.

The value moved can be either the head (fromPos == Left) or tail (fromPos == Right) of the list at src. Similarly, the value can be placed either at the head (toPos == Left) or tail (toPos == Right) of the list at dst.

If src does not exist it reports an error matching Miss.

If src and dst are the same list, the value is atomically rotated from one end to the other when fromPos != toPos, or if fromPos == toPos nothing happens.

func (*ListKeyspace[K, V]) PopLeft

func (*ListKeyspace[K, V]) PopLeft(ctx context.Context, key K) (val V, err error)

PopLeft pops a single element off the head of the list stored at key and returns it. If the key does not exist, it returns an error matching Miss.

See https://redis.io/commands/lpop/ for more information.

func (*ListKeyspace[K, V]) PopRight

func (*ListKeyspace[K, V]) PopRight(ctx context.Context, key K) (val V, err error)

PopRight pops a single element off the tail of the list stored at key and returns it. If the key does not exist, it returns an error matching Miss.

See https://redis.io/commands/rpop/ for more information.

func (*ListKeyspace[K, V]) PushLeft

func (*ListKeyspace[K, V]) PushLeft(ctx context.Context, key K, values ...V) (newLen int64, err error)

PushLeft pushes one or more values at the head of the list stored at key. If the key does not already exist, it is first created as an empty list.

If multiple values are given, they are inserted one after another, starting with the leftmost value. For instance,

PushLeft(ctx, "mylist", "a", "b", "c")

will result in a list containing "c" as its first element, "b" as its second element, and "a" as its third element.

See https://redis.io/commands/lpush/ for more information.

func (*ListKeyspace[K, V]) PushRight

func (*ListKeyspace[K, V]) PushRight(ctx context.Context, key K, values ...V) (newLen int64, err error)

PushRight pushes one or more values at the tail of the list stored at key. If the key does not already exist, it is first created as an empty list.

If multiple values are given, they are inserted one after another, starting with the leftmost value. For instance,

PushRight(ctx, "mylist", "a", "b", "c")

will result in a list containing "c" as its last element, "b" as its second to last element, and "a" as its third-to-last element.

See https://redis.io/commands/rpush/ for more information.

func (*ListKeyspace[K, V]) RemoveAll

func (*ListKeyspace[K, V]) RemoveAll(ctx context.Context, key K, needle V) (removed int64, err error)

RemoveAll removes all values equal to needle in the list stored at key.

It reports the number of elements removed.

If the list does not contain needle, or the list does not exist, it reports 0, nil.

See https://redis.io/commands/lrem/ for more information.

func (*ListKeyspace[K, V]) RemoveFirst

func (*ListKeyspace[K, V]) RemoveFirst(ctx context.Context, key K, count int64, needle V) (removed int64, err error)

RemoveFirst removes the first 'count' values equal to needle in the list stored at key.

It reports the number of elements removed.

If the list does not contain needle, or the list does not exist, it reports 0, nil.

See https://redis.io/commands/lrem/ for more information.

func (*ListKeyspace[K, V]) RemoveLast

func (*ListKeyspace[K, V]) RemoveLast(ctx context.Context, key K, count int64, needle V) (removed int64, err error)

RemoveLast removes the last 'count' values equal to needle in the list stored at key.

It reports the number of elements removed.

If the list does not contain needle, or the list does not exist, it reports 0, nil.

See https://redis.io/commands/lrem/ for more information.

func (*ListKeyspace[K, V]) Set

func (*ListKeyspace[K, V]) Set(ctx context.Context, key K, idx int64, val V) (err error)

Set updates the list element with the given idx to val.

Negative indices can be used to indicate offsets from the end of the list, where -1 is the last element of the list, -2 the penultimate element, and so on.

An error is returned for out of bounds indices.

See https://redis.io/commands/lset/ for more information.

func (*ListKeyspace[K, V]) Trim

func (*ListKeyspace[K, V]) Trim(ctx context.Context, key K, start, stop int64) (_ error)

Trim trims the list stored at key to only contain the elements between the indices start and stop (inclusive). Both start and stop are zero-based indices.

Negative indices can be used to indicate offsets from the end of the list, where -1 is the last element of the list, -2 the penultimate element, and so on.

Out of range indices are valid and are treated as if they specify the start or end of the list, respectively. If start > stop the end result is an empty list.

See https://redis.io/commands/ltrim/ for more information.

func (*ListKeyspace[K, V]) With

func (*ListKeyspace[K, V]) With(opts ...WriteOption) (_ *ListKeyspace[K, V])

With returns a reference to the same keyspace but with customized write options. The primary use case is for overriding the expiration time for certain cache operations.

It is intended to be used with method chaining:

myKeyspace.With(cache.ExpireIn(3 * time.Second)).Set(...)

type ListPos

type ListPos string
const (
	Left  ListPos = "LEFT"
	Right ListPos = "RIGHT"
)

type OpError

type OpError struct {
	Operation string
	RawKey    string
	Err       error
}

An OpError describes the operation that failed.

func (*OpError) Error

func (*OpError) Error() (_ string)

func (*OpError) Unwrap

func (*OpError) Unwrap() (_ error)

type SetKeyspace

type SetKeyspace[K any, V BasicType] struct {
	// contains filtered or unexported fields
}

SetKeyspace represents a set of cache keys, each containing an unordered set of values of type V.

func NewSetKeyspace

func NewSetKeyspace[K any, V BasicType](cluster *Cluster, cfg KeyspaceConfig) (_ *SetKeyspace[K, V])

NewSetKeyspace creates a keyspace that stores unordered sets in the given cluster.

The type parameter K specifies the key type, which can either be a named struct type or a basic type (string, int, etc).

The type parameter V specifies the value type, which is the type of the elements in each set. It must be a basic type (string, int, int64, or float64).

func (*SetKeyspace[K, V]) Add

func (*SetKeyspace[K, V]) Add(ctx context.Context, key K, values ...V) (added int, err error)

Add adds one or more values to the set stored at key. If the key does not already exist, it is first created as an empty set.

It reports the number of values that were added to the set, not including values already present beforehand.

See https://redis.io/commands/sadd/ for more information.

func (*SetKeyspace[K, V]) Contains

func (*SetKeyspace[K, V]) Contains(ctx context.Context, key K, val V) (contains bool, err error)

Contains reports whether the set stored at key contains the given value.

If the key does not exist it reports false, nil.

See https://redis.io/commands/sismember/ for more information.

func (*SetKeyspace[K, V]) Delete

func (*SetKeyspace[K, V]) Delete(ctx context.Context, keys ...K) (deleted int, err error)

Delete deletes the specified keys.

If a key does not exist it is ignored.

It reports the number of keys that were deleted.

See https://redis.io/commands/del/ for more information.

func (*SetKeyspace[K, V]) Diff

func (*SetKeyspace[K, V]) Diff(ctx context.Context, keys ...K) (_ []V, _ error)

Diff computes the set difference, between the first set and all the consecutive sets.

Set difference means the values present in the first set that are not present in any of the other sets.

Keys that do not exist are considered as empty sets.

At least one key must be provided: if no keys are provided an error is reported.

See https://redis.io/commands/sdiff/ for more information.

func (*SetKeyspace[K, V]) DiffMap

func (*SetKeyspace[K, V]) DiffMap(ctx context.Context, keys ...K) (_ map[V]struct{}, _ error)

DiffMap is identical to Diff except it returns the values as a map.

See https://redis.io/commands/sdiff/ for more information.

func (*SetKeyspace[K, V]) DiffStore

func (*SetKeyspace[K, V]) DiffStore(ctx context.Context, destination K, keys ...K) (size int64, err error)

DiffStore computes the set difference between keys (like Diff) and stores the result in destination.

It reports the size of the resulting set.

See https://redis.io/commands/sdiffstore/ for more information.

func (*SetKeyspace[K, V]) Intersect

func (*SetKeyspace[K, V]) Intersect(ctx context.Context, keys ...K) (_ []V, _ error)

Intersect computes the set intersection between the sets stored at the given keys.

Set intersection means the values common to all the provided sets.

Keys that do not exist are considered to be empty sets. As a result, if any key is missing the final result is the empty set.

At least one key must be provided: if no keys are provided an error is reported.

See https://redis.io/commands/sinter/ for more information.

func (*SetKeyspace[K, V]) IntersectMap

func (*SetKeyspace[K, V]) IntersectMap(ctx context.Context, keys ...K) (_ map[V]struct{}, _ error)

IntersectMap is identical to Intersect except it returns the values as a map.

See https://redis.io/commands/sinter/ for more information.

func (*SetKeyspace[K, V]) IntersectStore

func (*SetKeyspace[K, V]) IntersectStore(ctx context.Context, destination K, keys ...K) (size int64, err error)

IntersectStore computes the set intersection between keys (like Intersect) and stores the result in destination.

It reports the size of the resulting set.

See https://redis.io/commands/sinterstore/ for more information.

func (*SetKeyspace[K, V]) Items

func (*SetKeyspace[K, V]) Items(ctx context.Context, key K) (values []V, err error)

Items returns the elements in the set stored at key.

If the key does not exist it returns an empty slice and no error.

See https://redis.io/commands/smembers/ for more information.

func (*SetKeyspace[K, V]) ItemsMap

func (*SetKeyspace[K, V]) ItemsMap(ctx context.Context, key K) (values map[V]struct{}, err error)

ItemsMap is identical to Items except it returns the values as a map.

If the key does not exist it returns an empty (but non-nil) map and no error.

See https://redis.io/commands/smembers/ for more information.

func (*SetKeyspace[K, V]) Len

func (*SetKeyspace[K, V]) Len(ctx context.Context, key K) (length int64, err error)

Len reports the number of elements in the set stored at key.

If the key does not exist it reports 0, nil.

See https://redis.io/commands/slen/ for more information.

func (*SetKeyspace[K, V]) Move

func (*SetKeyspace[K, V]) Move(ctx context.Context, src, dst K, val V) (moved bool, err error)

Move atomically moves the given value from the set stored at src to the set stored at dst.

If the element does not exist in src it reports false, nil.

If the element already exists in dst it is still removed from src and Move still reports true, nil.

func (*SetKeyspace[K, V]) Pop

func (*SetKeyspace[K, V]) Pop(ctx context.Context, key K, count int) (values []V, err error)

Pop removes up to 'count' random elements (bounded by the set's size) from the set stored at key and returns them.

If the set is empty it returns an empty slice and no error.

See https://redis.io/commands/spop/ for more information.

func (*SetKeyspace[K, V]) PopOne

func (*SetKeyspace[K, V]) PopOne(ctx context.Context, key K) (val V, err error)

PopOne removes a random element from the set stored at key and returns it.

If the set is empty it reports an error matching Miss.

See https://redis.io/commands/spop/ for more information.

func (*SetKeyspace[K, V]) Remove

func (*SetKeyspace[K, V]) Remove(ctx context.Context, key K, values ...V) (removed int, err error)

Remove removes one or more values from the set stored at key.

If a value is not present in the set is it ignored.

Remove reports the number of values that were removed from the set. If the key does not already exist, it is a no-op and reports 0, nil.

See https://redis.io/commands/srem/ for more information.

func (*SetKeyspace[K, V]) Sample

func (*SetKeyspace[K, V]) Sample(ctx context.Context, key K, count int) (values []V, err error)

Sample returns up to 'count' distinct random elements from the set stored at key. The same element is never returned multiple times.

If the key does not exist it returns an empty slice and no error.

See https://redis.io/commands/srandmember/ for more information.

func (*SetKeyspace[K, V]) SampleOne

func (*SetKeyspace[K, V]) SampleOne(ctx context.Context, key K) (val V, err error)

SampleOne returns a random member from the set stored at key.

If the key does not exist it reports an error matching Miss.

See https://redis.io/commands/srandmember/ for more information.

func (*SetKeyspace[K, V]) SampleWithReplacement

func (*SetKeyspace[K, V]) SampleWithReplacement(ctx context.Context, key K, count int) (values []V, err error)

SampleWithReplacement returns count random elements from the set stored at key. The same element may be returned multiple times.

If the key does not exist it returns an empty slice and no error.

See https://redis.io/commands/srandmember/ for more information.

func (*SetKeyspace[K, V]) Union

func (*SetKeyspace[K, V]) Union(ctx context.Context, keys ...K) (_ []V, _ error)

Union computes the set union between the sets stored at the given keys.

Set union means the values present in at least one of the provided sets.

Keys that do not exist are considered to be empty sets.

At least one key must be provided: if no keys are provided an error is reported.

See https://redis.io/commands/sunion/ for more information.

func (*SetKeyspace[K, V]) UnionMap

func (*SetKeyspace[K, V]) UnionMap(ctx context.Context, keys ...K) (_ map[V]struct{}, _ error)

UnionMap is identical to Union except it returns the values as a map.

See https://redis.io/commands/sunion/ for more information.

func (*SetKeyspace[K, V]) UnionStore

func (*SetKeyspace[K, V]) UnionStore(ctx context.Context, destination K, keys ...K) (size int64, err error)

UnionStore computes the set union between sets (like Union) and stores the result in destination.

It reports the size of the resulting set.

See https://redis.io/commands/sunionstore/ for more information.

func (*SetKeyspace[K, V]) With

func (*SetKeyspace[K, V]) With(opts ...WriteOption) (_ *SetKeyspace[K, V])

With returns a reference to the same keyspace but with customized write options. The primary use case is for overriding the expiration time for certain cache operations.

It is intended to be used with method chaining:

myKeyspace.With(cache.ExpireIn(3 * time.Second)).Set(...)

type StringKeyspace

type StringKeyspace[K any] struct {
	// contains filtered or unexported fields
}

StringKeyspace represents a set of cache keys that hold string values.

func NewStringKeyspace

func NewStringKeyspace[K any](cluster *Cluster, cfg KeyspaceConfig) (_ *StringKeyspace[K])

NewStringKeyspace creates a keyspace that stores string values in the given cluster.

The type parameter K specifies the key type, which can either be a named struct type or a basic type (string, int, etc).

func (*StringKeyspace[K]) Append

func (*StringKeyspace[K]) Append(ctx context.Context, key K, val string) (newLen int64, err error)

Append appends to the string with the given key.

If the key does not exist it is first created and set as the empty string, causing Append to behave like Set.

It returns the new string length.

See https://redis.io/commands/append/ for more information.

func (*StringKeyspace[K]) Delete

func (*StringKeyspace[K]) Delete(ctx context.Context, keys ...K) (deleted int, err error)

Delete deletes the specified keys.

If a key does not exist it is ignored.

It reports the number of keys that were deleted.

See https://redis.io/commands/del/ for more information.

func (*StringKeyspace[K]) Get

func (*StringKeyspace[K]) Get(ctx context.Context, key K) (_ string, _ error)

Get gets the value stored at key. If the key does not exist, it returns an error matching Miss.

See https://redis.io/commands/get/ for more information.

func (*StringKeyspace[K]) GetAndDelete

func (*StringKeyspace[K]) GetAndDelete(ctx context.Context, key K) (oldVal string, err error)

GetAndDelete deletes the key and returns the previously stored value. If the key does not already exist, it does nothing and returns "", nil.

See https://redis.io/commands/getdel/ for more information.

func (*StringKeyspace[K]) GetAndSet

func (*StringKeyspace[K]) GetAndSet(ctx context.Context, key K, val string) (oldVal string, err error)

GetAndSet updates the value of key to val and returns the previously stored value. If the key does not already exist, it sets it and returns "", nil.

See https://redis.io/commands/getset/ for more information.

func (*StringKeyspace[K]) GetRange

func (*StringKeyspace[K]) GetRange(ctx context.Context, key K, from, to int64) (val string, err error)

GetRange returns a substring of the string value stored in key.

The from and to values are zero-based indices, but unlike Go slicing the 'to' value is inclusive.

Negative values can be used in order to provide an offset starting from the end of the string, so -1 means the last character and -len(str) the first character, and so forth.

If the string does not exist it returns the empty string.

See https://redis.io/commands/setrange/ for more information.

func (*StringKeyspace[K]) Len

func (*StringKeyspace[K]) Len(ctx context.Context, key K) (length int64, err error)

Len reports the length of the string value stored at key.

Non-existing keys are considered as empty strings.

See https://redis.io/commands/strlen/ for more information.

func (*StringKeyspace[K]) Replace

func (*StringKeyspace[K]) Replace(ctx context.Context, key K, val string) (_ error)

Replace replaces the existing value stored at key to val. If the key does not already exist, it reports an error matching Miss.

See https://redis.io/commands/set/ for more information.

func (*StringKeyspace[K]) Set

func (*StringKeyspace[K]) Set(ctx context.Context, key K, val string) (_ error)

Set updates the value stored at key to val.

See https://redis.io/commands/set/ for more information.

func (*StringKeyspace[K]) SetIfNotExists

func (*StringKeyspace[K]) SetIfNotExists(ctx context.Context, key K, val string) (_ error)

SetIfNotExists sets the value stored at key to val, but only if the key does not exist beforehand. If the key already exists, it reports an error matching KeyExists.

See https://redis.io/commands/setnx/ for more information.

func (*StringKeyspace[K]) SetRange

func (*StringKeyspace[K]) SetRange(ctx context.Context, key K, offset int64, val string) (newLen int64, err error)

SetRange overwrites part of the string stored at key, starting at the zero-based offset and for the entire length of val, extending the string if necessary to make room for val.

If the offset is larger than the current string length stored at key, the string is first padded with zero-bytes to make offset fit.

Non-existing keys are considered as empty strings.

See https://redis.io/commands/setrange/ for more information.

func (*StringKeyspace[K]) With

func (*StringKeyspace[K]) With(opts ...WriteOption) (_ *StringKeyspace[K])

With returns a reference to the same keyspace but with customized write options. The primary use case is for overriding the expiration time for certain cache operations.

It is intended to be used with method chaining:

myKeyspace.With(cache.ExpireIn(3 * time.Second)).Set(...)

type StructKeyspace

type StructKeyspace[K, V any] struct {
	// contains filtered or unexported fields
}

StructKeyspace represents a set of cache keys that hold struct values.

func NewStructKeyspace

func NewStructKeyspace[K, V any](cluster *Cluster, cfg KeyspaceConfig) (_ *StructKeyspace[K, V])

NewStructKeyspace creates a keyspace that stores structs in the given cluster.

The type parameter K specifies the key type, which can either be a named struct type or a basic type (string, int, etc).

The value parameter V specifies the named struct type that should be stored.

func (*StructKeyspace[K, V]) Delete

func (*StructKeyspace[K, V]) Delete(ctx context.Context, keys ...K) (deleted int, err error)

Delete deletes the specified keys.

If a key does not exist it is ignored.

It reports the number of keys that were deleted.

See https://redis.io/commands/del/ for more information.

func (*StructKeyspace[K, V]) Get

func (*StructKeyspace[K, V]) Get(ctx context.Context, key K) (_ V, _ error)

Get gets the value stored at key. If the key does not exist, it returns an error matching Miss.

See https://redis.io/commands/get/ for more information.

func (*StructKeyspace[K, V]) GetAndDelete

func (*StructKeyspace[K, V]) GetAndDelete(ctx context.Context, key K) (oldVal V, err error)

GetAndDelete deletes the key and returns the previously stored value. If the key does not already exist, it does nothing and returns the zero value of V and nil.

See https://redis.io/commands/getdel/ for more information.

func (*StructKeyspace[K, V]) GetAndSet

func (*StructKeyspace[K, V]) GetAndSet(ctx context.Context, key K, val V) (oldVal V, err error)

GetAndSet updates the value of key to val and returns the previously stored value. If the key does not already exist, it sets it and returns 0, nil.

See https://redis.io/commands/getset/ for more information.

func (*StructKeyspace[K, V]) Replace

func (*StructKeyspace[K, V]) Replace(ctx context.Context, key K, val V) (_ error)

Replace replaces the existing value stored at key to val. If the key does not already exist, it reports an error matching Miss.

See https://redis.io/commands/set/ for more information.

func (*StructKeyspace[K, V]) Set

func (*StructKeyspace[K, V]) Set(ctx context.Context, key K, val V) (_ error)

Set updates the value stored at key to val.

See https://redis.io/commands/set/ for more information.

func (*StructKeyspace[K, V]) SetIfNotExists

func (*StructKeyspace[K, V]) SetIfNotExists(ctx context.Context, key K, val V) (_ error)

SetIfNotExists sets the value stored at key to val, but only if the key does not exist beforehand. If the key already exists, it reports an error matching KeyExists.

See https://redis.io/commands/setnx/ for more information.

func (*StructKeyspace[K, V]) With

func (*StructKeyspace[K, V]) With(opts ...WriteOption) (_ *StructKeyspace[K, V])

With returns a reference to the same keyspace but with customized write options. The primary use case is for overriding the expiration time for certain cache operations.

It is intended to be used with method chaining:

myKeyspace.With(cache.ExpireIn(3 * time.Second)).Set(...)

type WriteOption

type WriteOption interface {
	// contains filtered or unexported methods
}

An WriteOption customizes the behavior of a single cache write operation.

Jump to

Keyboard shortcuts

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