Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface HashClient

哈希表

Hierarchy

Index

Methods

hdel

  • hdel(key: string, ...fields: string[]): number
  • 删除哈希表 key 中的一个或多个指定域,不存在的域将被忽略

    Parameters

    • key: string

      哈希表

    • Rest ...fields: string[]

      域列表

    Returns number

    被成功移除的域的数量,不包括被忽略的域

hexists

  • hexists(key: string, field: string): number
  • 检查给定域 field 是否存在于哈希表 hash 当中

    Parameters

    • key: string

      哈希表

    • field: string

    Returns number

    给定域存在时返回 1 , 在给定域不存在时返回 0

hget

  • hget(key: string, field: string): string
  • 返回哈希表中给定域的值。

    Parameters

    • key: string

      哈希表

    • field: string

    Returns string

    如果存在则返回值,不存在则返回空字符串

hgetAll

  • 返回哈希表 key 中,所有的域和值。

    Parameters

    • key: string

      哈希表

    Returns StringDict

    键值对

hincrby

  • hincrby(key: string, field: string, increment: number): number
  • 为哈希表 key 中的域 field 的值加上增量 increment

    Parameters

    • key: string

      哈希表, 如果 key 不存在,一个新的哈希表被创建并执行 HINCRBY 命令

    • field: string

      域,如果域 field 不存在,那么在执行命令前,域的值被初始化为 0。 如果存在且值是字符串的, 则抛异常

    • increment: number

      增量,可为负数,本操作的值被限制在 64 位(bit)有符号数字表示之内

    Returns number

    执行 HINCRBY 命令之后,哈希表 key 中域 field 的值

hkeys

  • hkeys(key: string): string[]
  • 返回哈希表 key 中的所有域。

    Parameters

    • key: string

      哈希表

    Returns string[]

    一个包含哈希表中所有域的表。 当 key 不存在时,返回一个空表。

hlen

  • hlen(key: string): number
  • 返回哈希表 key 中域的数量。

    Parameters

    • key: string

      哈希表

    Returns number

    哈希表中域的数量。 当 key 不存在时,返回 0 。

hmget

  • hmget(key: string, ...fields: string[]): string[]
  • 返回哈希表 key 中,一个或多个给定域的值。

    Parameters

    • key: string

      哈希表

    • Rest ...fields: string[]

      域列表

    Returns string[]

    一个包含多个给定域的关联值的表,表值的排列顺序和给定域参数的请求顺序一样。

hmset

  • 同时将多个 field-value (域-值)对设置到哈希表 key 中。 此命令会覆盖哈希表中已存在的域。

    Parameters

    • key: string

      哈希表,如果 key 不存在,一个空哈希表被创建并执行 HMSET 操作

    • map: StringDict

      必须是<string, string>键值对

    Returns string

    如果命令执行成功,返回 OK。 当 key 不是哈希表(hash)类型时,返回一个错误

hscan

  • 哈希表的增量式迭代命令, 用于增量地迭代一个哈希表上的元素。

    example
    import * as redis from 'redis';
    
    let cli = redis.newClient("test");
    let key = "hash_test_scan_1";
    
    // 预置数据用于测试
    // for (let i = 0; i < 2000; i++) {
    //     cli.hset(key, `F:${i}`, `VBCCCCC_${i}`);
    // }
    
    // console.log(cli.hmset(key, { A: "A", B: "B", C: "C2", D: "D2" }));
    console.log("no option", cli.hscan(key, 0));
    console.log("empty option", cli.hscan(key, 0, {}));
    console.log("count 5", cli.hscan(key, 0, { match: "F:1*", count: 5 }));
    
    
    // scan all
    let pattern = "F:1*";
    let count = 100;
    
    console.log("scan all with iteration ", hscan(key, count, pattern));
    console.log("scan all with iteration ", hscan(key, 50, pattern));
    
    function hscan(key: string, count: number, pattern: string): number {
        let cursor = 0;
        let iter = 1;
        let m = {}
        while (true) {
            console.log("iteration ", iter, "cursor ", cursor);
            let res = cli.hscan(key, cursor, { match: pattern, count: count });
            // 仅用于检查是否会出现重复,按cursor迭代不会出现重复。
            for (let k in res.hash) {
                if (m[k]) {
                    throw new Error(`duplicated hash field ${k}`);
                }
                m[k] = res.hash[k];
                console.log(k, " => ", res.hash[k]);
            }
            // 如果 cursor 为 0 代表已经遍历完了整个数据集
            if (res.cursor == 0) {
                // console.log("end with iteration ", iter);
                console.log("all fields ", Object.keys(m).length)
                return iter;
            }
            iter++;
            cursor = res.cursor;
        }
    }
    
    

    Parameters

    • key: string

      哈希表

    • cursor: number

      起始游标

    • Optional opt: ScanOption

      scan 可选参数

    Returns HScanResult

hset

  • hset(key: string, field: string, value: string): number
  • 将哈希表 hash 中域 field 的值设置为 value

    Parameters

    • key: string

      哈希表

    • field: string

    • value: string

      字段值, 如果值为null或者undefined,设置成空字符串

    Returns number

    当 HSET 命令在哈希表中新创建 field 域并成功为它设置值时, 命令返回 1 ; 如果域 field 已经存在于哈希表, 并且 HSET 命令成功使用新值覆盖了它的旧值, 那么命令返回 0 。

hsetnx

  • hsetnx(key: string, field: string, value: string): number
  • 当且仅当域 field 尚未存在于哈希表的情况下, 将它的值设置为 value

    Parameters

    • key: string

      哈希表

    • field: string

    • value: string

      字段值

    Returns number

    HSETNX 命令在设置成功时返回 1 , 在给定域已经存在而放弃执行设置操作时返回 0

hstrlen

  • hstrlen(key: string, field: string): number
  • 返回哈希表 key 中, 与给定域 field 相关联的值的字符串长度(string length)

    Parameters

    • key: string

      哈希表

    • field: string

    Returns number

    如果给定的键或者域不存在, 那么命令返回 0

hvals

  • hvals(key: string): string[]
  • 返回哈希表 key 中所有域的值。

    Parameters

    • key: string

      哈希表

    Returns string[]

    一个包含哈希表中所有值的表。 当 key 不存在时,返回一个空表。