首页 > 数据库 >005-Redis的 Hash 命令组

005-Redis的 Hash 命令组

时间:2022-10-06 14:55:56浏览次数:73  
标签:Hash 005 6379 0.1 Redis value field key hash

1. Hash

1.1 hdel

1.1.1 基本信息

HDEL key field [field ...]

summary: Delete one or more hash fields

since: 2.0.0

Removes the specified fields from the hash stored at key. 从键存储的哈希中移除指定的字段。

Specified fields that do not exist within this hash are ignored. 如果指定字段不存在,则忽略。

If key does not exist, it is treated as an empty hash and this command returns 0. 如果键不存在,则将其视为空散列,该命令返回0。

1.1.2 返回

  • 从哈希中移除的字段数,不包括指定但不存在的字段数

1.1.3 练习

127.0.0.1:6379[1]> hset myhash field1 "field1" field2 "field2"
(integer) 2
127.0.0.1:6379[1]> hdel myhash field1
(integer) 1

1.2 hexists

1.2.1 基本信息

HEXISTS key field

summary: Determine if a hash field exists

since: 2.0.0

Returns if field is an existing field in the hash stored at key.

返回字段是否为键存储的散列中的现有字段。

1.2.2 返回

  • 如果散列包含字段,则为1
  • 如果散列不包含字段或键不存在,则返回0

1.2.3 练习

127.0.0.1:6379[1]> hexists myhash field1
(integer) 0
127.0.0.1:6379[1]> hexists myhash field2
(integer) 1

1.3 hget

1.3.1 基本信息

HGET key field

summary: Get the value of a hash field

since: 2.0.0

Returns the value associated with field in the hash stored at key.

返回存储在键上的哈希中与字段关联的值。

1.3.2 返回

  • 与字段关联的值,如果字段不存在于散列或键中,则为 nil

1.3.3 练习

127.0.0.1:6379[1]> hget myhash field2
"field2"

1.4 hgetall

1.4.1 基本信息

HGETALL key

summary: Get all the fields and values in a hash

since: 2.0.0

Returns all fields and values of the hash stored at key. 返回键存储的哈希的所有字段和值。

In the returned value, every field name is followed by its value, so the length of the reply is twice the size of the hash.

在返回的值中,每个字段名后面都跟着它的值,因此长度是散列大小的两倍。

1.4.2 返回

  • 存储在散列中的字段及其值的列表,或键不存在时的空列表

1.4.3 练习

127.0.0.1:6379[1]> hgetall myhash
1) "field2"
2) "field2"

1.5 hincrby

1.5.1 基本信息

HINCRBY key field increment

summary: Increment the integer value of a hash field by the given number

since: 2.0.0

Increments the number stored at field in the hash stored at key by increment. 按增量递增存储在键上的散列中字段中的数字。

If key does not exist, a new key holding a hash is created. If field does not exist the value is set to 0 before the operation is performed.

如果键不存在,则创建一个包含散列的新键。如果字段不存在,则在执行操作之前将该值设置为0。

The range of values supported by HINCRBY is limited to 64 bit signed integers.

HINCRBY 支持的值范围限制为64位有符号整数。

1.5.2 返回

  • 增量操作后字段的值

1.5.3 练习

127.0.0.1:6379[1]> hset myhash field3 3
(integer) 1
127.0.0.1:6379[1]> hincrby myhash field3 5
(integer) 8
127.0.0.1:6379[1]> hget myhash field3
"8"
127.0.0.1:6379[1]> hincrby myhash field3 -9
(integer) -1
127.0.0.1:6379[1]> hget myhash field3
"-1"

1.6 hincrbyfloat

1.6.1 基本信息

HINCRBYFLOAT key field increment

summary: Increment the float value of a hash field by the given amount

since: 2.6.0

Increment the specified field of a hash stored at key, and representing a floating point number, by the specified increment. If the increment value is negative, the result is to have the hash field value decremented instead of incremented. If the field does not exist, it is set to 0 before performing the operation. An error is returned if one of the following conditions occur:

将存储在键上并表示浮点数的散列的指定字段增加指定的增量。如果增量值为负,则结果是散列字段值递减而不是递增。如果该字段不存在,则在执行操作之前将其设置为0。如果出现下列情况之一,则返回错误:

  • The field contains a value of the wrong type (not a string).

    • 该字段包含错误类型的值(不是字符串)。
  • The current field content or the specified increment are not parsable as a double precision floating point number.

    • 当前字段内容或指定的增量不能解析为双精度浮点数。

1.6.2 返回

  • 字段增量后的值

1.6.3 练习

