Creates a new Redis client
Symbol
RedisClient.constructor
URL to connect to, defaults to process.env.VALKEY_URL, process.env.REDIS_URL, or "valkey://localhost:6379"
Additional options
const valkey = new RedisClient();
await valkey.set("hello", "world");
console.log(await valkey.get("hello"));
Referenced types
interface RedisOptions
- tls?: boolean | { ca: string | Buffer<ArrayBufferLike> | string | Buffer<ArrayBufferLike>[]; cert: string | Buffer<ArrayBufferLike>; key: string | Buffer<ArrayBufferLike>; rejectUnauthorized: boolean }
TLS options Can be a boolean or an object with TLS options
- url?: string
URL to connect to, defaults to "redis://localhost:6379" Supported protocols: redis://, rediss://, redis+unix://, redis+tls://
class RedisClient
- onclose: null | (this: RedisClient, error: Error) => void
Callback fired when the client disconnects from the Redis server
- onconnect: null | (this: RedisClient) => void
Callback fired when the client connects to the Redis server
- append(key: string | Blob | ArrayBufferView<ArrayBufferLike>, value: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<number>
Append a value to a key
@param keyThe key to append to
@param valueThe value to append
@returnsPromise that resolves with the length of the string after the append operation
Count the number of set bits (population counting) in a string
@param keyThe key to count bits in
@returnsPromise that resolves with the number of bits set to 1
Disconnect from the Redis server
Decrement the integer value of a key by one
@param keyThe key to decrement
@returnsPromise that resolves with the new value
Delete a key
@param keyThe key to delete
@returnsPromise that resolves with the number of keys removed
Return a serialized version of the value stored at the specified key
@param keyThe key to dump
@returnsPromise that resolves with the serialized value, or null if the key doesn't exist
Determine if a key exists
@param keyThe key to check
@returnsPromise that resolves with true if the key exists, false otherwise
Set a key's time to live in seconds
@param keyThe key to set the expiration for
@param secondsThe number of seconds until expiration
@returnsPromise that resolves with 1 if the timeout was set, 0 if not
Get the expiration time of a key as a UNIX timestamp in seconds
@param keyThe key to check
@returnsPromise that resolves with the timestamp, or -1 if the key has no expiration, or -2 if the key doesn't exist
Get the value of a key
@param keyThe key to get
@returnsPromise that resolves with the key's value, or null if the key doesn't exist
Get the value of a key and delete the key
@param keyThe key to get and delete
@returnsPromise that resolves with the value of the key, or null if the key doesn't exist
Get the value of a key and optionally set its expiration
@param keyThe key to get
@returnsPromise that resolves with the value of the key, or null if the key doesn't exist
- getset(key: string | Blob | ArrayBufferView<ArrayBufferLike>, value: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<null | string>
Set the value of a key and return its old value
@param keyThe key to set
@param valueThe value to set
@returnsPromise that resolves with the old value, or null if the key didn't exist
- hgetall(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<null | Record<string, string>>
Get all the fields and values in a hash
@param keyThe hash key
@returnsPromise that resolves with an object containing all fields and values
- hincrby(key: string | Blob | ArrayBufferView<ArrayBufferLike>, field: string, increment: string | number): Promise<number>
Increment the integer value of a hash field by the given number
@param keyThe hash key
@param fieldThe field to increment
@param incrementThe amount to increment by
@returnsPromise that resolves with the new value
- hincrbyfloat(key: string | Blob | ArrayBufferView<ArrayBufferLike>, field: string, increment: string | number): Promise<string>
Increment the float value of a hash field by the given amount
@param keyThe hash key
@param fieldThe field to increment
@param incrementThe amount to increment by
@returnsPromise that resolves with the new value as a string
Get all field names in a hash
@param keyThe hash key
@returnsPromise that resolves with an array of field names
Get the number of fields in a hash
@param keyThe hash key
@returnsPromise that resolves with the number of fields
- hmget(key: string | Blob | ArrayBufferView<ArrayBufferLike>, fields: string[]): Promise<null | string[]>
Get the values of all the given hash fields
@param keyThe hash key
@param fieldsThe fields to get
@returnsPromise that resolves with an array of values
- hmset(key: string | Blob | ArrayBufferView<ArrayBufferLike>, fieldValues: string[]): Promise<string>
Set multiple hash fields to multiple values
@param keyThe hash key
@param fieldValuesAn array of alternating field names and values
@returnsPromise that resolves with "OK" on success
Get all values in a hash
@param keyThe hash key
@returnsPromise that resolves with an array of values
Increment the integer value of a key by one
@param keyThe key to increment
@returnsPromise that resolves with the new value
Find all keys matching the given pattern
@param patternThe pattern to match
@returnsPromise that resolves with an array of matching keys
Get the length of a list
@param keyThe list key
@returnsPromise that resolves with the length of the list
Remove and get the first element in a list
@param keyThe list key
@returnsPromise that resolves with the first element, or null if the list is empty
- lpush(key: string | Blob | ArrayBufferView<ArrayBufferLike>, value: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<number>
Prepend one or multiple values to a list
@param keyThe list key
@param valueThe value to prepend
@returnsPromise that resolves with the length of the list after the push operation
- lpushx(key: string | Blob | ArrayBufferView<ArrayBufferLike>, value: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<number>
Prepend a value to a list, only if the list exists
@param keyThe list key
@param valueThe value to prepend
@returnsPromise that resolves with the length of the list after the push operation, or 0 if the list doesn't exist
Get the values of all specified keys
@param keysThe keys to get
@returnsPromise that resolves with an array of values, with null for keys that don't exist
Remove the expiration from a key
@param keyThe key to persist
@returnsPromise that resolves with 1 if the timeout was removed, 0 if the key doesn't exist or has no timeout
Get the expiration time of a key as a UNIX timestamp in milliseconds
@param keyThe key to check
@returnsPromise that resolves with the timestamp, or -1 if the key has no expiration, or -2 if the key doesn't exist
Add one or more members to a HyperLogLog
@param keyThe HyperLogLog key
@param elementThe element to add
@returnsPromise that resolves with 1 if the HyperLogLog was altered, 0 otherwise
Get the time to live for a key in milliseconds
@param keyThe key to check
@returnsPromise that resolves with the TTL in milliseconds, or -1 if the key has no expiration, or -2 if the key doesn't exist
Remove and get the last element in a list
@param keyThe list key
@returnsPromise that resolves with the last element, or null if the list is empty
- rpush(key: string | Blob | ArrayBufferView<ArrayBufferLike>, value: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<number>
Append one or multiple values to a list
@param keyThe list key
@param valueThe value to append
@returnsPromise that resolves with the length of the list after the push operation
- rpushx(key: string | Blob | ArrayBufferView<ArrayBufferLike>, value: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<number>
Append a value to a list, only if the list exists
@param keyThe list key
@param valueThe value to append
@returnsPromise that resolves with the length of the list after the push operation, or 0 if the list doesn't exist
Add a member to a set
@param keyThe set key
@param memberThe member to add
@returnsPromise that resolves with 1 if the member was added, 0 if it already existed
Get the number of members in a set
@param keyThe set key
@returnsPromise that resolves with the cardinality (number of elements) of the set
Send a raw command to the Redis server
@param commandThe command to send
@param argsThe arguments to the command
@returnsA promise that resolves with the command result
- set(key: string | Blob | ArrayBufferView<ArrayBufferLike>, value: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<'OK'>
Set key to hold the string value
@param keyThe key to set
@param valueThe value to set
@returnsPromise that resolves with "OK" on success
set(key: string | Blob | ArrayBufferView<ArrayBufferLike>, value: string | Blob | ArrayBufferView<ArrayBufferLike>, ex: 'EX', seconds: number): Promise<'OK'>Set key to hold the string value with expiration
@param keyThe key to set
@param valueThe value to set
@param exSet the specified expire time, in seconds
@returnsPromise that resolves with "OK" on success
set(key: string | Blob | ArrayBufferView<ArrayBufferLike>, value: string | Blob | ArrayBufferView<ArrayBufferLike>, px: 'PX', milliseconds: number): Promise<'OK'>Set key to hold the string value with expiration
@param keyThe key to set
@param valueThe value to set
@param pxSet the specified expire time, in milliseconds
@returnsPromise that resolves with "OK" on success
set(key: string | Blob | ArrayBufferView<ArrayBufferLike>, value: string | Blob | ArrayBufferView<ArrayBufferLike>, exat: 'EXAT', timestampSeconds: number): Promise<'OK'>Set key to hold the string value with expiration at a specific Unix timestamp
@param keyThe key to set
@param valueThe value to set
@param exatSet the specified Unix time at which the key will expire, in seconds
@returnsPromise that resolves with "OK" on success
set(key: string | Blob | ArrayBufferView<ArrayBufferLike>, value: string | Blob | ArrayBufferView<ArrayBufferLike>, pxat: 'PXAT', timestampMilliseconds: number): Promise<'OK'>Set key to hold the string value with expiration at a specific Unix timestamp
@param keyThe key to set
@param valueThe value to set
@param pxatSet the specified Unix time at which the key will expire, in milliseconds
@returnsPromise that resolves with "OK" on success
set(key: string | Blob | ArrayBufferView<ArrayBufferLike>, value: string | Blob | ArrayBufferView<ArrayBufferLike>, nx: 'NX'): Promise<null | 'OK'>Set key to hold the string value only if key does not exist
@param keyThe key to set
@param valueThe value to set
@param nxOnly set the key if it does not already exist
@returnsPromise that resolves with "OK" on success, or null if the key already exists
set(key: string | Blob | ArrayBufferView<ArrayBufferLike>, value: string | Blob | ArrayBufferView<ArrayBufferLike>, xx: 'XX'): Promise<null | 'OK'>Set key to hold the string value only if key already exists
@param keyThe key to set
@param valueThe value to set
@param xxOnly set the key if it already exists
@returnsPromise that resolves with "OK" on success, or null if the key does not exist
set(key: string | Blob | ArrayBufferView<ArrayBufferLike>, value: string | Blob | ArrayBufferView<ArrayBufferLike>, get: 'GET'): Promise<null | string>Set key to hold the string value and return the old value
@param keyThe key to set
@param valueThe value to set
@param getReturn the old string stored at key, or null if key did not exist
@returnsPromise that resolves with the old value, or null if key did not exist
set(key: string | Blob | ArrayBufferView<ArrayBufferLike>, value: string | Blob | ArrayBufferView<ArrayBufferLike>, keepttl: 'KEEPTTL'): Promise<'OK'>Set key to hold the string value and retain the time to live
@param keyThe key to set
@param valueThe value to set
@param keepttlRetain the time to live associated with the key
@returnsPromise that resolves with "OK" on success
set(key: string | Blob | ArrayBufferView<ArrayBufferLike>, value: string | Blob | ArrayBufferView<ArrayBufferLike>, ...options: string[]): Promise<null | string>Set key to hold the string value with various options
@param keyThe key to set
@param valueThe value to set
@param optionsArray of options (EX, PX, EXAT, PXAT, NX, XX, KEEPTTL, GET)
@returnsPromise that resolves with "OK" on success, null if NX/XX condition not met, or the old value if GET is specified
- setnx(key: string | Blob | ArrayBufferView<ArrayBufferLike>, value: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<number>
Set the value of a key, only if the key does not exist
@param keyThe key to set
@param valueThe value to set
@returnsPromise that resolves with 1 if the key was set, 0 if the key was not set
Check if a value is a member of a set
@param keyThe set key
@param memberThe member to check
@returnsPromise that resolves with true if the member exists, false otherwise
Get all the members in a set
@param keyThe set key
@returnsPromise that resolves with an array of all members
Remove and return a random member from a set
@param keyThe set key
@returnsPromise that resolves with the removed member, or null if the set is empty
Get a random member from a set
@param keyThe set key
@returnsPromise that resolves with a random member, or null if the set is empty
Remove a member from a set
@param keyThe set key
@param memberThe member to remove
@returnsPromise that resolves with 1 if the member was removed, 0 if it didn't exist
Get the length of the value stored in a key
@param keyThe key to check
@returnsPromise that resolves with the length of the string value, or 0 if the key doesn't exist
Get the time to live for a key in seconds
@param keyThe key to get the TTL for
@returnsPromise that resolves with the TTL, -1 if no expiry, or -2 if key doesn't exist
Get the number of members in a sorted set
@param keyThe sorted set key
@returnsPromise that resolves with the cardinality (number of elements) of the sorted set
Remove and return members with the highest scores in a sorted set
@param keyThe sorted set key
@returnsPromise that resolves with the removed member and its score, or null if the set is empty
Remove and return members with the lowest scores in a sorted set
@param keyThe sorted set key
@returnsPromise that resolves with the removed member and its score, or null if the set is empty
Get one or multiple random members from a sorted set
@param keyThe sorted set key
@returnsPromise that resolves with a random member, or null if the set is empty
- zscore(key: string | Blob | ArrayBufferView<ArrayBufferLike>, member: string): Promise<null | string>
Get the score associated with the given member in a sorted set
@param keyThe sorted set key
@param memberThe member to get the score for
@returnsPromise that resolves with the score of the member as a string, or null if the member or key doesn't exist