encore.dev/storage/cache
Classes
CacheCluster
CacheCluster represents a Redis cache cluster.
Create a new cluster using new CacheCluster(name).
Reference an existing cluster using CacheCluster.named(name).
Example
import { CacheCluster } from "encore.dev/storage/cache";
const myCache = new CacheCluster("my-cache", {
evictionPolicy: "allkeys-lru",
});
Constructors
Constructor
new CacheCluster(name, cfg?): CacheCluster
Creates a new cache cluster with the given name and configuration.
Parameters
name
string
The unique name for this cache cluster
cfg?
Optional configuration for the cluster
Returns
Methods
named()
static named<Name>(name): CacheCluster
Reference an existing cache cluster by name.
To create a new cache cluster, use new CacheCluster(...) instead.
Type Parameters
Name
Name extends string
Parameters
name
StringLiteral\<Name>
Returns
CacheError
CacheError is the base class for all cache-related errors.
Extends
Error
Extended by
Constructors
Constructor
new CacheError(msg): CacheError
Parameters
msg
string
Returns
Overrides
Error.constructor
CacheKeyExists
CacheKeyExists is thrown when attempting to set a key that already exists using setIfNotExists.
Extends
Constructors
Constructor
new CacheKeyExists(key): CacheKeyExists
Parameters
key
string
Returns
Overrides
CacheMiss
CacheMiss is thrown when a cache key is not found.
Extends
Constructors
Constructor
new CacheMiss(key): CacheMiss
Parameters
key
string
Returns
Overrides
FloatKeyspace
FloatKeyspace stores 64-bit floating point values.
Example
const scores = new FloatKeyspace<string>(cluster, {
keyPattern: "score/:playerId",
});
await scores.set("player1", 100.5);
const newScore = await scores.increment("player1", 10.25);
Extends
BasicKeyspace\<K,number>
Type Parameters
K
K
Constructors
Constructor
new FloatKeyspace<K>(cluster, config): FloatKeyspace<K>
Parameters
cluster
config
KeyspaceConfig\<K>
Returns
FloatKeyspace\<K>
Overrides
BasicKeyspace<K, number>.constructor
Properties
cluster
protected readonly cluster: CacheCluster
Inherited from
BasicKeyspace.cluster
config
protected readonly config: KeyspaceConfig<K>
Inherited from
BasicKeyspace.config
keyMapper
protected readonly keyMapper: (key) => string
Parameters
key
K
Returns
string
Inherited from
BasicKeyspace.keyMapper
Methods
decrement()
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.
Parameters
key
K
The cache key.
delta?
number = 1
The amount to decrement by (default 1).
options?
Returns
Promise\<number>
The new value after decrementing.
See
https://redis.io/commands/incrbyfloat/
delete()
delete(...keys): Promise<number>
Deletes the specified keys. If a key does not exist it is ignored.
Parameters
keys
...K[]
Returns
Promise\<number>
The number of keys that were deleted.
See
https://redis.io/commands/del/
Inherited from
BasicKeyspace.delete
deserialize()
protected deserialize(data): number
Deserializes a Buffer from storage to a value.
Parameters
data
Buffer
Returns
number
Overrides
BasicKeyspace.deserialize
get()
get(key): Promise<number | undefined>
Gets the value stored at key.
If the key does not exist, it returns undefined.
Parameters
key
K
Returns
Promise\<number | undefined>
The value, or undefined if the key does not exist.
See
https://redis.io/commands/get/
Inherited from
BasicKeyspace.get
getAndDelete()
getAndDelete(key): Promise<number | undefined>
Deletes the key and returns the previously stored value.
If the key does not already exist, it returns undefined.
Parameters
key
K
Returns
Promise\<number | undefined>
The previous value, or undefined if the key did not exist.
See
https://redis.io/commands/getdel/
Inherited from
BasicKeyspace.getAndDelete
getAndSet()
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.
Parameters
key
K
value
number
options?
Returns
Promise\<number | undefined>
The previous value, or undefined if the key did not exist.
See
https://redis.io/commands/getset/
Inherited from
BasicKeyspace.getAndSet
increment()
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.
Parameters
key
K
The cache key.
delta?
number = 1
The amount to increment by (default 1).
options?
Returns
Promise\<number>
The new value after incrementing.
See
https://redis.io/commands/incrbyfloat/
mapKey()
protected mapKey(key): string
Maps a key to its Redis key string.
Parameters
key
K
Returns
string
Inherited from
BasicKeyspace.mapKey
multiGet()
multiGet(...keys): Promise<(number | undefined)[]>
Gets the values stored at multiple keys.
Parameters
keys
...K[]
Returns
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.
See
https://redis.io/commands/mget/
Inherited from
BasicKeyspace.multiGet
replace()
replace(
key,
value,
options?): Promise<void>;
Replaces the existing value stored at key to val.
Parameters
key
K
value
number
options?
Returns
Promise\<void>
Throws
If the key does not already exist.
See
https://redis.io/commands/set/
Inherited from
BasicKeyspace.replace
resolveTtl()
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
Parameters
options?
Returns
number | undefined
Inherited from
BasicKeyspace.resolveTtl
serialize()
protected serialize(value): Buffer
Serializes a value to a Buffer for storage.
Parameters
value
number
Returns
Buffer
Overrides
BasicKeyspace.serialize
set()
set(
key,
value,
options?): Promise<void>;
Updates the value stored at key to val.
Parameters
key
K
value
number
options?
Returns
Promise\<void>
See
https://redis.io/commands/set/
Inherited from
BasicKeyspace.set
setIfNotExists()
setIfNotExists(
key,
value,
options?): Promise<void>;
Sets the value stored at key to val, but only if the key does not exist beforehand.
Parameters
key
K
value
number
options?
Returns
Promise\<void>
Throws
If the key already exists.
See
https://redis.io/commands/setnx/
Inherited from
BasicKeyspace.setIfNotExists
with()
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.
Parameters
options
Returns
this
Example
await myKeyspace.with({ expiry: expireIn(5000) }).set(key, value)
Inherited from
BasicKeyspace.with
IntKeyspace
IntKeyspace stores 64-bit integer values.
Values are floored to integers using Math.floor.
For fractional values, use FloatKeyspace instead.
Example
const counters = new IntKeyspace<string>(cluster, {
keyPattern: "counter/:name",
});
await counters.set("page-views", 0);
const newCount = await counters.increment("page-views", 1);
Extends
BasicKeyspace\<K,number>
Type Parameters
K
K
Constructors
Constructor
new IntKeyspace<K>(cluster, config): IntKeyspace<K>
Parameters
cluster
config
KeyspaceConfig\<K>
Returns
IntKeyspace\<K>
Overrides
BasicKeyspace<K, number>.constructor
Properties
cluster
protected readonly cluster: CacheCluster
Inherited from
BasicKeyspace.cluster
config
protected readonly config: KeyspaceConfig<K>
Inherited from
BasicKeyspace.config
keyMapper
protected readonly keyMapper: (key) => string
Parameters
key
K
Returns
string
Inherited from
BasicKeyspace.keyMapper
Methods
decrement()
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.
Parameters
key
K
The cache key.
delta?
number = 1
The amount to decrement by (default 1).
options?
Returns
Promise\<number>
The new value after decrementing.
See
https://redis.io/commands/decrby/
delete()
delete(...keys): Promise<number>
Deletes the specified keys. If a key does not exist it is ignored.
Parameters
keys
...K[]
Returns
Promise\<number>
The number of keys that were deleted.
See
https://redis.io/commands/del/
Inherited from
BasicKeyspace.delete
deserialize()
protected deserialize(data): number
Deserializes a Buffer from storage to a value.
Parameters
data
Buffer
Returns
number
Overrides
BasicKeyspace.deserialize
get()
get(key): Promise<number | undefined>
Gets the value stored at key.
If the key does not exist, it returns undefined.
Parameters
key
K
Returns
Promise\<number | undefined>
The value, or undefined if the key does not exist.
See
https://redis.io/commands/get/
Inherited from
BasicKeyspace.get
getAndDelete()
getAndDelete(key): Promise<number | undefined>
Deletes the key and returns the previously stored value.
If the key does not already exist, it returns undefined.
Parameters
key
K
Returns
Promise\<number | undefined>
The previous value, or undefined if the key did not exist.
See
https://redis.io/commands/getdel/
Inherited from
BasicKeyspace.getAndDelete
getAndSet()
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.
Parameters
key
K
value
number
options?
Returns
Promise\<number | undefined>
The previous value, or undefined if the key did not exist.
See
https://redis.io/commands/getset/
Inherited from
BasicKeyspace.getAndSet
increment()
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.
Parameters
key
K
The cache key.
delta?
number = 1
The amount to increment by (default 1).
options?
Returns
Promise\<number>
The new value after incrementing.
See
https://redis.io/commands/incrby/
mapKey()
protected mapKey(key): string
Maps a key to its Redis key string.
Parameters
key
K
Returns
string
Inherited from
BasicKeyspace.mapKey
multiGet()
multiGet(...keys): Promise<(number | undefined)[]>
Gets the values stored at multiple keys.
Parameters
keys
...K[]
Returns
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.
See
https://redis.io/commands/mget/
Inherited from
BasicKeyspace.multiGet
replace()
replace(
key,
value,
options?): Promise<void>;
Replaces the existing value stored at key to val.
Parameters
key
K
value
number
options?
Returns
Promise\<void>
Throws
If the key does not already exist.
See
https://redis.io/commands/set/
Inherited from
BasicKeyspace.replace
resolveTtl()
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
Parameters
options?
Returns
number | undefined
Inherited from
BasicKeyspace.resolveTtl
serialize()
protected serialize(value): Buffer
Serializes a value to a Buffer for storage.
Parameters
value
number
Returns
Buffer
Overrides
BasicKeyspace.serialize
set()
set(
key,
value,
options?): Promise<void>;
Updates the value stored at key to val.
Parameters
key
K
value
number
options?
Returns
Promise\<void>
See
https://redis.io/commands/set/
Inherited from
BasicKeyspace.set
setIfNotExists()
setIfNotExists(
key,
value,
options?): Promise<void>;
Sets the value stored at key to val, but only if the key does not exist beforehand.
Parameters
key
K
value
number
options?
Returns
Promise\<void>
Throws
If the key already exists.
See
https://redis.io/commands/setnx/
Inherited from
BasicKeyspace.setIfNotExists
with()
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.
Parameters
options
Returns
this
Example
await myKeyspace.with({ expiry: expireIn(5000) }).set(key, value)
Inherited from
BasicKeyspace.with
NumberListKeyspace
NumberListKeyspace stores lists of numeric values.
Example
const scores = new NumberListKeyspace<string>(cluster, {
keyPattern: "scores/:gameId",
});
await scores.pushRight("game1", 100, 200, 300);
const allScores = await scores.items("game1");
Extends
ListKeyspace\<K,number>
Type Parameters
K
K
Constructors
Constructor
new NumberListKeyspace<K>(cluster, config): NumberListKeyspace<K>
Parameters
cluster
config
KeyspaceConfig\<K>
Returns
Overrides
ListKeyspace<K, number>.constructor
Properties
cluster
protected readonly cluster: CacheCluster
Inherited from
ListKeyspace.cluster
config
protected readonly config: KeyspaceConfig<K>
Inherited from
ListKeyspace.config
keyMapper
protected readonly keyMapper: (key) => string
Parameters
key
K
Returns
string
Inherited from
ListKeyspace.keyMapper
Methods
delete()
delete(...keys): Promise<number>
Deletes the specified keys. If a key does not exist it is ignored.
Parameters
keys
...K[]
Returns
Promise\<number>
The number of keys that were deleted.
See
https://redis.io/commands/del/
Inherited from
ListKeyspace.delete
deserializeItem()
protected deserializeItem(data): number
Parameters
data
Buffer
Returns
number
Overrides
ListKeyspace.deserializeItem
get()
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.
Parameters
key
K
The cache key.
index
number
Zero-based index of the element to retrieve.
Returns
Promise\<number | undefined>
The value at the index, or undefined if out of range or the key does not exist.
See
https://redis.io/commands/lindex/
Inherited from
ListKeyspace.get
getRange()
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.
Parameters
key
K
The cache key.
start
number
Start index (inclusive).
stop
number
Stop index (inclusive).
Returns
Promise\<number[]>
The elements in the specified range.
See
https://redis.io/commands/lrange/
Inherited from
ListKeyspace.getRange
insertAfter()
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.
Parameters
key
K
The cache key.
pivot
number
The existing element to insert after.
value
number
The value to insert.
options?
Returns
Promise\<number>
The new list length, or -1 if pivot was not found.
See
https://redis.io/commands/linsert/
Inherited from
ListKeyspace.insertAfter
insertBefore()
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.
Parameters
key
K
The cache key.
pivot
number
The existing element to insert before.
value
number
The value to insert.
options?
Returns
Promise\<number>
The new list length, or -1 if pivot was not found.
See
https://redis.io/commands/linsert/
Inherited from
ListKeyspace.insertBefore
items()
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.
Parameters
key
K
Returns
Promise\<number[]>
All elements in the list.
See
https://redis.io/commands/lrange/
Inherited from
ListKeyspace.items
len()
len(key): Promise<number>
Returns the length of the list stored at key.
Non-existing keys are considered as empty lists.
Parameters
key
K
Returns
Promise\<number>
The list length.
See
https://redis.io/commands/llen/
Inherited from
ListKeyspace.len
mapKey()
protected mapKey(key): string
Maps a key to its Redis key string.
Parameters
key
K
Returns
string
Inherited from
ListKeyspace.mapKey
move()
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.
Parameters
src
K
Source list key.
dst
K
Destination list key.
srcPos
Position to pop from in the source list.
dstPos
Position to push to in the destination list.
options?
Returns
Promise\<number | undefined>
The moved element, or undefined if the source list does not exist.
See
https://redis.io/commands/lmove/
Inherited from
ListKeyspace.move
popLeft()
popLeft(key, options?): Promise<number | undefined>
Pops a single element off the head of the list stored at key.
Parameters
key
K
options?
Returns
Promise\<number | undefined>
The popped value, or undefined if the key does not exist.
See
https://redis.io/commands/lpop/
Inherited from
ListKeyspace.popLeft
popRight()
popRight(key, options?): Promise<number | undefined>
Pops a single element off the tail of the list stored at key.
Parameters
key
K
options?
Returns
Promise\<number | undefined>
The popped value, or undefined if the key does not exist.
See
https://redis.io/commands/rpop/
Inherited from
ListKeyspace.popRight
pushLeft()
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.
Parameters
key
K
values
...number[]
Returns
Promise\<number>
The length of the list after the operation.
See
https://redis.io/commands/lpush/
Inherited from
ListKeyspace.pushLeft
pushRight()
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.
Parameters
key
K
values
...number[]
Returns
Promise\<number>
The length of the list after the operation.
See
https://redis.io/commands/rpush/
Inherited from
ListKeyspace.pushRight
removeAll()
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.
Parameters
key
K
The cache key.
value
number
The value to remove.
options?
Returns
Promise\<number>
The number of elements removed.
See
https://redis.io/commands/lrem/
Inherited from
ListKeyspace.removeAll
removeFirst()
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.
Parameters
key
K
The cache key.
count
number
Maximum number of occurrences to remove.
value
number
The value to remove.
options?
Returns
Promise\<number>
The number of elements removed.
See
https://redis.io/commands/lrem/
Inherited from
ListKeyspace.removeFirst
removeLast()
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.
Parameters
key
K
The cache key.
count
number
Maximum number of occurrences to remove.
value
number
The value to remove.
options?
Returns
Promise\<number>
The number of elements removed.
See
https://redis.io/commands/lrem/
Inherited from
ListKeyspace.removeLast
resolveTtl()
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
Parameters
options?
Returns
number | undefined
Inherited from
ListKeyspace.resolveTtl
serializeItem()
protected serializeItem(value): Buffer
Parameters
value
number
Returns
Buffer
Overrides
ListKeyspace.serializeItem
set()
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.
Parameters
key
K
The cache key.
index
number
Zero-based index of the element to update.
value
number
The new value.
options?
Returns
Promise\<void>
Throws
If the index is out of range.
See
https://redis.io/commands/lset/
Inherited from
ListKeyspace.set
trim()
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.
Parameters
key
K
The cache key.
start
number
Start index (inclusive).
stop
number
Stop index (inclusive).
options?
Returns
Promise\<void>
See
https://redis.io/commands/ltrim/
Inherited from
ListKeyspace.trim
with()
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.
Parameters
options
Returns
this
Example
await myKeyspace.with({ expiry: expireIn(5000) }).set(key, value)
Inherited from
ListKeyspace.with
NumberSetKeyspace
NumberSetKeyspace stores sets of unique numeric values.
Example
const scores = new NumberSetKeyspace<string>(cluster, {
keyPattern: "unique-scores/:gameId",
});
await scores.add("game1", 100, 200, 300);
const hasScore = await scores.contains("game1", 100);
Extends
SetKeyspace\<K,number>
Type Parameters
K
K
Constructors
Constructor
new NumberSetKeyspace<K>(cluster, config): NumberSetKeyspace<K>
Parameters
cluster
config
KeyspaceConfig\<K>
Returns
Overrides
SetKeyspace<K, number>.constructor
Properties
cluster
protected readonly cluster: CacheCluster
Inherited from
SetKeyspace.cluster
config
protected readonly config: KeyspaceConfig<K>
Inherited from
SetKeyspace.config
keyMapper
protected readonly keyMapper: (key) => string
Parameters
key
K
Returns
string
Inherited from
SetKeyspace.keyMapper
Methods
add()
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.
Parameters
key
K
members
...number[]
Returns
Promise\<number>
The number of values that were added to the set, not including values already present beforehand.
See
https://redis.io/commands/sadd/
Inherited from
SetKeyspace.add
contains()
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.
Parameters
key
K
member
number
Returns
Promise\<boolean>
true if the member exists in the set, false otherwise.
See
https://redis.io/commands/sismember/
Inherited from
SetKeyspace.contains
delete()
delete(...keys): Promise<number>
Deletes the specified keys. If a key does not exist it is ignored.
Parameters
keys
...K[]
Returns
Promise\<number>
The number of keys that were deleted.
See
https://redis.io/commands/del/
Inherited from
SetKeyspace.delete
deserializeItem()
protected deserializeItem(data): number
Parameters
data
Buffer
Returns
number
Overrides
SetKeyspace.deserializeItem
diff()
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.
Parameters
keys
...K[]
Keys of sets to compute difference for. At least one must be provided.
Returns
Promise\<number[]>
Members in the first set but not in any of the other sets.
Throws
If no keys are provided.
See
https://redis.io/commands/sdiff/
Inherited from
SetKeyspace.diff
diffSet()
diffSet(...keys): Promise<Set<number>>
Identical to diff except it returns the values as a Set.
Parameters
keys
...K[]
Returns
Promise\<Set\<number>>
See
https://redis.io/commands/sdiff/
Inherited from
SetKeyspace.diffSet
diffStore()
diffStore(destination, ...keys): Promise<number>
Computes the set difference between keys (like diff) and stores the result
in destination.
Parameters
destination
K
Key to store the result.
keys
...K[]
Keys of sets to compute difference for.
Returns
Promise\<number>
The size of the resulting set.
See
https://redis.io/commands/sdiffstore/
Inherited from
SetKeyspace.diffStore
intersect()
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.
Parameters
keys
...K[]
Keys of sets to compute intersection for. At least one must be provided.
Returns
Promise\<number[]>
Members common to all sets.
Throws
If no keys are provided.
See
https://redis.io/commands/sinter/
Inherited from
SetKeyspace.intersect
intersectSet()
intersectSet(...keys): Promise<Set<number>>
Identical to intersect except it returns the values as a Set.
Parameters
keys
...K[]
Returns
Promise\<Set\<number>>
See
https://redis.io/commands/sinter/
Inherited from
SetKeyspace.intersectSet
intersectStore()
intersectStore(destination, ...keys): Promise<number>
Computes the set intersection between keys (like intersect) and stores the result
in destination.
Parameters
destination
K
Key to store the result.
keys
...K[]
Keys of sets to compute intersection for.
Returns
Promise\<number>
The size of the resulting set.
See
https://redis.io/commands/sinterstore/
Inherited from
SetKeyspace.intersectStore
items()
items(key): Promise<number[]>
Returns the elements in the set stored at key.
If the key does not exist it returns an empty array.
Parameters
key
K
Returns
Promise\<number[]>
All members of the set.
See
https://redis.io/commands/smembers/
Inherited from
SetKeyspace.items
itemsSet()
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.
Parameters
key
K
Returns
Promise\<Set\<number>>
All members of the set as a Set.
See
https://redis.io/commands/smembers/
Inherited from
SetKeyspace.itemsSet
len()
len(key): Promise<number>
Returns the number of elements in the set stored at key.
If the key does not exist it returns 0.
Parameters
key
K
Returns
Promise\<number>
The set cardinality.
See
https://redis.io/commands/scard/
Inherited from
SetKeyspace.len
mapKey()
protected mapKey(key): string
Maps a key to its Redis key string.
Parameters
key
K
Returns
string
Inherited from
SetKeyspace.mapKey
move()
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.
Parameters
src
K
Source set key.
dst
K
Destination set key.
member
number
The member to move.
options?
Returns
Promise\<boolean>
true if the member was moved, false if not found in src.
See
https://redis.io/commands/smove/
Inherited from
SetKeyspace.move
pop()
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.
Parameters
key
K
The cache key.
count
number
Number of members to pop.
options?
Returns
Promise\<number[]>
The removed members (may be fewer than count if the set is small).
See
https://redis.io/commands/spop/
Inherited from
SetKeyspace.pop
popOne()
popOne(key, options?): Promise<number | undefined>
Removes a random element from the set stored at key and returns it.
Parameters
key
K
options?
Returns
Promise\<number | undefined>
The removed member, or undefined if the set is empty.
See
https://redis.io/commands/spop/
Inherited from
SetKeyspace.popOne
remove()
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.
Parameters
key
K
members
...number[]
Returns
Promise\<number>
The number of values that were removed from the set.
See
https://redis.io/commands/srem/
Inherited from
SetKeyspace.remove
resolveTtl()
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
Parameters
options?
Returns
number | undefined
Inherited from
SetKeyspace.resolveTtl
sample()
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.
Parameters
key
K
The cache key.
count
number
Number of distinct members to return.
Returns
Promise\<number[]>
Random members (may be fewer than count if the set is small).
See
https://redis.io/commands/srandmember/
Inherited from
SetKeyspace.sample
sampleOne()
sampleOne(key): Promise<number | undefined>
Returns a random member from the set stored at key without removing it.
Parameters
key
K
Returns
Promise\<number | undefined>
A random member, or undefined if the key does not exist.
See
https://redis.io/commands/srandmember/
Inherited from
SetKeyspace.sampleOne
sampleWithReplacement()
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.
Parameters
key
K
The cache key.
count
number
Number of members to return (may include duplicates).
Returns
Promise\<number[]>
Random members, possibly with duplicates.
See
https://redis.io/commands/srandmember/
Inherited from
SetKeyspace.sampleWithReplacement
serializeItem()
protected serializeItem(value): Buffer
Parameters
value
number
Returns
Buffer
Overrides
SetKeyspace.serializeItem
union()
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.
Parameters
keys
...K[]
Keys of sets to compute union for. At least one must be provided.
Returns
Promise\<number[]>
Members in any of the provided sets.
Throws
If no keys are provided.
See
https://redis.io/commands/sunion/
Inherited from
SetKeyspace.union
unionSet()
unionSet(...keys): Promise<Set<number>>
Identical to union except it returns the values as a Set.
Parameters
keys
...K[]
Returns
Promise\<Set\<number>>
See
https://redis.io/commands/sunion/
Inherited from
SetKeyspace.unionSet
unionStore()
unionStore(destination, ...keys): Promise<number>
Computes the set union between sets (like union) and stores the result
in destination.
Parameters
destination
K
Key to store the result.
keys
...K[]
Keys of sets to compute union for.
Returns
Promise\<number>
The size of the resulting set.
See
https://redis.io/commands/sunionstore/
Inherited from
SetKeyspace.unionStore
with()
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.
Parameters
options
Returns
this
Example
await myKeyspace.with({ expiry: expireIn(5000) }).set(key, value)
Inherited from
SetKeyspace.with
StringKeyspace
StringKeyspace stores string values.
Example
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");
Extends
BasicKeyspace\<K,string>
Type Parameters
K
K
Constructors
Constructor
new StringKeyspace<K>(cluster, config): StringKeyspace<K>
Parameters
cluster
config
KeyspaceConfig\<K>
Returns
StringKeyspace\<K>
Overrides
BasicKeyspace<K, string>.constructor
Properties
cluster
protected readonly cluster: CacheCluster
Inherited from
config
protected readonly config: KeyspaceConfig<K>
Inherited from
BasicKeyspace.config
keyMapper
protected readonly keyMapper: (key) => string
Parameters
key
K
Returns
string
Inherited from
BasicKeyspace.keyMapper
Methods
append()
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.
Parameters
key
K
value
string
options?
Returns
Promise\<number>
The new string length.
See
https://redis.io/commands/append/
delete()
delete(...keys): Promise<number>
Deletes the specified keys. If a key does not exist it is ignored.
Parameters
keys
...K[]
Returns
Promise\<number>
The number of keys that were deleted.
See
https://redis.io/commands/del/
Inherited from
BasicKeyspace.delete
deserialize()
protected deserialize(data): string
Deserializes a Buffer from storage to a value.
Parameters
data
Buffer
Returns
string
Overrides
BasicKeyspace.deserialize
get()
get(key): Promise<string | undefined>
Gets the value stored at key.
If the key does not exist, it returns undefined.
Parameters
key
K
Returns
Promise\<string | undefined>
The value, or undefined if the key does not exist.
See
https://redis.io/commands/get/
Inherited from
BasicKeyspace.get
getAndDelete()
getAndDelete(key): Promise<string | undefined>
Deletes the key and returns the previously stored value.
If the key does not already exist, it returns undefined.
Parameters
key
K
Returns
Promise\<string | undefined>
The previous value, or undefined if the key did not exist.
See
https://redis.io/commands/getdel/
Inherited from
BasicKeyspace.getAndDelete
getAndSet()
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.
Parameters
key
K
value
string
options?
Returns
Promise\<string | undefined>
The previous value, or undefined if the key did not exist.
See
https://redis.io/commands/getset/
Inherited from
BasicKeyspace.getAndSet
getRange()
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.
Parameters
key
K
The cache key.
start
number
Start index (inclusive, 0-based).
end
number
End index (inclusive, 0-based). Use -1 for end of string.
Returns
Promise\<string>
The substring.
See
https://redis.io/commands/getrange/
len()
len(key): Promise<number>
Returns the length of the string value stored at key.
Non-existing keys are considered as empty strings.
Parameters
key
K
Returns
Promise\<number>
The string length.
See
https://redis.io/commands/strlen/
mapKey()
protected mapKey(key): string
Maps a key to its Redis key string.
Parameters
key
K
Returns
string
Inherited from
BasicKeyspace.mapKey
multiGet()
multiGet(...keys): Promise<(string | undefined)[]>
Gets the values stored at multiple keys.
Parameters
keys
...K[]
Returns
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.
See
https://redis.io/commands/mget/
Inherited from
BasicKeyspace.multiGet
replace()
replace(
key,
value,
options?): Promise<void>;
Replaces the existing value stored at key to val.
Parameters
key
K
value
string
options?
Returns
Promise\<void>
Throws
If the key does not already exist.
See
https://redis.io/commands/set/
Inherited from
BasicKeyspace.replace
resolveTtl()
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
Parameters
options?
Returns
number | undefined
Inherited from
BasicKeyspace.resolveTtl
serialize()
protected serialize(value): Buffer
Serializes a value to a Buffer for storage.
Parameters
value
string
Returns
Buffer
Overrides
BasicKeyspace.serialize
set()
set(
key,
value,
options?): Promise<void>;
Updates the value stored at key to val.
Parameters
key
K
value
string
options?
Returns
Promise\<void>
See
https://redis.io/commands/set/
Inherited from
BasicKeyspace.set
setIfNotExists()
setIfNotExists(
key,
value,
options?): Promise<void>;
Sets the value stored at key to val, but only if the key does not exist beforehand.
Parameters
key
K
value
string
options?
Returns
Promise\<void>
Throws
If the key already exists.
See
https://redis.io/commands/setnx/
Inherited from
BasicKeyspace.setIfNotExists
setRange()
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.
Parameters
key
K
The cache key.
offset
number
Zero-based byte offset to start writing at.
value
string
The string to write.
options?
Returns
Promise\<number>
The length of the string after the operation.
See
https://redis.io/commands/setrange/
with()
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.
Parameters
options
Returns
this
Example
await myKeyspace.with({ expiry: expireIn(5000) }).set(key, value)
Inherited from
BasicKeyspace.with
StringListKeyspace
StringListKeyspace stores lists of string values.
Example
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");
Extends
ListKeyspace\<K,string>
Type Parameters
K
K
Constructors
Constructor
new StringListKeyspace<K>(cluster, config): StringListKeyspace<K>
Parameters
cluster
config
KeyspaceConfig\<K>
Returns
Overrides
ListKeyspace<K, string>.constructor
Properties
cluster
protected readonly cluster: CacheCluster
Inherited from
ListKeyspace.cluster
config
protected readonly config: KeyspaceConfig<K>
Inherited from
ListKeyspace.config
keyMapper
protected readonly keyMapper: (key) => string
Parameters
key
K
Returns
string
Inherited from
ListKeyspace.keyMapper
Methods
delete()
delete(...keys): Promise<number>
Deletes the specified keys. If a key does not exist it is ignored.
Parameters
keys
...K[]
Returns
Promise\<number>
The number of keys that were deleted.
See
https://redis.io/commands/del/
Inherited from
ListKeyspace.delete
deserializeItem()
protected deserializeItem(data): string
Parameters
data
Buffer
Returns
string
Overrides
ListKeyspace.deserializeItem
get()
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.
Parameters
key
K
The cache key.
index
number
Zero-based index of the element to retrieve.
Returns
Promise\<string | undefined>
The value at the index, or undefined if out of range or the key does not exist.
See
https://redis.io/commands/lindex/
Inherited from
ListKeyspace.get
getRange()
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.
Parameters
key
K
The cache key.
start
number
Start index (inclusive).
stop
number
Stop index (inclusive).
Returns
Promise\<string[]>
The elements in the specified range.
See
https://redis.io/commands/lrange/
Inherited from
ListKeyspace.getRange
insertAfter()
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.
Parameters
key
K
The cache key.
pivot
string
The existing element to insert after.
value
string
The value to insert.
options?
Returns
Promise\<number>
The new list length, or -1 if pivot was not found.
See
https://redis.io/commands/linsert/
Inherited from
ListKeyspace.insertAfter
insertBefore()
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.
Parameters
key
K
The cache key.
pivot
string
The existing element to insert before.
value
string
The value to insert.
options?
Returns
Promise\<number>
The new list length, or -1 if pivot was not found.
See
https://redis.io/commands/linsert/
Inherited from
ListKeyspace.insertBefore
items()
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.
Parameters
key
K
Returns
Promise\<string[]>
All elements in the list.
See
https://redis.io/commands/lrange/
Inherited from
ListKeyspace.items
len()
len(key): Promise<number>
Returns the length of the list stored at key.
Non-existing keys are considered as empty lists.
Parameters
key
K
Returns
Promise\<number>
The list length.
See
https://redis.io/commands/llen/
Inherited from
ListKeyspace.len
mapKey()
protected mapKey(key): string
Maps a key to its Redis key string.
Parameters
key
K
Returns
string
Inherited from
ListKeyspace.mapKey
move()
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.
Parameters
src
K
Source list key.
dst
K
Destination list key.
srcPos
Position to pop from in the source list.
dstPos
Position to push to in the destination list.
options?
Returns
Promise\<string | undefined>
The moved element, or undefined if the source list does not exist.
See
https://redis.io/commands/lmove/
Inherited from
ListKeyspace.move
popLeft()
popLeft(key, options?): Promise<string | undefined>
Pops a single element off the head of the list stored at key.
Parameters
key
K
options?
Returns
Promise\<string | undefined>
The popped value, or undefined if the key does not exist.
See
https://redis.io/commands/lpop/
Inherited from
ListKeyspace.popLeft
popRight()
popRight(key, options?): Promise<string | undefined>
Pops a single element off the tail of the list stored at key.
Parameters
key
K
options?
Returns
Promise\<string | undefined>
The popped value, or undefined if the key does not exist.
See
https://redis.io/commands/rpop/
Inherited from
ListKeyspace.popRight
pushLeft()
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.
Parameters
key
K
values
...string[]
Returns
Promise\<number>
The length of the list after the operation.
See
https://redis.io/commands/lpush/
Inherited from
ListKeyspace.pushLeft
pushRight()
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.
Parameters
key
K
values
...string[]
Returns
Promise\<number>
The length of the list after the operation.
See
https://redis.io/commands/rpush/
Inherited from
ListKeyspace.pushRight
removeAll()
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.
Parameters
key
K
The cache key.
value
string
The value to remove.
options?
Returns
Promise\<number>
The number of elements removed.
See
https://redis.io/commands/lrem/
Inherited from
ListKeyspace.removeAll
removeFirst()
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.
Parameters
key
K
The cache key.
count
number
Maximum number of occurrences to remove.
value
string
The value to remove.
options?
Returns
Promise\<number>
The number of elements removed.
See
https://redis.io/commands/lrem/
Inherited from
ListKeyspace.removeFirst
removeLast()
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.
Parameters
key
K
The cache key.
count
number
Maximum number of occurrences to remove.
value
string
The value to remove.
options?
Returns
Promise\<number>
The number of elements removed.
See
https://redis.io/commands/lrem/
Inherited from
ListKeyspace.removeLast
resolveTtl()
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
Parameters
options?
Returns
number | undefined
Inherited from
ListKeyspace.resolveTtl
serializeItem()
protected serializeItem(value): Buffer
Parameters
value
string
Returns
Buffer
Overrides
ListKeyspace.serializeItem
set()
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.
Parameters
key
K
The cache key.
index
number
Zero-based index of the element to update.
value
string
The new value.
options?
Returns
Promise\<void>
Throws
If the index is out of range.
See
https://redis.io/commands/lset/
Inherited from
ListKeyspace.set
trim()
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.
Parameters
key
K
The cache key.
start
number
Start index (inclusive).
stop
number
Stop index (inclusive).
options?
Returns
Promise\<void>
See
https://redis.io/commands/ltrim/
Inherited from
ListKeyspace.trim
with()
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.
Parameters
options
Returns
this
Example
await myKeyspace.with({ expiry: expireIn(5000) }).set(key, value)
Inherited from
ListKeyspace.with
StringSetKeyspace
StringSetKeyspace stores sets of unique string values.
Example
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");
Extends
SetKeyspace\<K,string>
Type Parameters
K
K
Constructors
Constructor
new StringSetKeyspace<K>(cluster, config): StringSetKeyspace<K>
Parameters
cluster
config
KeyspaceConfig\<K>
Returns
Overrides
SetKeyspace<K, string>.constructor
Properties
cluster
protected readonly cluster: CacheCluster
Inherited from
SetKeyspace.cluster
config
protected readonly config: KeyspaceConfig<K>
Inherited from
SetKeyspace.config
keyMapper
protected readonly keyMapper: (key) => string
Parameters
key
K
Returns
string
Inherited from
SetKeyspace.keyMapper
Methods
add()
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.
Parameters
key
K
members
...string[]
Returns
Promise\<number>
The number of values that were added to the set, not including values already present beforehand.
See
https://redis.io/commands/sadd/
Inherited from
SetKeyspace.add
contains()
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.
Parameters
key
K
member
string
Returns
Promise\<boolean>
true if the member exists in the set, false otherwise.
See
https://redis.io/commands/sismember/
Inherited from
SetKeyspace.contains
delete()
delete(...keys): Promise<number>
Deletes the specified keys. If a key does not exist it is ignored.
Parameters
keys
...K[]
Returns
Promise\<number>
The number of keys that were deleted.
See
https://redis.io/commands/del/
Inherited from
SetKeyspace.delete
deserializeItem()
protected deserializeItem(data): string
Parameters
data
Buffer
Returns
string
Overrides
SetKeyspace.deserializeItem
diff()
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.
Parameters
keys
...K[]
Keys of sets to compute difference for. At least one must be provided.
Returns
Promise\<string[]>
Members in the first set but not in any of the other sets.
Throws
If no keys are provided.
See
https://redis.io/commands/sdiff/
Inherited from
SetKeyspace.diff
diffSet()
diffSet(...keys): Promise<Set<string>>
Identical to diff except it returns the values as a Set.
Parameters
keys
...K[]
Returns
Promise\<Set\<string>>
See
https://redis.io/commands/sdiff/
Inherited from
SetKeyspace.diffSet
diffStore()
diffStore(destination, ...keys): Promise<number>
Computes the set difference between keys (like diff) and stores the result
in destination.
Parameters
destination
K
Key to store the result.
keys
...K[]
Keys of sets to compute difference for.
Returns
Promise\<number>
The size of the resulting set.
See
https://redis.io/commands/sdiffstore/
Inherited from
SetKeyspace.diffStore
intersect()
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.
Parameters
keys
...K[]
Keys of sets to compute intersection for. At least one must be provided.
Returns
Promise\<string[]>
Members common to all sets.
Throws
If no keys are provided.
See
https://redis.io/commands/sinter/
Inherited from
SetKeyspace.intersect
intersectSet()
intersectSet(...keys): Promise<Set<string>>
Identical to intersect except it returns the values as a Set.
Parameters
keys
...K[]
Returns
Promise\<Set\<string>>
See
https://redis.io/commands/sinter/
Inherited from
SetKeyspace.intersectSet
intersectStore()
intersectStore(destination, ...keys): Promise<number>
Computes the set intersection between keys (like intersect) and stores the result
in destination.
Parameters
destination
K
Key to store the result.
keys
...K[]
Keys of sets to compute intersection for.
Returns
Promise\<number>
The size of the resulting set.
See
https://redis.io/commands/sinterstore/
Inherited from
SetKeyspace.intersectStore
items()
items(key): Promise<string[]>
Returns the elements in the set stored at key.
If the key does not exist it returns an empty array.
Parameters
key
K
Returns
Promise\<string[]>
All members of the set.
See
https://redis.io/commands/smembers/
Inherited from
SetKeyspace.items
itemsSet()
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.
Parameters
key
K
Returns
Promise\<Set\<string>>
All members of the set as a Set.
See
https://redis.io/commands/smembers/
Inherited from
SetKeyspace.itemsSet
len()
len(key): Promise<number>
Returns the number of elements in the set stored at key.
If the key does not exist it returns 0.
Parameters
key
K
Returns
Promise\<number>
The set cardinality.
See
https://redis.io/commands/scard/
Inherited from
SetKeyspace.len
mapKey()
protected mapKey(key): string
Maps a key to its Redis key string.
Parameters
key
K
Returns
string
Inherited from
SetKeyspace.mapKey
move()
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.
Parameters
src
K
Source set key.
dst
K
Destination set key.
member
string
The member to move.
options?
Returns
Promise\<boolean>
true if the member was moved, false if not found in src.
See
https://redis.io/commands/smove/
Inherited from
SetKeyspace.move
pop()
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.
Parameters
key
K
The cache key.
count
number
Number of members to pop.
options?
Returns
Promise\<string[]>
The removed members (may be fewer than count if the set is small).
See
https://redis.io/commands/spop/
Inherited from
SetKeyspace.pop
popOne()
popOne(key, options?): Promise<string | undefined>
Removes a random element from the set stored at key and returns it.
Parameters
key
K
options?
Returns
Promise\<string | undefined>
The removed member, or undefined if the set is empty.
See
https://redis.io/commands/spop/
Inherited from
SetKeyspace.popOne
remove()
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.
Parameters
key
K
members
...string[]
Returns
Promise\<number>
The number of values that were removed from the set.
See
https://redis.io/commands/srem/
Inherited from
SetKeyspace.remove
resolveTtl()
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
Parameters
options?
Returns
number | undefined
Inherited from
SetKeyspace.resolveTtl
sample()
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.
Parameters
key
K
The cache key.
count
number
Number of distinct members to return.
Returns
Promise\<string[]>
Random members (may be fewer than count if the set is small).
See
https://redis.io/commands/srandmember/
Inherited from
SetKeyspace.sample
sampleOne()
sampleOne(key): Promise<string | undefined>
Returns a random member from the set stored at key without removing it.
Parameters
key
K
Returns
Promise\<string | undefined>
A random member, or undefined if the key does not exist.
See
https://redis.io/commands/srandmember/
Inherited from
SetKeyspace.sampleOne
sampleWithReplacement()
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.
Parameters
key
K
The cache key.
count
number
Number of members to return (may include duplicates).
Returns
Promise\<string[]>
Random members, possibly with duplicates.
See
https://redis.io/commands/srandmember/
Inherited from
SetKeyspace.sampleWithReplacement
serializeItem()
protected serializeItem(value): Buffer
Parameters
value
string
Returns
Buffer
Overrides
SetKeyspace.serializeItem
union()
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.
Parameters
keys
...K[]
Keys of sets to compute union for. At least one must be provided.
Returns
Promise\<string[]>
Members in any of the provided sets.
Throws
If no keys are provided.
See
https://redis.io/commands/sunion/
Inherited from
SetKeyspace.union
unionSet()
unionSet(...keys): Promise<Set<string>>
Identical to union except it returns the values as a Set.
Parameters
keys
...K[]
Returns
Promise\<Set\<string>>
See
https://redis.io/commands/sunion/
Inherited from
SetKeyspace.unionSet
unionStore()
unionStore(destination, ...keys): Promise<number>
Computes the set union between sets (like union) and stores the result
in destination.
Parameters
destination
K
Key to store the result.
keys
...K[]
Keys of sets to compute union for.
Returns
Promise\<number>
The size of the resulting set.
See
https://redis.io/commands/sunionstore/
Inherited from
SetKeyspace.unionStore
with()
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.
Parameters
options
Returns
this
Example
await myKeyspace.with({ expiry: expireIn(5000) }).set(key, value)
Inherited from
SetKeyspace.with
StructKeyspace
StructKeyspace stores arbitrary objects serialized as JSON.
Example
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");
Extends
BasicKeyspace\<K,V>
Type Parameters
K
K
V
V
Constructors
Constructor
new StructKeyspace<K, V>(cluster, config): StructKeyspace<K, V>
Parameters
cluster
config
KeyspaceConfig\<K>
Returns
StructKeyspace\<K, V>
Overrides
BasicKeyspace<K, V>.constructor
Properties
cluster
protected readonly cluster: CacheCluster
Inherited from
BasicKeyspace.cluster
config
protected readonly config: KeyspaceConfig<K>
Inherited from
BasicKeyspace.config
keyMapper
protected readonly keyMapper: (key) => string
Parameters
key
K
Returns
string
Inherited from
BasicKeyspace.keyMapper
Methods
delete()
delete(...keys): Promise<number>
Deletes the specified keys. If a key does not exist it is ignored.
Parameters
keys
...K[]
Returns
Promise\<number>
The number of keys that were deleted.
See
https://redis.io/commands/del/
Inherited from
BasicKeyspace.delete
deserialize()
protected deserialize(data): V
Deserializes a Buffer from storage to a value.
Parameters
data
Buffer
Returns
V
Overrides
BasicKeyspace.deserialize
get()
get(key): Promise<V | undefined>
Gets the value stored at key.
If the key does not exist, it returns undefined.
Parameters
key
K
Returns
Promise\<V | undefined>
The value, or undefined if the key does not exist.
See
https://redis.io/commands/get/
Inherited from
BasicKeyspace.get
getAndDelete()
getAndDelete(key): Promise<V | undefined>
Deletes the key and returns the previously stored value.
If the key does not already exist, it returns undefined.
Parameters
key
K
Returns
Promise\<V | undefined>
The previous value, or undefined if the key did not exist.
See
https://redis.io/commands/getdel/
Inherited from
BasicKeyspace.getAndDelete
getAndSet()
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.
Parameters
key
K
value
V
options?
Returns
Promise\<V | undefined>
The previous value, or undefined if the key did not exist.
See
https://redis.io/commands/getset/
Inherited from
BasicKeyspace.getAndSet
mapKey()
protected mapKey(key): string
Maps a key to its Redis key string.
Parameters
key
K
Returns
string
Inherited from
BasicKeyspace.mapKey
multiGet()
multiGet(...keys): Promise<(V | undefined)[]>
Gets the values stored at multiple keys.
Parameters
keys
...K[]
Returns
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.
See
https://redis.io/commands/mget/
Inherited from
BasicKeyspace.multiGet
replace()
replace(
key,
value,
options?): Promise<void>;
Replaces the existing value stored at key to val.
Parameters
key
K
value
V
options?
Returns
Promise\<void>
Throws
If the key does not already exist.
See
https://redis.io/commands/set/
Inherited from
BasicKeyspace.replace
resolveTtl()
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
Parameters
options?
Returns
number | undefined
Inherited from
BasicKeyspace.resolveTtl
serialize()
protected serialize(value): Buffer
Serializes a value to a Buffer for storage.
Parameters
value
V
Returns
Buffer
Overrides
BasicKeyspace.serialize
set()
set(
key,
value,
options?): Promise<void>;
Updates the value stored at key to val.
Parameters
key
K
value
V
options?
Returns
Promise\<void>
See
https://redis.io/commands/set/
Inherited from
BasicKeyspace.set
setIfNotExists()
setIfNotExists(
key,
value,
options?): Promise<void>;
Sets the value stored at key to val, but only if the key does not exist beforehand.
Parameters
key
K
value
V
options?
Returns
Promise\<void>
Throws
If the key already exists.
See
https://redis.io/commands/setnx/
Inherited from
BasicKeyspace.setIfNotExists
with()
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.
Parameters
options
Returns
this
Example
await myKeyspace.with({ expiry: expireIn(5000) }).set(key, value)
Inherited from
BasicKeyspace.with
Interfaces
CacheClusterConfig
Configuration options for a cache cluster.
Properties
evictionPolicy?
optional evictionPolicy?: EvictionPolicy
The eviction policy to use when the cache is full. Defaults to "allkeys-lru".
KeyspaceConfig
Configuration for a cache keyspace.
Type Parameters
K
K
Properties
defaultExpiry?
optional defaultExpiry?: Expiry
Default expiry for cache entries in this keyspace. If not set, entries do not expire.
keyPattern
keyPattern: string
The pattern for generating cache keys.
Use :fieldName to include a field from the key type.
Example
// For a simple key type (string, number)
keyPattern: "user/:id"
// For a struct key type
keyPattern: "user/:userId/region/:region"
WriteOptions
Options for write operations.
Properties
expiry?
optional expiry?: Expiry
Expiry for this specific write operation. Overrides the keyspace's defaultExpiry.
Type Aliases
EvictionPolicy
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.
Expiry
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.
ListPosition
type ListPosition = "left" | "right"
Position in a list (left/head or right/tail).
Variables
keepTTL
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.
neverExpire
const neverExpire: Expiry = "never"
neverExpire sets the cache entry to never expire. Note: Redis may still evict the key based on the eviction policy.
Functions
expireDailyAt()
function expireDailyAt(
hours,
minutes,
seconds): Expiry;
expireDailyAt sets the cache entry to expire at a specific time each day (UTC).
Parameters
hours
number
Hour (0-23)
minutes
number
Minutes (0-59)
seconds
number
Seconds (0-59)
Returns
expireIn()
function expireIn(ms): Expiry
expireIn sets the cache entry to expire after the specified duration.
Parameters
ms
number
Duration in milliseconds
Returns
expireInHours()
function expireInHours(hours): Expiry
expireInHours sets the cache entry to expire after the specified hours.
Parameters
hours
number
Duration in hours
Returns
expireInMinutes()
function expireInMinutes(minutes): Expiry
expireInMinutes sets the cache entry to expire after the specified minutes.
Parameters
minutes
number
Duration in minutes
Returns
expireInSeconds()
function expireInSeconds(seconds): Expiry
expireInSeconds sets the cache entry to expire after the specified seconds.
Parameters
seconds
number
Duration in seconds