127.0.0.1:6379[1]> hincrbyfloat myhash field3 7.53
"6.53000000000000025"
127.0.0.1:6379[1]> hincrbyfloat myhash field3 -2
"4.53000000000000025"
127.0.0.1:6379[1]> hget myhash field3
"4.53000000000000025"

1.7 hkeys

1.7.1 基本信息

HKEYS key

summary: Get all the fields in a hash

since: 2.0.0

Returns all field names in the hash stored at key.

返回键存储的散列中的所有字段名。

1.7.2 返回

  • 散列中的字段列表,或键不存在时的空列表

1.7.3 练习

127.0.0.1:6379[1]> hkeys myhash
1) "field2"
2) "field3"

1.8 hlen

1.8.1 基本信息

HLEN key

summary: Get the number of fields in a hash

since: 2.0.0

Returns the number of fields contained in the hash stored at key.

返回键存储的哈希中包含的字段数。

1.8.2 返回

  • 散列中的字段数,或键不存在时为0

1.8.3 练习

127.0.0.1:6379[1]> hlen myhash
(integer) 2

1.9 hmget

1.9.1 基本信息

HMGET key field [field ...]

summary: Get the values of all the given hash fields

since: 2.0.0

Returns the values associated with the specified fields in the hash stored at key.

返回键存储的哈希中与指定字段关联的值。

For every field that does not exist in the hash, a nil value is returned. Because non-existing keys are treated as empty hashes, running HMGET against a non-existing key will return a list of nil values.

对于散列中不存在的每个字段,将返回一个 nil 值。因为不存在的键被视为空散列,对不存在的键运行 HMGET 将返回一个 nil 值列表。

1.9.2 返回

  • 与给定字段关联的值列表,顺序与请求的顺序相同

1.9.3 练习

127.0.0.1:6379[1]> hmget myhash field1 field2 field3
1) (nil)
2) "field2"
3) "4.53000000000000025"

1.10 hrandfield

1.10.1 基本信息

HRANDFIELD key [count [WITHVALUES]]

summary: Get one or multiple random fields from a hash

since: 6.2.0

When called with just the key argument, return a random field from the hash value stored at key.

当仅使用 key 参数调用时,从存储在 key 上的散列值返回一个随机字段。

If the provided count argument is positive, return an array of distinct fields. The array's length is either count or the hash's number of fields (HLEN), whichever is lower.

如果提供的 count 参数为正,则返回一个包含不同字段的数组。数组的长度是 count 或 hash 的字段数(HLEN) ,以较低者为准。

If called with a negative count, the behavior changes and the command is allowed to return the same field multiple times. In this case, the number of returned fields is the absolute value of the specified count.

如果调用时计数为负,则行为将发生更改,并允许命令多次返回相同的字段。在这种情况下,返回字段的数量是指定计数的绝对值。

The optional WITHVALUES modifier changes the reply so it includes the respective values of the randomly selected hash fields.

可选的 WITHVALUES 修饰符更改应答,使其包含随机选择的散列字段的各自值。

1.10.2 返回

  • 如果没有附加 count 参数,该命令将返回一个随机选择的字段
  • 如果键不存在,则返回 nil
  • 当传递额外的 count 参数时,命令返回一个字段数组
  • 当键不存在时返回一个空数组
  • 如果使用 WITHVALUES 修饰符,则是一个列表字段及其来自哈希的值

1.10.3 练习

127.0.0.1:6379[1]> hset myhash field4 "field4" field5 "field5" field6 "4444"
(integer) 3
127.0.0.1:6379[1]> hrandfield myhash 3
1) "field4"
2) "field2"
3) "field3"

1.11 hscan

1.11.1 基本信息

HSCAN key cursor [MATCH pattern] [COUNT count]

summary: Incrementally iterate hash fields and associated values

since: 2.8.0

1.11.2 返回

  • 散列中字段及其关联的值

1.11.3 练习

127.0.0.1:6379[1]> hscan myhash 2
1) "0"
2)  1) "field2"
    2) "field2"
    3) "field3"
    4) "4.53000000000000025"
    5) "field4"
    6) "field4"
    7) "field5"
    8) "field5"
    9) "field6"
   10) "4444"

1.12 hset

1.12.1 基本信息

HSET key field value [field value ...]

summary: Set the string value of a hash field

since: 2.0.0

Sets field in the hash stored at key to value. If key does not exist, a new key holding a hash is created. If field already exists in the hash, it is overwritten.

将存储在键的哈希中的字段设置为 value。如果键不存在,则创建一个包含散列的新键。如果字段已经存在于散列中,则将覆盖该字段。

1.12.2 返回

  • 添加的字段数

1.12.3 练习

