edgeport - v1.0.6
    Preparing search index...

    Interface RedisSession

    A live Redis connection over a single socket.

    Obtain one from connect. A background pump reads replies and routes them, so commands, pipelines, and Pub/Sub deliveries can all be in flight at once. It is an AsyncDisposable, so await using closes it cleanly.

    send runs any command, which is the escape hatch for everything the typed methods below do not cover (streams, cluster commands, module commands, CONFIG, ...).

    1.0.6

    interface RedisSession {
        protocol: 2 | 3;
        serverInfo: Record<string, RedisNative> | undefined;
        subscriptionCount: number;
        "[asyncDispose]"(): PromiseLike<void>;
        close(): Promise<void>;
        decr(key: string): Promise<number>;
        del(...keys: string[]): Promise<number>;
        eval(script: string, opts?: RedisEvalOptions): Promise<RedisReply>;
        evalSha(sha: string, opts?: RedisEvalOptions): Promise<RedisReply>;
        exists(...keys: string[]): Promise<number>;
        expire(key: string, seconds: number): Promise<boolean>;
        get(key: string): Promise<Uint8Array<ArrayBufferLike> | null>;
        getText(key: string): Promise<string | null>;
        hdel(key: string, ...fields: string[]): Promise<number>;
        hget(
            key: string,
            field: string,
        ): Promise<Uint8Array<ArrayBufferLike> | null>;
        hgetall(key: string): Promise<Record<string, string>>;
        hgetText(key: string, field: string): Promise<string | null>;
        hset(key: string, entries: Record<string, RedisArg>): Promise<number>;
        incr(key: string): Promise<number>;
        incrBy(key: string, by: number): Promise<number>;
        info(section?: string): Promise<Record<string, string>>;
        keys(pattern: string): Promise<string[]>;
        llen(key: string): Promise<number>;
        lpop(key: string): Promise<string | null>;
        lpush(key: string, ...values: RedisArg[]): Promise<number>;
        lrange(key: string, start: number, stop: number): Promise<string[]>;
        mget(keys: readonly string[]): Promise<(string | null)[]>;
        mset(entries: Record<string, RedisArg>): Promise<void>;
        multi(commands: readonly (readonly RedisArg[])[]): Promise<RedisReply[]>;
        ping(message?: string): Promise<string>;
        pipeline(commands: readonly (readonly RedisArg[])[]): Promise<RedisReply[]>;
        psubscribe(...patterns: string[]): Promise<RedisSubscription>;
        publish(channel: string, message: RedisArg): Promise<number>;
        publishJson(channel: string, value: unknown): Promise<number>;
        rpop(key: string): Promise<string | null>;
        rpush(key: string, ...values: RedisArg[]): Promise<number>;
        sadd(key: string, ...members: RedisArg[]): Promise<number>;
        scan(opts?: RedisScanOptions & { cursor?: string }): Promise<RedisScanPage>;
        scanIterator(opts?: RedisScanOptions): AsyncIterableIterator<string>;
        select(db: number): Promise<void>;
        send(...args: RedisArg[]): Promise<RedisReply>;
        set(key: string, value: RedisArg, opts?: RedisSetOptions): Promise<boolean>;
        sismember(key: string, member: RedisArg): Promise<boolean>;
        smembers(key: string): Promise<string[]>;
        srem(key: string, ...members: RedisArg[]): Promise<number>;
        subscribe(...channels: string[]): Promise<RedisSubscription>;
        ttl(key: string): Promise<number>;
        zadd(key: string, entries: Record<string, number>): Promise<number>;
        zrange(
            key: string,
            start: number,
            stop: number,
            opts?: { rev?: boolean },
        ): Promise<string[]>;
        zrangeWithScores(
            key: string,
            start: number,
            stop: number,
            opts?: { rev?: boolean },
        ): Promise<RedisScoreEntry[]>;
        zrem(key: string, ...members: RedisArg[]): Promise<number>;
    }

    Hierarchy

    • AsyncDisposable
      • RedisSession
    Index
    protocol: 2 | 3

    The RESP version in use on this connection.

    serverInfo: Record<string, RedisNative> | undefined

    The server properties from the HELLO handshake; only populated when protocol: 3.

    subscriptionCount: number

    How many channels and patterns the connection is currently subscribed to.

    • Returns PromiseLike<void>

    • Closes the connection and ends every subscription.

      Returns Promise<void>

      Resolves once the socket is closed.

    • Decrements a key by one.

      Parameters

      • key: string

        The key to decrement.

      Returns Promise<number>

      The value after the decrement.

    • Deletes keys.

      Parameters

      • ...keys: string[]

        The keys to delete.

      Returns Promise<number>

      How many keys were removed.

    • Runs a Lua script the server has already cached, by its SHA1.

      Parameters

      • sha: string

        The script's SHA1, as returned by SCRIPT LOAD.

      • Optionalopts: RedisEvalOptions

        The script's KEYS and ARGV.

      Returns Promise<RedisReply>

      The script's reply.

      If the script is not cached (NOSCRIPT) or raises an error.

    • Counts how many of the given keys exist.

      Parameters

      • ...keys: string[]

        The keys to test.

      Returns Promise<number>

      The number that exist (counting duplicates separately, as Redis does).

    • Sets a key's time to live.

      Parameters

      • key: string

        The key to expire.

      • seconds: number

        Seconds until expiry.

      Returns Promise<boolean>

      True if the timeout was set, false if the key does not exist.

    • Gets a key's value as raw bytes.

      Parameters

      • key: string

        The key to read.

      Returns Promise<Uint8Array<ArrayBufferLike> | null>

      The bytes, or null if the key does not exist.

    • Gets a key's value as UTF-8 text.

      Parameters

      • key: string

        The key to read.

      Returns Promise<string | null>

      The value, or null if the key does not exist.

    • Deletes hash fields.

      Parameters

      • key: string

        The hash key.

      • ...fields: string[]

        The fields to remove.

      Returns Promise<number>

      How many fields were removed.

    • Gets a hash field as raw bytes.

      Parameters

      • key: string

        The hash key.

      • field: string

        The field to read.

      Returns Promise<Uint8Array<ArrayBufferLike> | null>

      The bytes, or null if the field or key does not exist.

    • Reads a whole hash.

      Normalizes the RESP2 flat array and the RESP3 map into the same object.

      Parameters

      • key: string

        The hash key.

      Returns Promise<Record<string, string>>

      The fields as a string-to-string object; empty if the key does not exist.

    • Gets a hash field as UTF-8 text.

      Parameters

      • key: string

        The hash key.

      • field: string

        The field to read.

      Returns Promise<string | null>

      The value, or null if the field or key does not exist.

    • Sets one or more hash fields.

      Parameters

      • key: string

        The hash key.

      • entries: Record<string, RedisArg>

        Field-to-value pairs to write.

      Returns Promise<number>

      How many fields were newly added.

    • Increments a key by one.

      Parameters

      • key: string

        The key to increment.

      Returns Promise<number>

      The value after the increment.

    • Increments a key by an integer amount.

      Parameters

      • key: string

        The key to increment.

      • by: number

        The amount to add (may be negative).

      Returns Promise<number>

      The value after the increment.

    • Reads the server's INFO report, parsed into fields.

      Parameters

      • Optionalsection: string

        Optional section to limit the report to, e.g. 'memory'.

      Returns Promise<Record<string, string>>

      The report's key:value lines as an object (section headers are dropped).

    • Lists every key matching a pattern.

      KEYS walks the whole keyspace, which blocks the server on a large database; prefer scanIterator outside of small datasets and one-off tooling.

      Parameters

      • pattern: string

        A glob-style pattern, e.g. 'session:*'.

      Returns Promise<string[]>

      The matching keys.

    • Reads a list's length.

      Parameters

      • key: string

        The list key.

      Returns Promise<number>

      The number of elements; 0 if the key does not exist.

    • Removes and returns the first element of a list.

      Parameters

      • key: string

        The list key.

      Returns Promise<string | null>

      The element as text, or null if the list is empty.

    • Prepends values to a list.

      Parameters

      • key: string

        The list key.

      • ...values: RedisArg[]

        The values to prepend.

      Returns Promise<number>

      The list's length afterwards.

    • Reads a range of a list.

      Parameters

      • key: string

        The list key.

      • start: number

        First index (0-based; negative counts from the end).

      • stop: number

        Last index, inclusive (-1 for the final element).

      Returns Promise<string[]>

      The elements as text.

    • Gets several keys at once.

      Parameters

      • keys: readonly string[]

        The keys to read.

      Returns Promise<(string | null)[]>

      One entry per key, null where the key does not exist.

    • Sets several keys at once.

      Parameters

      • entries: Record<string, RedisArg>

        Key-to-value pairs to write.

      Returns Promise<void>

      Resolves once the server confirms.

    • Runs several commands atomically in a MULTI / EXEC transaction.

      Like pipeline, a per-command failure surfaces on that reply's RedisReply.error rather than throwing.

      Parameters

      • commands: readonly (readonly RedisArg[])[]

        The commands to queue, in order.

      Returns Promise<RedisReply[]>

      One reply per command, as returned by EXEC.

      If the server rejects a command at queue time and aborts the transaction, or if the transaction is discarded.

      If the connection is closed.

    • Pings the server.

      Parameters

      • Optionalmessage: string

        Optional payload the server echoes back.

      Returns Promise<string>

      'PONG', or the echoed message.

    • Runs several commands in one round trip and returns their replies in order.

      Unlike send, a command that fails does not throw: its reply carries the message on RedisReply.error so one failure does not discard the other results. Reading such a reply's value throws, so a failure cannot be mistaken for data.

      Parameters

      • commands: readonly (readonly RedisArg[])[]

        The commands to run, in order.

      Returns Promise<RedisReply[]>

      One reply per command.

      If the connection is closed.

    • Subscribes to one or more glob-style channel patterns.

      Parameters

      • ...patterns: string[]

        The patterns to subscribe to, e.g. 'news.*'.

      Returns Promise<RedisSubscription>

      The subscription; each delivery carries the matched pattern.

      If no patterns are given.

    • Publishes a message to a channel.

      Parameters

      • channel: string

        The channel to publish to.

      • message: RedisArg

        The payload (a string is UTF-8 encoded).

      Returns Promise<number>

      How many subscribers received it.

    • Publishes a value as JSON to a channel.

      Parameters

      • channel: string

        The channel to publish to.

      • value: unknown

        The value to serialize and publish.

      Returns Promise<number>

      How many subscribers received it.

    • Removes and returns the last element of a list.

      Parameters

      • key: string

        The list key.

      Returns Promise<string | null>

      The element as text, or null if the list is empty.

    • Appends values to a list.

      Parameters

      • key: string

        The list key.

      • ...values: RedisArg[]

        The values to append.

      Returns Promise<number>

      The list's length afterwards.

    • Adds members to a set.

      Parameters

      • key: string

        The set key.

      • ...members: RedisArg[]

        The members to add.

      Returns Promise<number>

      How many members were newly added.

    • Walks the whole keyspace one page at a time, yielding each key.

      Parameters

      Returns AsyncIterableIterator<string>

      An async iterable of keys.

      for await (const key of redis.scanIterator({ match: 'session:*' })) {
      await redis.del(key);
      }
    • Switches the connection to another database index.

      Parameters

      • db: number

        The database index.

      Returns Promise<void>

      Resolves once the server confirms.

    • Runs one command and returns its reply.

      Parameters

      • ...args: RedisArg[]

        The command name followed by its arguments.

      Returns Promise<RedisReply>

      The reply.

      If the server rejects the command for want of authorization.

      If the server replies with an error.

      If the connection is closed.

      const reply = await redis.send('SET', 'greeting', 'hello');
      reply.text(); // 'OK'
    • Sets a key, optionally with an expiry or an existence condition.

      Parameters

      • key: string

        The key to write.

      • value: RedisArg

        The value (a string is UTF-8 encoded).

      • Optionalopts: RedisSetOptions

        Expiry and NX / XX options.

      Returns Promise<boolean>

      True if the key was set; false when nx or xx prevented it.

    • Tests set membership.

      Parameters

      • key: string

        The set key.

      • member: RedisArg

        The member to test.

      Returns Promise<boolean>

      True if the member is in the set.

    • Reads every member of a set.

      Parameters

      • key: string

        The set key.

      Returns Promise<string[]>

      The members as text; empty if the key does not exist.

    • Removes members from a set.

      Parameters

      • key: string

        The set key.

      • ...members: RedisArg[]

        The members to remove.

      Returns Promise<number>

      How many members were removed.

    • Subscribes to one or more channels.

      Resolves once the server has confirmed every channel, so a publish issued afterwards is guaranteed to be delivered. On a protocol: 2 connection this puts the socket into subscriber mode, where Redis rejects ordinary commands until every subscription is released; protocol: 3 has no such restriction.

      Parameters

      • ...channels: string[]

        The channels to subscribe to.

      Returns Promise<RedisSubscription>

      The subscription.

      If no channels are given.

    • Reads a key's remaining time to live.

      Parameters

      • key: string

        The key to inspect.

      Returns Promise<number>

      Seconds remaining, -1 if the key has no expiry, -2 if it does not exist.

    • Adds scored members to a sorted set.

      Parameters

      • key: string

        The sorted-set key.

      • entries: Record<string, number>

        Member-to-score pairs.

      Returns Promise<number>

      How many members were newly added.

    • Reads a range of a sorted set by rank.

      Parameters

      • key: string

        The sorted-set key.

      • start: number

        First rank (0-based; negative counts from the end).

      • stop: number

        Last rank, inclusive (-1 for the last member).

      • Optionalopts: { rev?: boolean }

        Pass rev to order from highest score to lowest.

      Returns Promise<string[]>

      The members as text, in score order.

    • Reads a range of a sorted set by rank, with each member's score.

      Normalizes the RESP2 flat array and the RESP3 member-score pairs into the same shape.

      Parameters

      • key: string

        The sorted-set key.

      • start: number

        First rank (0-based; negative counts from the end).

      • stop: number

        Last rank, inclusive (-1 for the last member).

      • Optionalopts: { rev?: boolean }

        Pass rev to order from highest score to lowest.

      Returns Promise<RedisScoreEntry[]>

      The members with their scores, in score order.

    • Removes members from a sorted set.

      Parameters

      • key: string

        The sorted-set key.

      • ...members: RedisArg[]

        The members to remove.

      Returns Promise<number>

      How many members were removed.