<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] ## 哈希表(Hash) ***** ### HSET ``` HSET key field value ``` 將哈希表key中的域field的值設為value。 如果key不存在,一個新的哈希表被創建并進行[HSET](https://redis.readthedocs.io/en/2.4/hash.html#hset)操作。 如果域field已經存在于哈希表中,舊值將被覆蓋。 **時間復雜度:** O(1) **返回值:** 如果field是哈希表中的一個新建域,并且值設置成功,返回1。 如果哈希表中域field已經存在且舊值已被新值覆蓋,返回0。 ``` redis> HSET website google "www.g.cn" # 一個新域 (integer) 1 redis> HSET website google "www.google.com" # 覆蓋一個舊域 (integer) 0 ``` ### HSETNX[](https://redis.readthedocs.io/en/2.4/hash.html#hsetnx "Permalink to this headline") HSETNX key field value 將哈希表key中的域field的值設置為value,當且僅當域field不存在。 若域field已經存在,該操作無效。 如果key不存在,一個新哈希表被創建并執行[HSETNX](https://redis.readthedocs.io/en/2.4/hash.html#hsetnx)命令。 **時間復雜度:** O(1) **返回值:** 設置成功,返回1。 如果給定域已經存在且沒有操作被執行,返回0。 ``` redis> HSETNX nosql key-value-store redis (integer) 1 redis> HSETNX nosql key-value-store redis # 操作無效,域key-value-store已存在 (integer) 0 ``` ### HMSET[](https://redis.readthedocs.io/en/2.4/hash.html#hmset "Permalink to this headline") HMSET key field value \[field value ...\] 同時將多個field\-value(域-值)對設置到哈希表key中。 此命令會覆蓋哈希表中已存在的域。 如果key不存在,一個空哈希表被創建并執行[HMSET](https://redis.readthedocs.io/en/2.4/hash.html#hmset)操作。 **時間復雜度:** O(N),N為field\-value對的數量。 **返回值:** 如果命令執行成功,返回OK。 當key不是哈希表(hash)類型時,返回一個錯誤。 ``` # 情況1: 哈希表 redis> HMSET website google www.google.com yahoo www.yahoo.com OK redis> HGET website google "www.google.com" redis> HGET website yahoo "www.yahoo.com" # 情況2:類型錯誤時 redis> SET G 10 # 出錯情況 OK redis> HMSET G name huangz age 20 (error) ERR Operation against a key holding the wrong kind of value ``` ### HGET[](https://redis.readthedocs.io/en/2.4/hash.html#hget "Permalink to this headline") HGET key field 返回哈希表key中給定域field的值。 **時間復雜度:** O(1) **返回值:** 給定域的值。 當給定域不存在或是給定key不存在時,返回nil。 ``` redis> HSET huangz blog huangz.iteye.com (integer) 1 redis> HGET huangz blog "huangz.iteye.com" ``` ### HMGET[](https://redis.readthedocs.io/en/2.4/hash.html#hmget "Permalink to this headline") ``` HMGET key field \[field ...\] ``` 返回哈希表key中,一個或多個給定域的值。 如果給定的域不存在于哈希表,那么返回一個nil值。 因為不存在的key被當作一個空哈希表來處理,所以對一個不存在的key進行[HMGET](https://redis.readthedocs.io/en/2.4/hash.html#hmget)操作將返回一個只帶有nil值的表。 **時間復雜度:** O(N),N為給定域的數量。 **返回值:** 一個包含多個給定域的關聯值的表,表值的排列順序和給定域參數的請求順序一樣。 ``` redis> HMSET pet dog "doudou" cat "nounou" # 一次保存多個值 OK redis> HMGET pet dog cat fake_pet # 返回值的順序和傳入參數的順序一樣。 1) "doudou" 2) "nounou" 3) (nil) # 不存在的域返回nil值 ``` ### HGETALL[](https://redis.readthedocs.io/en/2.4/hash.html#hgetall "Permalink to this headline") ``` HGETALL key ``` 返回哈希表key中,所有的域和值。 在返回值里,緊跟每個域名(field name)之后是域的值(value),所以返回值的長度是哈希表大小的兩倍。 **時間復雜度:** O(N),N為哈希表的大小。 **返回值:** 以列表形式返回哈希表的域和域的值。 若key不存在,返回空列表。 ``` redis> HSET hash_name jack "Jack Sparrow" (integer) 1 redis> HSET hash_name gump "Forrest Gump" (integer) 1 redis> HGETALL hash_name 1) "jack" # 域 2) "Jack Sparrow" # 值 3) "gump" 4) "Forrest Gump" ``` ### HDEL[](https://redis.readthedocs.io/en/2.4/hash.html#hdel "Permalink to this headline") ``` HDEL key field \[field ...\] ``` 刪除哈希表key中的一個或多個指定域,不存在的域將被忽略。 **時間復雜度:** O(N),N為要刪除的域的數量。 **返回值:** 被成功移除的域的數量,不包括被忽略的域。 Note 在Redis2.4以下的版本里,[HDEL](https://redis.readthedocs.io/en/2.4/hash.html#hdel)每次只能刪除單個域,如果你需要在一個原子時間內刪除多個域,請將命令包含在[*MULTI*](https://redis.readthedocs.io/en/2.4/transaction.html#multi)/[*EXEC*](https://redis.readthedocs.io/en/2.4/transaction.html#exec)塊內。 ``` # 測試數據 redis> HGETALL abbr 1) "a" 2) "apple" 3) "b" 4) "banana" 5) "c" 6) "cat" 7) "d" 8) "dog" # 刪除單個域 redis> HDEL abbr a (integer) 1 # 刪除不存在的域 redis> HDEL abbr not-exists-field (integer) 0 # 刪除多個域 redis> HDEL abbr b c (integer) 2 redis> HGETALL abbr 1) "d" 2) "dog" ``` ### HLEN[](https://redis.readthedocs.io/en/2.4/hash.html#hlen "Permalink to this headline") ``` HLEN key ``` 返回哈希表key中域的數量。 **時間復雜度:** O(1) **返回值:** 哈希表中域的數量。 當key不存在時,返回0。 ``` redis> HSET hash_name jack "Jack Sparrow" (integer) 1 redis> HSET hash_name gump "Forrest Gump" (integer) 1 redis> HLEN hash_name (integer) 2 ``` ### HEXISTS[](https://redis.readthedocs.io/en/2.4/hash.html#hexists "Permalink to this headline") HEXISTS key field 查看哈希表key中,給定域field是否存在。 **時間復雜度:** O(1) **返回值:** 如果哈希表含有給定域,返回1。 如果哈希表不含有給定域,或key不存在,返回0。 ``` redis> HEXISTS phone myphone (integer) 0 redis> HSET phone myphone nokia-1110 (integer) 1 redis> HEXISTS phone myphone (integer) 1 ``` ### HINCRBY[](https://redis.readthedocs.io/en/2.4/hash.html#hincrby "Permalink to this headline") HINCRBY key field increment 為哈希表key中的域field的值加上增量increment。 增量也可以為負數,相當于對給定域進行減法操作。 如果key不存在,一個新的哈希表被創建并執行[HINCRBY](https://redis.readthedocs.io/en/2.4/hash.html#hincrby)命令。 如果域field不存在,那么在執行命令前,域的值被初始化為0。 對一個儲存字符串值的域field執行[HINCRBY](https://redis.readthedocs.io/en/2.4/hash.html#hincrby)命令將造成一個錯誤。 本操作的值限制在64位(bit)有符號數字表示之內。 **時間復雜度:** O(1) **返回值:** 執行[HINCRBY](https://redis.readthedocs.io/en/2.4/hash.html#hincrby)命令之后,哈希表key中域field的值。 ``` ### 情況1:increment為正數 redis> HEXISTS counter page_view # 對空域進行設置 (integer) 0 redis> HINCRBY counter page_view 200 (integer) 200 redis> HGET counter page_view "200" ### 情況2:increment為負數 redis> HGET counter page_view "200" redis> HINCRBY counter page_view -50 (integer) 150 redis> HGET counter page_view "150" ### 情況3:嘗試對字符串值的域執行HINCRBY命令 redis> HSET myhash string hello,world # 設定一個字符串值 (integer) 1 redis> HGET myhash string "hello,world" redis> HINCRBY myhash string 1 # 命令執行失敗,錯誤。 (error) ERR hash value is not an integer redis> HGET myhash string # 原值不變 "hello,world" ``` ### HKEYS[](https://redis.readthedocs.io/en/2.4/hash.html#hkeys "Permalink to this headline") HKEYS key 返回哈希表key中的所有域。 **時間復雜度:** O(N),N為哈希表的大小。 **返回值:** 一個包含哈希表中所有域的表。 當key不存在時,返回一個空表。 ``` ### 情況1:哈希表非空 redis> HMSET website google www.google.com yahoo www.yahoo.com OK redis> HKEYS website 1) "google" 2) "yahoo" ### 情況2:空哈希表/key不存在 redis> EXISTS fake_key (integer) 0 redis> HKEYS fake_key (empty list or set) ``` ### HVALS[](https://redis.readthedocs.io/en/2.4/hash.html#hvals "Permalink to this headline") HVALS key 返回哈希表key中的所有值。 **時間復雜度:** O(N),N為哈希表的大小。 **返回值:** 一個包含哈希表中所有值的表。 當key不存在時,返回一個空表。 ``` ### 情況1:非空哈希表 redis> HMSET website google www.google.com yahoo www.yahoo.com OK redis> HVALS website 1) "www.google.com" 2) "www.yahoo.com" ### 情況2:空哈希表/不存在的key redis> EXISTS not_exists (integer) 0 redis> HVALS not_exists (empty list or set) ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看