docs/ts/runtime/storage-cache.mdx
CacheCluster represents a Redis cache cluster.
Create a new cluster using new CacheCluster(name).
Reference an existing cluster using CacheCluster.named(name).
import { CacheCluster } from "encore.dev/storage/cache";
const myCache = new CacheCluster("my-cache", {
evictionPolicy: "allkeys-lru",
});
new CacheCluster(name, cfg?): CacheCluster
Creates a new cache cluster with the given name and configuration.
string
The unique name for this cache cluster
Optional configuration for the cluster
static named<Name>(name): CacheCluster
Reference an existing cache cluster by name.
To create a new cache cluster, use new CacheCluster(...) instead.
Name extends string
StringLiteral<Name>
CacheError is the base class for all cache-related errors.
Errornew CacheError(msg): CacheError
string
Error.constructor
CacheKeyExists is thrown when attempting to set a key that already exists using setIfNotExists.
new CacheKeyExists(key): CacheKeyExists
string
CacheMiss is thrown when a cache key is not found.
new CacheMiss(key): CacheMiss
string
FloatKeyspace stores 64-bit floating point values.
const scores = new FloatKeyspace<string>(cluster, {
keyPattern: "score/:playerId",
});
await scores.set("player1", 100.5);
const newScore = await scores.increment("player1", 10.25);
BasicKeyspace<K, number>K
new FloatKeyspace<K>(cluster, config): FloatKeyspace<K>
BasicKeyspace<K, number>.constructor
protected readonly cluster: CacheCluster
BasicKeyspace.cluster
protected readonly config: KeyspaceConfig<K>
BasicKeyspace.config
protected readonly keyMapper: (key) => string
K
string
BasicKeyspace.keyMapper
decrement(
key,
delta?,
options?): Promise<number>;
Decrements the number stored at key by delta.
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 increment for that.
K
The cache key.
number = 1
The amount to decrement by (default 1).
Promise<number>
The new value after decrementing.
https://redis.io/commands/incrbyfloat/
delete(...keys): Promise<number>
Deletes the specified keys. If a key does not exist it is ignored.
...K[]
Promise<number>
The number of keys that were deleted.
https://redis.io/commands/del/
BasicKeyspace.delete
protected deserialize(data): number
Deserializes a Buffer from storage to a value.
Buffer
number
BasicKeyspace.deserialize
get(key): Promise<number | undefined>
Gets the value stored at key.
If the key does not exist, it returns undefined.
K
Promise<number | undefined>
The value, or undefined if the key does not exist.
https://redis.io/commands/get/
BasicKeyspace.get
getAndDelete(key): Promise<number | undefined>
Deletes the key and returns the previously stored value.
If the key does not already exist, it returns undefined.
K
Promise<number | undefined>
The previous value, or undefined if the key did not exist.
https://redis.io/commands/getdel/
BasicKeyspace.getAndDelete
getAndSet(
key,
value,
options?): Promise<number | undefined>;
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 undefined.
K
number
Promise<number | undefined>
The previous value, or undefined if the key did not exist.
https://redis.io/commands/getset/
BasicKeyspace.getAndSet
increment(
key,
delta?,
options?): Promise<number>;
Increments the number stored at key by delta.
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 decrement for that.
K
The cache key.
number = 1
The amount to increment by (default 1).
Promise<number>
The new value after incrementing.
https://redis.io/commands/incrbyfloat/
protected mapKey(key): string
Maps a key to its Redis key string.
K
string
BasicKeyspace.mapKey
multiGet(...keys): Promise<(number | undefined)[]>
Gets the values stored at multiple keys.
...K[]
Promise<(number | undefined)[]>
An array of values in the same order as the provided keys.
Each element is the value or undefined if the key was not found.
https://redis.io/commands/mget/
BasicKeyspace.multiGet
replace(
key,
value,
options?): Promise<void>;
Replaces the existing value stored at key to val.
K
number
Promise<void>
If the key does not already exist.
https://redis.io/commands/set/
BasicKeyspace.replace
protected resolveTtl(options?): number | undefined
Resolves the TTL for a write operation. Returns i64 sentinel for NAPI: undefined=no config, -1=KeepTTL, -2=Persist/NeverExpire, >=0=ms
number | undefined
BasicKeyspace.resolveTtl
protected serialize(value): Buffer
Serializes a value to a Buffer for storage.
number
Buffer
BasicKeyspace.serialize
set(
key,
value,
options?): Promise<void>;
Updates the value stored at key to val.
K
number
Promise<void>
https://redis.io/commands/set/
BasicKeyspace.set
setIfNotExists(
key,
value,
options?): Promise<void>;
Sets the value stored at key to val, but only if the key does not exist beforehand.
K
number
Promise<void>
If the key already exists.
https://redis.io/commands/setnx/
BasicKeyspace.setIfNotExists
with(options): this
Returns a shallow clone of this keyspace with the specified write options applied. This allows setting expiry for a chain of operations.
this
await myKeyspace.with({ expiry: expireIn(5000) }).set(key, value)
BasicKeyspace.with
IntKeyspace stores 64-bit integer values.
Values are floored to integers using Math.floor.
For fractional values, use FloatKeyspace instead.
const counters = new IntKeyspace<string>(cluster, {
keyPattern: "counter/:name",
});
await counters.set("page-views", 0);
const newCount = await counters.increment("page-views", 1);
BasicKeyspace<K, number>K
new IntKeyspace<K>(cluster, config): IntKeyspace<K>
IntKeyspace<K>
BasicKeyspace<K, number>.constructor
protected readonly cluster: CacheCluster
BasicKeyspace.cluster
protected readonly config: KeyspaceConfig<K>
BasicKeyspace.config
protected readonly keyMapper: (key) => string
K
string
BasicKeyspace.keyMapper
decrement(
key,
delta?,
options?): Promise<number>;
Decrements the number stored at key by delta.
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 increment for that.
K
The cache key.
number = 1
The amount to decrement by (default 1).
Promise<number>
The new value after decrementing.
https://redis.io/commands/decrby/
delete(...keys): Promise<number>
Deletes the specified keys. If a key does not exist it is ignored.
...K[]
Promise<number>
The number of keys that were deleted.
https://redis.io/commands/del/
BasicKeyspace.delete
protected deserialize(data): number
Deserializes a Buffer from storage to a value.
Buffer
number
BasicKeyspace.deserialize
get(key): Promise<number | undefined>
Gets the value stored at key.
If the key does not exist, it returns undefined.
K
Promise<number | undefined>
The value, or undefined if the key does not exist.
https://redis.io/commands/get/
BasicKeyspace.get
getAndDelete(key): Promise<number | undefined>
Deletes the key and returns the previously stored value.
If the key does not already exist, it returns undefined.
K
Promise<number | undefined>
The previous value, or undefined if the key did not exist.
https://redis.io/commands/getdel/
BasicKeyspace.getAndDelete
getAndSet(
key,
value,
options?): Promise<number | undefined>;
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 undefined.
K
number
Promise<number | undefined>
The previous value, or undefined if the key did not exist.
https://redis.io/commands/getset/
BasicKeyspace.getAndSet
increment(
key,
delta?,
options?): Promise<number>;
Increments the number stored at key by delta.
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 decrement for that.
K
The cache key.
number = 1
The amount to increment by (default 1).
Promise<number>
The new value after incrementing.
https://redis.io/commands/incrby/
protected mapKey(key): string
Maps a key to its Redis key string.
K
string
BasicKeyspace.mapKey
multiGet(...keys): Promise<(number | undefined)[]>
Gets the values stored at multiple keys.
...K[]
Promise<(number | undefined)[]>
An array of values in the same order as the provided keys.
Each element is the value or undefined if the key was not found.
https://redis.io/commands/mget/
BasicKeyspace.multiGet
replace(
key,
value,
options?): Promise<void>;
Replaces the existing value stored at key to val.
K
number
Promise<void>
If the key does not already exist.
https://redis.io/commands/set/
BasicKeyspace.replace
protected resolveTtl(options?): number | undefined
Resolves the TTL for a write operation. Returns i64 sentinel for NAPI: undefined=no config, -1=KeepTTL, -2=Persist/NeverExpire, >=0=ms
number | undefined
BasicKeyspace.resolveTtl
protected serialize(value): Buffer
Serializes a value to a Buffer for storage.
number
Buffer
BasicKeyspace.serialize
set(
key,
value,
options?): Promise<void>;
Updates the value stored at key to val.
K
number
Promise<void>
https://redis.io/commands/set/
BasicKeyspace.set
setIfNotExists(
key,
value,
options?): Promise<void>;
Sets the value stored at key to val, but only if the key does not exist beforehand.
K
number
Promise<void>
If the key already exists.
https://redis.io/commands/setnx/
BasicKeyspace.setIfNotExists
with(options): this
Returns a shallow clone of this keyspace with the specified write options applied. This allows setting expiry for a chain of operations.
this
await myKeyspace.with({ expiry: expireIn(5000) }).set(key, value)
BasicKeyspace.with
NumberListKeyspace stores lists of numeric values.
const scores = new NumberListKeyspace<string>(cluster, {
keyPattern: "scores/:gameId",
});
await scores.pushRight("game1", 100, 200, 300);
const allScores = await scores.items("game1");
ListKeyspace<K, number>K
new NumberListKeyspace<K>(cluster, config): NumberListKeyspace<K>
ListKeyspace<K, number>.constructor
protected readonly cluster: CacheCluster
ListKeyspace.cluster
protected readonly config: KeyspaceConfig<K>
ListKeyspace.config
protected readonly keyMapper: (key) => string
K
string
ListKeyspace.keyMapper
delete(...keys): Promise<number>
Deletes the specified keys. If a key does not exist it is ignored.
...K[]
Promise<number>
The number of keys that were deleted.
https://redis.io/commands/del/
ListKeyspace.delete
protected deserializeItem(data): number
Buffer
number
ListKeyspace.deserializeItem
get(key, index): Promise<number | undefined>
Returns the value of the list element at the given index.
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.
K
The cache key.
number
Zero-based index of the element to retrieve.
Promise<number | undefined>
The value at the index, or undefined if out of range or the key does not exist.
https://redis.io/commands/lindex/
ListKeyspace.get
getRange(
key,
start,
stop): Promise<number[]>;
Returns the elements in the list stored at key between start and stop (inclusive).
Both 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.
If the key does not exist it returns an empty array.
K
The cache key.
number
Start index (inclusive).
number
Stop index (inclusive).
Promise<number[]>
The elements in the specified range.
https://redis.io/commands/lrange/
ListKeyspace.getRange
insertAfter(
key,
pivot,
value,
options?): Promise<number>;
Inserts value into the list stored at key, at the position just after pivot.
If the list does not contain pivot, the value is not inserted and -1 is returned.
K
The cache key.
number
The existing element to insert after.
number
The value to insert.
Promise<number>
The new list length, or -1 if pivot was not found.
https://redis.io/commands/linsert/
ListKeyspace.insertAfter
insertBefore(
key,
pivot,
value,
options?): Promise<number>;
Inserts value into the list stored at key, at the position just before pivot.
If the list does not contain pivot, the value is not inserted and -1 is returned.
K
The cache key.
number
The existing element to insert before.
number
The value to insert.
Promise<number>
The new list length, or -1 if pivot was not found.
https://redis.io/commands/linsert/
ListKeyspace.insertBefore
items(key): Promise<number[]>
Returns all the elements in the list stored at key.
If the key does not exist it returns an empty array.
K
Promise<number[]>
All elements in the list.
https://redis.io/commands/lrange/
ListKeyspace.items
len(key): Promise<number>
Returns the length of the list stored at key.
Non-existing keys are considered as empty lists.
K
Promise<number>
The list length.
https://redis.io/commands/llen/
ListKeyspace.len
protected mapKey(key): string
Maps a key to its Redis key string.
K
string
ListKeyspace.mapKey
move(
src,
dst,
srcPos,
dstPos,
options?): Promise<number | undefined>;
Atomically moves an element from the list stored at src to the list stored at dst.
The value moved can be either the head (srcPos === "left") or tail (srcPos === "right")
of the list at src. Similarly, the value can be placed either at the head (dstPos === "left")
or tail (dstPos === "right") of the list at dst.
If src and dst are the same list, the value is atomically rotated from one end to the other
when srcPos !== dstPos, or if srcPos === dstPos nothing happens.
K
Source list key.
K
Destination list key.
Position to pop from in the source list.
Position to push to in the destination list.
Promise<number | undefined>
The moved element, or undefined if the source list does not exist.
https://redis.io/commands/lmove/
ListKeyspace.move
popLeft(key, options?): Promise<number | undefined>
Pops a single element off the head of the list stored at key.
K
Promise<number | undefined>
The popped value, or undefined if the key does not exist.
https://redis.io/commands/lpop/
ListKeyspace.popLeft
popRight(key, options?): Promise<number | undefined>
Pops a single element off the tail of the list stored at key.
K
Promise<number | undefined>
The popped value, or undefined if the key does not exist.
https://redis.io/commands/rpop/
ListKeyspace.popRight
pushLeft(key, ...values): Promise<number>
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(key, "a", "b", "c") will result in a list containing
"c" as its first element, "b" as its second, and "a" as its third.
K
...number[]
Promise<number>
The length of the list after the operation.
https://redis.io/commands/lpush/
ListKeyspace.pushLeft
pushRight(key, ...values): Promise<number>
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(key, "a", "b", "c") will result in a list containing
"a" as its first element, "b" as its second, and "c" as its third.
K
...number[]
Promise<number>
The length of the list after the operation.
https://redis.io/commands/rpush/
ListKeyspace.pushRight
removeAll(
key,
value,
options?): Promise<number>;
Removes all occurrences of value in the list stored at key.
If the list does not contain value, or the list does not exist, returns 0.
K
The cache key.
number
The value to remove.
Promise<number>
The number of elements removed.
https://redis.io/commands/lrem/
ListKeyspace.removeAll
removeFirst(
key,
count,
value,
options?): Promise<number>;
Removes the first count occurrences of value in the list stored at key,
scanning from head to tail.
If the list does not contain value, or the list does not exist, returns 0.
K
The cache key.
number
Maximum number of occurrences to remove.
number
The value to remove.
Promise<number>
The number of elements removed.
https://redis.io/commands/lrem/
ListKeyspace.removeFirst
removeLast(
key,
count,
value,
options?): Promise<number>;
Removes the last count occurrences of value in the list stored at key,
scanning from tail to head.
If the list does not contain value, or the list does not exist, returns 0.
K
The cache key.
number
Maximum number of occurrences to remove.
number
The value to remove.
Promise<number>
The number of elements removed.
https://redis.io/commands/lrem/
ListKeyspace.removeLast
protected resolveTtl(options?): number | undefined
Resolves the TTL for a write operation. Returns i64 sentinel for NAPI: undefined=no config, -1=KeepTTL, -2=Persist/NeverExpire, >=0=ms
number | undefined
ListKeyspace.resolveTtl
protected serializeItem(value): Buffer
number
Buffer
ListKeyspace.serializeItem
set(
key,
index,
value,
options?): Promise<void>;
Updates the list element at the given index.
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.
K
The cache key.
number
Zero-based index of the element to update.
number
The new value.
Promise<void>
If the index is out of range.
https://redis.io/commands/lset/
ListKeyspace.set
trim(
key,
start,
stop,
options?): Promise<void>;
Trims the list stored at key to only contain the elements between the indices
start and stop (inclusive). Both 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.
K
The cache key.
number
Start index (inclusive).
number
Stop index (inclusive).
Promise<void>
https://redis.io/commands/ltrim/
ListKeyspace.trim
with(options): this
Returns a shallow clone of this keyspace with the specified write options applied. This allows setting expiry for a chain of operations.
this
await myKeyspace.with({ expiry: expireIn(5000) }).set(key, value)
ListKeyspace.with
NumberSetKeyspace stores sets of unique numeric values.
const scores = new NumberSetKeyspace<string>(cluster, {
keyPattern: "unique-scores/:gameId",
});
await scores.add("game1", 100, 200, 300);
const hasScore = await scores.contains("game1", 100);
SetKeyspace<K, number>K
new NumberSetKeyspace<K>(cluster, config): NumberSetKeyspace<K>
SetKeyspace<K, number>.constructor
protected readonly cluster: CacheCluster
SetKeyspace.cluster
protected readonly config: KeyspaceConfig<K>
SetKeyspace.config
protected readonly keyMapper: (key) => string
K
string
SetKeyspace.keyMapper
add(key, ...members): Promise<number>
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.
K
...number[]
Promise<number>
The number of values that were added to the set, not including values already present beforehand.
https://redis.io/commands/sadd/
SetKeyspace.add
contains(key, member): Promise<boolean>
Reports whether the set stored at key contains the given value.
If the key does not exist it returns false.
K
number
Promise<boolean>
true if the member exists in the set, false otherwise.
https://redis.io/commands/sismember/
SetKeyspace.contains
delete(...keys): Promise<number>
Deletes the specified keys. If a key does not exist it is ignored.
...K[]
Promise<number>
The number of keys that were deleted.
https://redis.io/commands/del/
SetKeyspace.delete
protected deserializeItem(data): number
Buffer
number
SetKeyspace.deserializeItem
diff(...keys): Promise<number[]>
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.
...K[]
Keys of sets to compute difference for. At least one must be provided.
Promise<number[]>
Members in the first set but not in any of the other sets.
If no keys are provided.
https://redis.io/commands/sdiff/
SetKeyspace.diff
diffSet(...keys): Promise<Set<number>>
Identical to diff except it returns the values as a Set.
...K[]
Promise<Set<number>>
https://redis.io/commands/sdiff/
SetKeyspace.diffSet
diffStore(destination, ...keys): Promise<number>
Computes the set difference between keys (like diff) and stores the result
in destination.
K
Key to store the result.
...K[]
Keys of sets to compute difference for.
Promise<number>
The size of the resulting set.
https://redis.io/commands/sdiffstore/
SetKeyspace.diffStore
intersect(...keys): Promise<number[]>
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.
...K[]
Keys of sets to compute intersection for. At least one must be provided.
Promise<number[]>
Members common to all sets.
If no keys are provided.
https://redis.io/commands/sinter/
SetKeyspace.intersect
intersectSet(...keys): Promise<Set<number>>
Identical to intersect except it returns the values as a Set.
...K[]
Promise<Set<number>>
https://redis.io/commands/sinter/
SetKeyspace.intersectSet
intersectStore(destination, ...keys): Promise<number>
Computes the set intersection between keys (like intersect) and stores the result
in destination.
K
Key to store the result.
...K[]
Keys of sets to compute intersection for.
Promise<number>
The size of the resulting set.
https://redis.io/commands/sinterstore/
SetKeyspace.intersectStore
items(key): Promise<number[]>
Returns the elements in the set stored at key.
If the key does not exist it returns an empty array.
K
Promise<number[]>
All members of the set.
https://redis.io/commands/smembers/
SetKeyspace.items
itemsSet(key): Promise<Set<number>>
Identical to items except it returns the values as a Set.
If the key does not exist it returns an empty Set.
K
Promise<Set<number>>
All members of the set as a Set.
https://redis.io/commands/smembers/
SetKeyspace.itemsSet
len(key): Promise<number>
Returns the number of elements in the set stored at key.
If the key does not exist it returns 0.
K
Promise<number>
The set cardinality.
https://redis.io/commands/scard/
SetKeyspace.len
protected mapKey(key): string
Maps a key to its Redis key string.
K
string
SetKeyspace.mapKey
move(
src,
dst,
member,
options?): Promise<boolean>;
Atomically moves the given member from the set stored at src
to the set stored at dst.
If the element already exists in dst it is still removed from src.
K
Source set key.
K
Destination set key.
number
The member to move.
Promise<boolean>
true if the member was moved, false if not found in src.
https://redis.io/commands/smove/
SetKeyspace.move
pop(
key,
count,
options?): Promise<number[]>;
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 array.
K
The cache key.
number
Number of members to pop.
Promise<number[]>
The removed members (may be fewer than count if the set is small).
https://redis.io/commands/spop/
SetKeyspace.pop
popOne(key, options?): Promise<number | undefined>
Removes a random element from the set stored at key and returns it.
K
Promise<number | undefined>
The removed member, or undefined if the set is empty.
https://redis.io/commands/spop/
SetKeyspace.popOne
remove(key, ...members): Promise<number>
Removes one or more values from the set stored at key. Values not present in the set are ignored. If the key does not already exist, it is a no-op.
K
...number[]
Promise<number>
The number of values that were removed from the set.
https://redis.io/commands/srem/
SetKeyspace.remove
protected resolveTtl(options?): number | undefined
Resolves the TTL for a write operation. Returns i64 sentinel for NAPI: undefined=no config, -1=KeepTTL, -2=Persist/NeverExpire, >=0=ms
number | undefined
SetKeyspace.resolveTtl
sample(key, count): Promise<number[]>
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 array.
K
The cache key.
number
Number of distinct members to return.
Promise<number[]>
Random members (may be fewer than count if the set is small).
https://redis.io/commands/srandmember/
SetKeyspace.sample
sampleOne(key): Promise<number | undefined>
Returns a random member from the set stored at key without removing it.
K
Promise<number | undefined>
A random member, or undefined if the key does not exist.
https://redis.io/commands/srandmember/
SetKeyspace.sampleOne
sampleWithReplacement(key, count): Promise<number[]>
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 array.
K
The cache key.
number
Number of members to return (may include duplicates).
Promise<number[]>
Random members, possibly with duplicates.
https://redis.io/commands/srandmember/
SetKeyspace.sampleWithReplacement
protected serializeItem(value): Buffer
number
Buffer
SetKeyspace.serializeItem
union(...keys): Promise<number[]>
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.
...K[]
Keys of sets to compute union for. At least one must be provided.
Promise<number[]>
Members in any of the provided sets.
If no keys are provided.
https://redis.io/commands/sunion/
SetKeyspace.union
unionSet(...keys): Promise<Set<number>>
Identical to union except it returns the values as a Set.
...K[]
Promise<Set<number>>
https://redis.io/commands/sunion/
SetKeyspace.unionSet
unionStore(destination, ...keys): Promise<number>
Computes the set union between sets (like union) and stores the result
in destination.
K
Key to store the result.
...K[]
Keys of sets to compute union for.
Promise<number>
The size of the resulting set.
https://redis.io/commands/sunionstore/
SetKeyspace.unionStore
with(options): this
Returns a shallow clone of this keyspace with the specified write options applied. This allows setting expiry for a chain of operations.
this
await myKeyspace.with({ expiry: expireIn(5000) }).set(key, value)
SetKeyspace.with
StringKeyspace stores string values.
const tokens = new StringKeyspace<string>(cluster, {
keyPattern: "token/:id",
defaultExpiry: ExpireIn(3600000), // 1 hour
});
await tokens.set("abc123", "user-token-value");
const token = await tokens.get("abc123");
BasicKeyspace<K, string>K
new StringKeyspace<K>(cluster, config): StringKeyspace<K>
BasicKeyspace<K, string>.constructor
protected readonly cluster: CacheCluster
protected readonly config: KeyspaceConfig<K>
BasicKeyspace.config
protected readonly keyMapper: (key) => string
K
string
BasicKeyspace.keyMapper
append(
key,
value,
options?): Promise<number>;
Appends a string to the value stored at key.
If the key does not exist it is first created and set as the empty string, causing append to behave like set.
K
string
Promise<number>
The new string length.
https://redis.io/commands/append/
delete(...keys): Promise<number>
Deletes the specified keys. If a key does not exist it is ignored.
...K[]
Promise<number>
The number of keys that were deleted.
https://redis.io/commands/del/
BasicKeyspace.delete
protected deserialize(data): string
Deserializes a Buffer from storage to a value.
Buffer
string
BasicKeyspace.deserialize
get(key): Promise<string | undefined>
Gets the value stored at key.
If the key does not exist, it returns undefined.
K
Promise<string | undefined>
The value, or undefined if the key does not exist.
https://redis.io/commands/get/
BasicKeyspace.get
getAndDelete(key): Promise<string | undefined>
Deletes the key and returns the previously stored value.
If the key does not already exist, it returns undefined.
K
Promise<string | undefined>
The previous value, or undefined if the key did not exist.
https://redis.io/commands/getdel/
BasicKeyspace.getAndDelete
getAndSet(
key,
value,
options?): Promise<string | undefined>;
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 undefined.
K
string
Promise<string | undefined>
The previous value, or undefined if the key did not exist.
https://redis.io/commands/getset/
BasicKeyspace.getAndSet
getRange(
key,
start,
end): Promise<string>;
Returns a substring of the string value stored at key.
The start and end values are zero-based indices, but unlike typical slicing
the end 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.
If the string does not exist it returns the empty string.
K
The cache key.
number
Start index (inclusive, 0-based).
number
End index (inclusive, 0-based). Use -1 for end of string.
Promise<string>
The substring.
https://redis.io/commands/getrange/
len(key): Promise<number>
Returns the length of the string value stored at key.
Non-existing keys are considered as empty strings.
K
Promise<number>
The string length.
https://redis.io/commands/strlen/
protected mapKey(key): string
Maps a key to its Redis key string.
K
string
BasicKeyspace.mapKey
multiGet(...keys): Promise<(string | undefined)[]>
Gets the values stored at multiple keys.
...K[]
Promise<(string | undefined)[]>
An array of values in the same order as the provided keys.
Each element is the value or undefined if the key was not found.
https://redis.io/commands/mget/
BasicKeyspace.multiGet
replace(
key,
value,
options?): Promise<void>;
Replaces the existing value stored at key to val.
K
string
Promise<void>
If the key does not already exist.
https://redis.io/commands/set/
BasicKeyspace.replace
protected resolveTtl(options?): number | undefined
Resolves the TTL for a write operation. Returns i64 sentinel for NAPI: undefined=no config, -1=KeepTTL, -2=Persist/NeverExpire, >=0=ms
number | undefined
BasicKeyspace.resolveTtl
protected serialize(value): Buffer
Serializes a value to a Buffer for storage.
string
Buffer
BasicKeyspace.serialize
set(
key,
value,
options?): Promise<void>;
Updates the value stored at key to val.
K
string
Promise<void>
https://redis.io/commands/set/
BasicKeyspace.set
setIfNotExists(
key,
value,
options?): Promise<void>;
Sets the value stored at key to val, but only if the key does not exist beforehand.
K
string
Promise<void>
If the key already exists.
https://redis.io/commands/setnx/
BasicKeyspace.setIfNotExists
setRange(
key,
offset,
value,
options?): Promise<number>;
Overwrites part of the string stored at key, starting at
the zero-based offset and for the entire length of value, extending
the string if necessary.
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.
K
The cache key.
number
Zero-based byte offset to start writing at.
string
The string to write.
Promise<number>
The length of the string after the operation.
https://redis.io/commands/setrange/
with(options): this
Returns a shallow clone of this keyspace with the specified write options applied. This allows setting expiry for a chain of operations.
this
await myKeyspace.with({ expiry: expireIn(5000) }).set(key, value)
BasicKeyspace.with
StringListKeyspace stores lists of string values.
const recentViews = new StringListKeyspace<string>(cluster, {
keyPattern: "recent-views/:userId",
defaultExpiry: ExpireIn(86400000), // 24 hours
});
await recentViews.pushLeft("user1", "product-123", "product-456");
const views = await recentViews.items("user1");
ListKeyspace<K, string>K
new StringListKeyspace<K>(cluster, config): StringListKeyspace<K>
ListKeyspace<K, string>.constructor
protected readonly cluster: CacheCluster
ListKeyspace.cluster
protected readonly config: KeyspaceConfig<K>
ListKeyspace.config
protected readonly keyMapper: (key) => string
K
string
ListKeyspace.keyMapper
delete(...keys): Promise<number>
Deletes the specified keys. If a key does not exist it is ignored.
...K[]
Promise<number>
The number of keys that were deleted.
https://redis.io/commands/del/
ListKeyspace.delete
protected deserializeItem(data): string
Buffer
string
ListKeyspace.deserializeItem
get(key, index): Promise<string | undefined>
Returns the value of the list element at the given index.
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.
K
The cache key.
number
Zero-based index of the element to retrieve.
Promise<string | undefined>
The value at the index, or undefined if out of range or the key does not exist.
https://redis.io/commands/lindex/
ListKeyspace.get
getRange(
key,
start,
stop): Promise<string[]>;
Returns the elements in the list stored at key between start and stop (inclusive).
Both 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.
If the key does not exist it returns an empty array.
K
The cache key.
number
Start index (inclusive).
number
Stop index (inclusive).
Promise<string[]>
The elements in the specified range.
https://redis.io/commands/lrange/
ListKeyspace.getRange
insertAfter(
key,
pivot,
value,
options?): Promise<number>;
Inserts value into the list stored at key, at the position just after pivot.
If the list does not contain pivot, the value is not inserted and -1 is returned.
K
The cache key.
string
The existing element to insert after.
string
The value to insert.
Promise<number>
The new list length, or -1 if pivot was not found.
https://redis.io/commands/linsert/
ListKeyspace.insertAfter
insertBefore(
key,
pivot,
value,
options?): Promise<number>;
Inserts value into the list stored at key, at the position just before pivot.
If the list does not contain pivot, the value is not inserted and -1 is returned.
K
The cache key.
string
The existing element to insert before.
string
The value to insert.
Promise<number>
The new list length, or -1 if pivot was not found.
https://redis.io/commands/linsert/
ListKeyspace.insertBefore
items(key): Promise<string[]>
Returns all the elements in the list stored at key.
If the key does not exist it returns an empty array.
K
Promise<string[]>
All elements in the list.
https://redis.io/commands/lrange/
ListKeyspace.items
len(key): Promise<number>
Returns the length of the list stored at key.
Non-existing keys are considered as empty lists.
K
Promise<number>
The list length.
https://redis.io/commands/llen/
ListKeyspace.len
protected mapKey(key): string
Maps a key to its Redis key string.
K
string
ListKeyspace.mapKey
move(
src,
dst,
srcPos,
dstPos,
options?): Promise<string | undefined>;
Atomically moves an element from the list stored at src to the list stored at dst.
The value moved can be either the head (srcPos === "left") or tail (srcPos === "right")
of the list at src. Similarly, the value can be placed either at the head (dstPos === "left")
or tail (dstPos === "right") of the list at dst.
If src and dst are the same list, the value is atomically rotated from one end to the other
when srcPos !== dstPos, or if srcPos === dstPos nothing happens.
K
Source list key.
K
Destination list key.
Position to pop from in the source list.
Position to push to in the destination list.
Promise<string | undefined>
The moved element, or undefined if the source list does not exist.
https://redis.io/commands/lmove/
ListKeyspace.move
popLeft(key, options?): Promise<string | undefined>
Pops a single element off the head of the list stored at key.
K
Promise<string | undefined>
The popped value, or undefined if the key does not exist.
https://redis.io/commands/lpop/
ListKeyspace.popLeft
popRight(key, options?): Promise<string | undefined>
Pops a single element off the tail of the list stored at key.
K
Promise<string | undefined>
The popped value, or undefined if the key does not exist.
https://redis.io/commands/rpop/
ListKeyspace.popRight
pushLeft(key, ...values): Promise<number>
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(key, "a", "b", "c") will result in a list containing
"c" as its first element, "b" as its second, and "a" as its third.
K
...string[]
Promise<number>
The length of the list after the operation.
https://redis.io/commands/lpush/
ListKeyspace.pushLeft
pushRight(key, ...values): Promise<number>
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(key, "a", "b", "c") will result in a list containing
"a" as its first element, "b" as its second, and "c" as its third.
K
...string[]
Promise<number>
The length of the list after the operation.
https://redis.io/commands/rpush/
ListKeyspace.pushRight
removeAll(
key,
value,
options?): Promise<number>;
Removes all occurrences of value in the list stored at key.
If the list does not contain value, or the list does not exist, returns 0.
K
The cache key.
string
The value to remove.
Promise<number>
The number of elements removed.
https://redis.io/commands/lrem/
ListKeyspace.removeAll
removeFirst(
key,
count,
value,
options?): Promise<number>;
Removes the first count occurrences of value in the list stored at key,
scanning from head to tail.
If the list does not contain value, or the list does not exist, returns 0.
K
The cache key.
number
Maximum number of occurrences to remove.
string
The value to remove.
Promise<number>
The number of elements removed.
https://redis.io/commands/lrem/
ListKeyspace.removeFirst
removeLast(
key,
count,
value,
options?): Promise<number>;
Removes the last count occurrences of value in the list stored at key,
scanning from tail to head.
If the list does not contain value, or the list does not exist, returns 0.
K
The cache key.
number
Maximum number of occurrences to remove.
string
The value to remove.
Promise<number>
The number of elements removed.
https://redis.io/commands/lrem/
ListKeyspace.removeLast
protected resolveTtl(options?): number | undefined
Resolves the TTL for a write operation. Returns i64 sentinel for NAPI: undefined=no config, -1=KeepTTL, -2=Persist/NeverExpire, >=0=ms
number | undefined
ListKeyspace.resolveTtl
protected serializeItem(value): Buffer
string
Buffer
ListKeyspace.serializeItem
set(
key,
index,
value,
options?): Promise<void>;
Updates the list element at the given index.
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.
K
The cache key.
number
Zero-based index of the element to update.
string
The new value.
Promise<void>
If the index is out of range.
https://redis.io/commands/lset/
ListKeyspace.set
trim(
key,
start,
stop,
options?): Promise<void>;
Trims the list stored at key to only contain the elements between the indices
start and stop (inclusive). Both 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.
K
The cache key.
number
Start index (inclusive).
number
Stop index (inclusive).
Promise<void>
https://redis.io/commands/ltrim/
ListKeyspace.trim
with(options): this
Returns a shallow clone of this keyspace with the specified write options applied. This allows setting expiry for a chain of operations.
this
await myKeyspace.with({ expiry: expireIn(5000) }).set(key, value)
ListKeyspace.with
StringSetKeyspace stores sets of unique string values.
const tags = new StringSetKeyspace<string>(cluster, {
keyPattern: "tags/:articleId",
});
await tags.add("article1", "typescript", "programming", "web");
const hasTech = await tags.contains("article1", "typescript");
const allTags = await tags.items("article1");
const tagSet = await tags.itemsSet("article1");
SetKeyspace<K, string>K
new StringSetKeyspace<K>(cluster, config): StringSetKeyspace<K>
SetKeyspace<K, string>.constructor
protected readonly cluster: CacheCluster
SetKeyspace.cluster
protected readonly config: KeyspaceConfig<K>
SetKeyspace.config
protected readonly keyMapper: (key) => string
K
string
SetKeyspace.keyMapper
add(key, ...members): Promise<number>
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.
K
...string[]
Promise<number>
The number of values that were added to the set, not including values already present beforehand.
https://redis.io/commands/sadd/
SetKeyspace.add
contains(key, member): Promise<boolean>
Reports whether the set stored at key contains the given value.
If the key does not exist it returns false.
K
string
Promise<boolean>
true if the member exists in the set, false otherwise.
https://redis.io/commands/sismember/
SetKeyspace.contains
delete(...keys): Promise<number>
Deletes the specified keys. If a key does not exist it is ignored.
...K[]
Promise<number>
The number of keys that were deleted.
https://redis.io/commands/del/
SetKeyspace.delete
protected deserializeItem(data): string
Buffer
string
SetKeyspace.deserializeItem
diff(...keys): Promise<string[]>
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.
...K[]
Keys of sets to compute difference for. At least one must be provided.
Promise<string[]>
Members in the first set but not in any of the other sets.
If no keys are provided.
https://redis.io/commands/sdiff/
SetKeyspace.diff
diffSet(...keys): Promise<Set<string>>
Identical to diff except it returns the values as a Set.
...K[]
Promise<Set<string>>
https://redis.io/commands/sdiff/
SetKeyspace.diffSet
diffStore(destination, ...keys): Promise<number>
Computes the set difference between keys (like diff) and stores the result
in destination.
K
Key to store the result.
...K[]
Keys of sets to compute difference for.
Promise<number>
The size of the resulting set.
https://redis.io/commands/sdiffstore/
SetKeyspace.diffStore
intersect(...keys): Promise<string[]>
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.
...K[]
Keys of sets to compute intersection for. At least one must be provided.
Promise<string[]>
Members common to all sets.
If no keys are provided.
https://redis.io/commands/sinter/
SetKeyspace.intersect
intersectSet(...keys): Promise<Set<string>>
Identical to intersect except it returns the values as a Set.
...K[]
Promise<Set<string>>
https://redis.io/commands/sinter/
SetKeyspace.intersectSet
intersectStore(destination, ...keys): Promise<number>
Computes the set intersection between keys (like intersect) and stores the result
in destination.
K
Key to store the result.
...K[]
Keys of sets to compute intersection for.
Promise<number>
The size of the resulting set.
https://redis.io/commands/sinterstore/
SetKeyspace.intersectStore
items(key): Promise<string[]>
Returns the elements in the set stored at key.
If the key does not exist it returns an empty array.
K
Promise<string[]>
All members of the set.
https://redis.io/commands/smembers/
SetKeyspace.items
itemsSet(key): Promise<Set<string>>
Identical to items except it returns the values as a Set.
If the key does not exist it returns an empty Set.
K
Promise<Set<string>>
All members of the set as a Set.
https://redis.io/commands/smembers/
SetKeyspace.itemsSet
len(key): Promise<number>
Returns the number of elements in the set stored at key.
If the key does not exist it returns 0.
K
Promise<number>
The set cardinality.
https://redis.io/commands/scard/
SetKeyspace.len
protected mapKey(key): string
Maps a key to its Redis key string.
K
string
SetKeyspace.mapKey
move(
src,
dst,
member,
options?): Promise<boolean>;
Atomically moves the given member from the set stored at src
to the set stored at dst.
If the element already exists in dst it is still removed from src.
K
Source set key.
K
Destination set key.
string
The member to move.
Promise<boolean>
true if the member was moved, false if not found in src.
https://redis.io/commands/smove/
SetKeyspace.move
pop(
key,
count,
options?): Promise<string[]>;
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 array.
K
The cache key.
number
Number of members to pop.
Promise<string[]>
The removed members (may be fewer than count if the set is small).
https://redis.io/commands/spop/
SetKeyspace.pop
popOne(key, options?): Promise<string | undefined>
Removes a random element from the set stored at key and returns it.
K
Promise<string | undefined>
The removed member, or undefined if the set is empty.
https://redis.io/commands/spop/
SetKeyspace.popOne
remove(key, ...members): Promise<number>
Removes one or more values from the set stored at key. Values not present in the set are ignored. If the key does not already exist, it is a no-op.
K
...string[]
Promise<number>
The number of values that were removed from the set.
https://redis.io/commands/srem/
SetKeyspace.remove
protected resolveTtl(options?): number | undefined
Resolves the TTL for a write operation. Returns i64 sentinel for NAPI: undefined=no config, -1=KeepTTL, -2=Persist/NeverExpire, >=0=ms
number | undefined
SetKeyspace.resolveTtl
sample(key, count): Promise<string[]>
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 array.
K
The cache key.
number
Number of distinct members to return.
Promise<string[]>
Random members (may be fewer than count if the set is small).
https://redis.io/commands/srandmember/
SetKeyspace.sample
sampleOne(key): Promise<string | undefined>
Returns a random member from the set stored at key without removing it.
K
Promise<string | undefined>
A random member, or undefined if the key does not exist.
https://redis.io/commands/srandmember/
SetKeyspace.sampleOne
sampleWithReplacement(key, count): Promise<string[]>
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 array.
K
The cache key.
number
Number of members to return (may include duplicates).
Promise<string[]>
Random members, possibly with duplicates.
https://redis.io/commands/srandmember/
SetKeyspace.sampleWithReplacement
protected serializeItem(value): Buffer
string
Buffer
SetKeyspace.serializeItem
union(...keys): Promise<string[]>
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.
...K[]
Keys of sets to compute union for. At least one must be provided.
Promise<string[]>
Members in any of the provided sets.
If no keys are provided.
https://redis.io/commands/sunion/
SetKeyspace.union
unionSet(...keys): Promise<Set<string>>
Identical to union except it returns the values as a Set.
...K[]
Promise<Set<string>>
https://redis.io/commands/sunion/
SetKeyspace.unionSet
unionStore(destination, ...keys): Promise<number>
Computes the set union between sets (like union) and stores the result
in destination.
K
Key to store the result.
...K[]
Keys of sets to compute union for.
Promise<number>
The size of the resulting set.
https://redis.io/commands/sunionstore/
SetKeyspace.unionStore
with(options): this
Returns a shallow clone of this keyspace with the specified write options applied. This allows setting expiry for a chain of operations.
this
await myKeyspace.with({ expiry: expireIn(5000) }).set(key, value)
SetKeyspace.with
StructKeyspace stores arbitrary objects serialized as JSON.
interface User {
id: string;
name: string;
email: string;
}
const users = new StructKeyspace<string, User>(cluster, {
keyPattern: "user/:id",
defaultExpiry: ExpireIn(3600000),
});
await users.set("user1", { id: "user1", name: "Alice", email: "[email protected]" });
const user = await users.get("user1");
BasicKeyspace<K, V>K
V
new StructKeyspace<K, V>(cluster, config): StructKeyspace<K, V>
StructKeyspace<K, V>
BasicKeyspace<K, V>.constructor
protected readonly cluster: CacheCluster
BasicKeyspace.cluster
protected readonly config: KeyspaceConfig<K>
BasicKeyspace.config
protected readonly keyMapper: (key) => string
K
string
BasicKeyspace.keyMapper
delete(...keys): Promise<number>
Deletes the specified keys. If a key does not exist it is ignored.
...K[]
Promise<number>
The number of keys that were deleted.
https://redis.io/commands/del/
BasicKeyspace.delete
protected deserialize(data): V
Deserializes a Buffer from storage to a value.
Buffer
V
BasicKeyspace.deserialize
get(key): Promise<V | undefined>
Gets the value stored at key.
If the key does not exist, it returns undefined.
K
Promise<V | undefined>
The value, or undefined if the key does not exist.
https://redis.io/commands/get/
BasicKeyspace.get
getAndDelete(key): Promise<V | undefined>
Deletes the key and returns the previously stored value.
If the key does not already exist, it returns undefined.
K
Promise<V | undefined>
The previous value, or undefined if the key did not exist.
https://redis.io/commands/getdel/
BasicKeyspace.getAndDelete
getAndSet(
key,
value,
options?): Promise<V | undefined>;
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 undefined.
K
V
Promise<V | undefined>
The previous value, or undefined if the key did not exist.
https://redis.io/commands/getset/
BasicKeyspace.getAndSet
protected mapKey(key): string
Maps a key to its Redis key string.
K
string
BasicKeyspace.mapKey
multiGet(...keys): Promise<(V | undefined)[]>
Gets the values stored at multiple keys.
...K[]
Promise<(V | undefined)[]>
An array of values in the same order as the provided keys.
Each element is the value or undefined if the key was not found.
https://redis.io/commands/mget/
BasicKeyspace.multiGet
replace(
key,
value,
options?): Promise<void>;
Replaces the existing value stored at key to val.
K
V
Promise<void>
If the key does not already exist.
https://redis.io/commands/set/
BasicKeyspace.replace
protected resolveTtl(options?): number | undefined
Resolves the TTL for a write operation. Returns i64 sentinel for NAPI: undefined=no config, -1=KeepTTL, -2=Persist/NeverExpire, >=0=ms
number | undefined
BasicKeyspace.resolveTtl
protected serialize(value): Buffer
Serializes a value to a Buffer for storage.
V
Buffer
BasicKeyspace.serialize
set(
key,
value,
options?): Promise<void>;
Updates the value stored at key to val.
K
V
Promise<void>
https://redis.io/commands/set/
BasicKeyspace.set
setIfNotExists(
key,
value,
options?): Promise<void>;
Sets the value stored at key to val, but only if the key does not exist beforehand.
K
V
Promise<void>
If the key already exists.
https://redis.io/commands/setnx/
BasicKeyspace.setIfNotExists
with(options): this
Returns a shallow clone of this keyspace with the specified write options applied. This allows setting expiry for a chain of operations.
this
await myKeyspace.with({ expiry: expireIn(5000) }).set(key, value)
BasicKeyspace.with
Configuration options for a cache cluster.
optional evictionPolicy?: EvictionPolicy
The eviction policy to use when the cache is full. Defaults to "allkeys-lru".
Configuration for a cache keyspace.
K
optional defaultExpiry?: Expiry
Default expiry for cache entries in this keyspace. If not set, entries do not expire.
keyPattern: string
The pattern for generating cache keys.
Use :fieldName to include a field from the key type.
// For a simple key type (string, number)
keyPattern: "user/:id"
// For a struct key type
keyPattern: "user/:userId/region/:region"
Options for write operations.
optional expiry?: Expiry
Expiry for this specific write operation. Overrides the keyspace's defaultExpiry.
<!-- symbol-end -->type EvictionPolicy =
| "noeviction"
| "allkeys-lru"
| "allkeys-lfu"
| "allkeys-random"
| "volatile-lru"
| "volatile-lfu"
| "volatile-ttl"
| "volatile-random";
Redis eviction policy that determines how keys are evicted when memory is full.
type Expiry =
| {
durationMs: number;
type: "duration";
}
| {
hours: number;
minutes: number;
seconds: number;
type: "time";
}
| "never"
| "keep-ttl";
Expiry represents a cache key expiration configuration. Use the helper functions to create expiry configurations.
type ListPosition = "left" | "right"
Position in a list (left/head or right/tail).
<!-- symbol-end -->const keepTTL: Expiry = "keep-ttl"
keepTTL preserves the existing TTL when updating a cache entry. If the key doesn't exist, no TTL is set.
const neverExpire: Expiry = "never"
neverExpire sets the cache entry to never expire. Note: Redis may still evict the key based on the eviction policy.
<!-- symbol-end -->function expireDailyAt(
hours,
minutes,
seconds): Expiry;
expireDailyAt sets the cache entry to expire at a specific time each day (UTC).
number
Hour (0-23)
number
Minutes (0-59)
number
Seconds (0-59)
function expireIn(ms): Expiry
expireIn sets the cache entry to expire after the specified duration.
number
Duration in milliseconds
function expireInHours(hours): Expiry
expireInHours sets the cache entry to expire after the specified hours.
number
Duration in hours
function expireInMinutes(minutes): Expiry
expireInMinutes sets the cache entry to expire after the specified minutes.
number
Duration in minutes
function expireInSeconds(seconds): Expiry
expireInSeconds sets the cache entry to expire after the specified seconds.
number
Duration in seconds