127.0.0.1:6379[1]> hset newkey f1 "111" f2 222 f3 333
(integer) 3

1.13 hsetnx

1.13.1 基本信息

HSETNX key field value

summary: Set the value of a hash field, only if the field does not exist

since: 2.0.0

Sets field in the hash stored at key to value, only if field does not yet exist. If key does not exist, a new key holding a hash is created. If field already exists, this operation has no effect.

仅当字段尚不存在时,才将存储在键上的散列中的字段设置为 value。如果键不存在,则创建一个包含散列的新键。如果字段已经存在,则此操作无效。

1.13.2 返回

  • 如果字段是散列中的新字段且值已设置,则为1
  • 如果散列中已存在字段且未执行任何操作,则为0

1.13.3 练习

127.0.0.1:6379[1]> hsetnx newkey f1 666
(integer) 0

1.14 hstrlen

1.14.1 基本信息

HSTRLEN key field

summary: Get the length of the value of a hash field

since: 3.2.0

Returns the string length of the value associated with field in the hash stored at key. If the key or the field do not exist, 0 is returned.

返回键存储的哈希中与字段关联的值的字符串长度。如果键或字段不存在,则返回0。

1.14.2 返回

  • 与字段关联的值的字符串长度,如果字段不存在于哈希或键中,则字符串长度为零。

1.14.3 练习

127.0.0.1:6379[1]> hstrlen newkey f3
(integer) 3

1.15 hvals

1.15.1 基本信息

HVALS key

summary: Get all the values in a hash

since: 2.0.0

Returns all values in the hash stored at key.

返回键存储的哈希中的所有值。

1.15.2 返回

  • 散列中的值列表,或键不存在时的空列表

1.15.3 练习

127.0.0.1:6379[1]> hvals newkey
1) "111"
2) "222"
3) "333"

标签:Hash,005,6379,0.1,Redis,value,field,key,hash
From: https://www.cnblogs.com/autumncat/p/16757606.html

相关文章

  • redis.conf 文件参数说明
    #不区分大小写#unitsarecaseinsensitiveso1GB1Gb1gBareallthesame.#include组合多个配置问题#include/path/to/local.conf#include/path/to/other......
  • NoSQL之Redis配置与优化
    NoSQL之Redis配置与优化一、关系数据库和非关系数据库1.关系型数据库●关系型数据库是一个结构化的数据库,创建在关系模型(二维表格模型)基础上,一般面向于记录。●主要......
  • Redis常用数据类型以及操作
    Redis常用数据类型以及操作一、String数据类型String是redis最基本的类型,最大能存储512MB的数据,String类型是二进制安全的,即可以存储任何数据、比如数字、图片、序列化对......
  • Redis高可用(持久化、主从复制、哨兵、集群)
    Redis高可用(持久化、主从复制、哨兵、集群)一、Redis高可用1.Redis高可用概述在web服务器中,高可用是指服务器可以正常访问的时间,衡量的标准是在多长时间内可以提供正常......
  • redis中lua脚本实战
    redis的lua脚本说明lua脚本其实是一个语言,有完整的程序控制语法和一些简单的特殊的数据结构比如table。不依赖于redis的,但是lua脚本是c写的一个非常简单的语言,所以redis也......
  • 海康威视无法添加到萤石云问题-20221005
    问题描述:型号:DS-2CD2245XM-LGLSET添加到海康互联没问题,但是直接添加到萤石云会提示添加失败(102037)解决方法:在萤石云接入时选择设备令牌二维码,如下图设备令牌中......
  • new project 前后端开发总结(net6+vue+mysql+redis+mq+mongodb+ES+docker)
    newproject前后端开发总结1.开发工具:vscode,vs2022,sqlserver2.前端:vue3,vite,typescript,scss,elementplus,router,asiox,vuex3.后端:.net6,automapper,autoface,sqlsu......
  • 20221005
    20221005简单点题目背景今天有个巨佬不讲题德,出了个题,说他是乱出的水题。他出的可不是水题啊,Trie树,后缀树,AC自动机,训练有素;后来听说他打了三年NOI,看来是,有备而来。题......
  • Solution Set -「NOIP Simu.」20221005
    \(\mathscr{A}\sim\)「CF1252G」PerformanceReview  Link&Submission.  Tag:「水题无tag」  记\(A=a_1\),对于任何其他的\(a\),我们只关心它与\(A\)......
  • 221005 %你赛
    T1题意给出一个长度为\(n\)的字符串\(S\)与一个长度为\(n\)的\(01\)串\(A\)。Bob会从\(1\)开始一直到\(n\)枚举\(i\),如果\(......