Bun

Symbol

RedisClient.constructor

constructor (url?: string, options?: RedisOptions): RedisClient

Creates a new Redis client

@param url

URL to connect to, defaults to process.env.VALKEY_URL, process.env.REDIS_URL, or "valkey://localhost:6379"

@param options

Additional options

const valkey = new RedisClient();

await valkey.set("hello", "world");

console.log(await valkey.get("hello"));

Referenced types

interface RedisOptions

  • autoReconnect?: boolean

    Whether to automatically reconnect

  • connectionTimeout?: number

    Connection timeout in milliseconds

  • enableAutoPipelining?: boolean

    Whether to enable auto-pipelining

  • enableOfflineQueue?: boolean

    Whether to queue commands when disconnected

  • idleTimeout?: number

    Idle timeout in milliseconds

  • maxRetries?: number

    Maximum number of reconnection attempts

  • 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://

  • readonly bufferedAmount: number

    Amount of data buffered in bytes

  • readonly connected: boolean

    Whether the client is connected to the Redis server

  • 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 key

    The key to append to

    @param value

    The value to append

    @returns

    Promise that resolves with the length of the string after the append operation

  • bitcount(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<number>

    Count the number of set bits (population counting) in a string

    @param key

    The key to count bits in

    @returns

    Promise that resolves with the number of bits set to 1

  • close(): void

    Disconnect from the Redis server

  • connect(): Promise<void>

    Connect to the Redis server

    @returns

    A promise that resolves when connected

  • decr(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<number>

    Decrement the integer value of a key by one

    @param key

    The key to decrement

    @returns

    Promise that resolves with the new value

  • del(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<number>

    Delete a key

    @param key

    The key to delete

    @returns

    Promise that resolves with the number of keys removed

  • dump(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<null | string>

    Return a serialized version of the value stored at the specified key

    @param key

    The key to dump

    @returns

    Promise that resolves with the serialized value, or null if the key doesn't exist

  • exists(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<boolean>

    Determine if a key exists

    @param key

    The key to check

    @returns

    Promise that resolves with true if the key exists, false otherwise

  • expire(key: string | Blob | ArrayBufferView<ArrayBufferLike>, seconds: number): Promise<number>

    Set a key's time to live in seconds

    @param key

    The key to set the expiration for

    @param seconds

    The number of seconds until expiration

    @returns

    Promise that resolves with 1 if the timeout was set, 0 if not

  • expiretime(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<number>

    Get the expiration time of a key as a UNIX timestamp in seconds

    @param key

    The key to check

    @returns

    Promise that resolves with the timestamp, or -1 if the key has no expiration, or -2 if the key doesn't exist

  • get(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<null | string>

    Get the value of a key

    @param key

    The key to get

    @returns

    Promise that resolves with the key's value, or null if the key doesn't exist

  • getdel(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<null | string>

    Get the value of a key and delete the key

    @param key

    The key to get and delete

    @returns

    Promise that resolves with the value of the key, or null if the key doesn't exist

  • getex(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<null | string>

    Get the value of a key and optionally set its expiration

    @param key

    The key to get

    @returns

    Promise 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 key

    The key to set

    @param value

    The value to set

    @returns

    Promise 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 key

    The hash key

    @returns

    Promise 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 key

    The hash key

    @param field

    The field to increment

    @param increment

    The amount to increment by

    @returns

    Promise 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 key

    The hash key

    @param field

    The field to increment

    @param increment

    The amount to increment by

    @returns

    Promise that resolves with the new value as a string

  • hkeys(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<string[]>

    Get all field names in a hash

    @param key

    The hash key

    @returns

    Promise that resolves with an array of field names

  • hlen(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<number>

    Get the number of fields in a hash

    @param key

    The hash key

    @returns

    Promise 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 key

    The hash key

    @param fields

    The fields to get

    @returns

    Promise 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 key

    The hash key

    @param fieldValues

    An array of alternating field names and values

    @returns

    Promise that resolves with "OK" on success

  • hvals(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<string[]>

    Get all values in a hash

    @param key

    The hash key

    @returns

    Promise that resolves with an array of values

  • incr(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<number>

    Increment the integer value of a key by one

    @param key

    The key to increment

    @returns

    Promise that resolves with the new value

  • keys(pattern: string): Promise<string[]>

    Find all keys matching the given pattern

    @param pattern

    The pattern to match

    @returns

    Promise that resolves with an array of matching keys

  • llen(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<number>

    Get the length of a list

    @param key

    The list key

    @returns

    Promise that resolves with the length of the list

  • lpop(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<null | string>

    Remove and get the first element in a list

    @param key

    The list key

    @returns

    Promise 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 key

    The list key

    @param value

    The value to prepend

    @returns

    Promise 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 key

    The list key

    @param value

    The value to prepend

    @returns

    Promise that resolves with the length of the list after the push operation, or 0 if the list doesn't exist

  • mget(...keys: string | Blob | ArrayBufferView<ArrayBufferLike>[]): Promise<null | string[]>

    Get the values of all specified keys

    @param keys

    The keys to get

    @returns

    Promise that resolves with an array of values, with null for keys that don't exist

  • persist(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<number>

    Remove the expiration from a key

    @param key

    The key to persist

    @returns

    Promise that resolves with 1 if the timeout was removed, 0 if the key doesn't exist or has no timeout

  • pexpiretime(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<number>

    Get the expiration time of a key as a UNIX timestamp in milliseconds

    @param key

    The key to check

    @returns

    Promise that resolves with the timestamp, or -1 if the key has no expiration, or -2 if the key doesn't exist

  • pfadd(key: string | Blob | ArrayBufferView<ArrayBufferLike>, element: string): Promise<number>

    Add one or more members to a HyperLogLog

    @param key

    The HyperLogLog key

    @param element

    The element to add

    @returns

    Promise that resolves with 1 if the HyperLogLog was altered, 0 otherwise

  • pttl(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<number>

    Get the time to live for a key in milliseconds

    @param key

    The key to check

    @returns

    Promise that resolves with the TTL in milliseconds, or -1 if the key has no expiration, or -2 if the key doesn't exist

  • rpop(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<null | string>

    Remove and get the last element in a list

    @param key

    The list key

    @returns

    Promise 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 key

    The list key

    @param value

    The value to append

    @returns

    Promise 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 key

    The list key

    @param value

    The value to append

    @returns

    Promise that resolves with the length of the list after the push operation, or 0 if the list doesn't exist

  • sadd(key: string | Blob | ArrayBufferView<ArrayBufferLike>, member: string): Promise<number>

    Add a member to a set

    @param key

    The set key

    @param member

    The member to add

    @returns

    Promise that resolves with 1 if the member was added, 0 if it already existed

  • scard(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<number>

    Get the number of members in a set

    @param key

    The set key

    @returns

    Promise that resolves with the cardinality (number of elements) of the set

  • send(command: string, args: string[]): Promise<any>

    Send a raw command to the Redis server

    @param command

    The command to send

    @param args

    The arguments to the command

    @returns

    A 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 key

    The key to set

    @param value

    The value to set

    @returns

    Promise 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 key

    The key to set

    @param value

    The value to set

    @param ex

    Set the specified expire time, in seconds

    @returns

    Promise 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 key

    The key to set

    @param value

    The value to set

    @param px

    Set the specified expire time, in milliseconds

    @returns

    Promise 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 key

    The key to set

    @param value

    The value to set

    @param exat

    Set the specified Unix time at which the key will expire, in seconds

    @returns

    Promise 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 key

    The key to set

    @param value

    The value to set

    @param pxat

    Set the specified Unix time at which the key will expire, in milliseconds

    @returns

    Promise 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 key

    The key to set

    @param value

    The value to set

    @param nx

    Only set the key if it does not already exist

    @returns

    Promise 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 key

    The key to set

    @param value

    The value to set

    @param xx

    Only set the key if it already exists

    @returns

    Promise 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 key

    The key to set

    @param value

    The value to set

    @param get

    Return the old string stored at key, or null if key did not exist

    @returns

    Promise 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 key

    The key to set

    @param value

    The value to set

    @param keepttl

    Retain the time to live associated with the key

    @returns

    Promise 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 key

    The key to set

    @param value

    The value to set

    @param options

    Array of options (EX, PX, EXAT, PXAT, NX, XX, KEEPTTL, GET)

    @returns

    Promise 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 key

    The key to set

    @param value

    The value to set

    @returns

    Promise that resolves with 1 if the key was set, 0 if the key was not set

  • sismember(key: string | Blob | ArrayBufferView<ArrayBufferLike>, member: string): Promise<boolean>

    Check if a value is a member of a set

    @param key

    The set key

    @param member

    The member to check

    @returns

    Promise that resolves with true if the member exists, false otherwise

  • smembers(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<string[]>

    Get all the members in a set

    @param key

    The set key

    @returns

    Promise that resolves with an array of all members

  • spop(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<null | string>

    Remove and return a random member from a set

    @param key

    The set key

    @returns

    Promise that resolves with the removed member, or null if the set is empty

  • srandmember(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<null | string>

    Get a random member from a set

    @param key

    The set key

    @returns

    Promise that resolves with a random member, or null if the set is empty

  • srem(key: string | Blob | ArrayBufferView<ArrayBufferLike>, member: string): Promise<number>

    Remove a member from a set

    @param key

    The set key

    @param member

    The member to remove

    @returns

    Promise that resolves with 1 if the member was removed, 0 if it didn't exist

  • strlen(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<number>

    Get the length of the value stored in a key

    @param key

    The key to check

    @returns

    Promise that resolves with the length of the string value, or 0 if the key doesn't exist

  • ttl(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<number>

    Get the time to live for a key in seconds

    @param key

    The key to get the TTL for

    @returns

    Promise that resolves with the TTL, -1 if no expiry, or -2 if key doesn't exist

  • zcard(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<number>

    Get the number of members in a sorted set

    @param key

    The sorted set key

    @returns

    Promise that resolves with the cardinality (number of elements) of the sorted set

  • zpopmax(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<null | string>

    Remove and return members with the highest scores in a sorted set

    @param key

    The sorted set key

    @returns

    Promise that resolves with the removed member and its score, or null if the set is empty

  • zpopmin(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<null | string>

    Remove and return members with the lowest scores in a sorted set

    @param key

    The sorted set key

    @returns

    Promise that resolves with the removed member and its score, or null if the set is empty

  • zrandmember(key: string | Blob | ArrayBufferView<ArrayBufferLike>): Promise<null | string>

    Get one or multiple random members from a sorted set

    @param key

    The sorted set key

    @returns

    Promise 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 key

    The sorted set key

    @param member

    The member to get the score for

    @returns

    Promise that resolves with the score of the member as a string, or null if the member or key doesn't exist