四、Redis数据结构之哈希、列表
1、 Redis数据结构之哈希
哈希类型是指键值对里的value本身存储的也是一个个的KV键值对,类似于python中的dict和java中的map集合。
1.1、 查看命令帮助
not connected> help @hash
HDEL key field [field ...]
summary: Delete one or more hash fields
since: 2.0.0
HEXISTS key field
summary: Determine if a hash field exists
since: 2.0.0
HGET key field
summary: Get the value of a hash field
since: 2.0.0
HGETALL key
summary: Get all the fields and values in a hash
since: 2.0.0
HINCRBY key field increment
summary: Increment the integer value of a hash field by the given number
since: 2.0.0
HINCRBYFLOAT key field increment
summary: Increment the float value of a hash field by the given amount
since: 2.6.0
HKEYS key
summary: Get all the fields in a hash
since: 2.0.0
HLEN key
summary: Get the number of fields in a hash
since: 2.0.0
HMGET key field [field ...]
summary: Get the values of all the given hash fields
since: 2.0.0
HMSET key field value [field value ...]
summary: Set multiple hash fields to multiple values
since: 2.0.0
HSCAN key cursor [MATCH pattern] [COUNT count]
summary: Incrementally iterate hash fields and associated values
since: 2.8.0
HSET key field value [field value ...]
summary: Set the string value of a hash field
since: 2.0.0
HSETNX key field value
summary: Set the value of a hash field, only if the field does not exist
since: 2.0.0
HSTRLEN key field
summary: Get the length of the value of a hash field
since: 3.2.0
HVALS key
summary: Get all the values in a hash
since: 2.0.0
1.2、 查看hash类型的key中指定的field是否存在
hexists key field
127.0.0.1:6379> HEXISTS csdn name
1
127.0.0.1:6379> HEXISTS csdn company
1
1.3、 设置hash类型的单个field或一次性设置多个field
HSET key field value [field value ...]
hset csdn id 1 name Alvin company 安哈大锅饭
1.4、 只有value中不存在的field才会被创建
HSETNX key field value
1.5、 删除数据
1.5.1、 使用hdel命令删除hash类型的value中的fields,可批量可单个删除
HDEL key field1 [field2 ...]
127.0.0.1:6379> HDEL csdn name
1
127.0.0.1:6379> HEXISTS csdn name
0
1.6、 更改数据
1.6.1、 对hash类型中相同的field进行set操作会更新该field的值
HSET key field value [field value ...]
1.7、 查找数据
1.7.1、 使用hget获取指定hash类型的field对应的值
HGET key field
127.0.0.1:6379> HGET csdn company
安哈大锅饭
1.7.2、 使用hmget获取指定hash类型的多个field对应的值
HMGET key field [field ...]
127.0.0.1:6379> HMGET csdn company id
安哈大锅饭
1
1.7.3、 使用hgetall获得指定hash类型对象的全部field和对应的value值
HGETALL key
127.0.0.1:6379> HGETALL csdn
id
1
company
安哈大锅饭
1.7.4、 使用hkeys获得指定hash类型对象的全部field
HKEYS key
127.0.0.1:6379> HKEYS csdn
id
company
1.7.5、 使用hvals获得指定hash类型对象的全部field的values值
HVALS key
127.0.0.1:6379> HVALS csdn
1
安哈大锅饭
1.8、 计数
1.8.1、 hincrby可以对hash对象的指定的field的value做递增操作
与string类型中的incrby类似,hincrby可以对hash对象的指定的field的value做递增操作,increment必须是整数(hash类型中没有hdecrby方法,当increment为负数时为递减操作),value必须是integer类型,否则会报对应的错误。
HINCRBY key field increment
#filed对应的value必须是integer类型,increment必须是整数,可以为负
127.0.0.1:6379> HINCRBY csdn id 10
11
1.8.2、 hincrbyfloat可以对hash对象的指定的field的value做递增操作
与string类型中的incrbyfloat类似,hincrbyfloat可以对hash对象的指定的field的value做递增操作,increment可以是整数或浮点数(hash类型中没有hdecrbyfloat方法,当increment为负数时为递减操作),value必须是数字类型,否则会报对应的错误。
HINCRBYFLOAT key field increment
#filed对应的value必须是数字类型,increment可以是整数或浮点,可以为负
127.0.0.1:6379> HINCRBYFLOAT csdn id 10.9
21.9
1.9、 长度
1.9.1、 hlen返回hash类型中field的数量
HLEN key
127.0.0.1:6379> HLEN csdn
2
1.9.2、 hstrlen返回hash类型中指定filed对应的value的字符长度
HSTRLEN key field
127.0.0.1:6379> HSTRLEN csdn id
4
127.0.0.1:6379> HSTRLEN csdn company
15
1.10、 过期
我们可以看到hash类型没有hsetex hpsetex一类的方法,想对hash对象做过期策略可以使用全局函数expire。
expire key seconds
127.0.0.1:6379> EXPIRE csdn 1000
1
127.0.0.1:6379> ttl csdn
987
2、 Redis数据结构之列表
Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)一个列表最多可以包含 2^32 - 1 个元素 (4294967295, 每个列表超过40亿个元素)。
2.1、 查看命令帮助
help @list
BLPOP key [key ...] timeout
summary: Remove and get the first element in a list, or block until one is available
since: 2.0.0
BRPOP key [key ...] timeout
summary: Remove and get the last element in a list, or block until one is available
since: 2.0.0
BRPOPLPUSH source destination timeout
summary: Pop an element from a list, push it to another list and return it; or block until one is available
since: 2.2.0
LINDEX key index
summary: Get an element from a list by its index
since: 1.0.0
LINSERT key BEFORE|AFTER pivot element
summary: Insert an element before or after another element in a list
since: 2.2.0
LLEN key
summary: Get the length of a list
since: 1.0.0
LPOP key
summary: Remove and get the first element in a list
since: 1.0.0
LPOS key element [RANK rank] [COUNT num-matches] [MAXLEN len]
summary: Return the index of matching elements on a list
since: 6.0.6
LPUSH key element [element ...]
summary: Prepend one or multiple elements to a list
since: 1.0.0
LPUSHX key element [element ...]
summary: Prepend an element to a list, only if the list exists
since: 2.2.0
LRANGE key start stop
summary: Get a range of elements from a list
since: 1.0.0
LREM key count element
summary: Remove elements from a list
since: 1.0.0
LSET key index element
summary: Set the value of an element in a list by its index
since: 1.0.0
LTRIM key start stop
summary: Trim a list to the specified range
since: 1.0.0
RPOP key
summary: Remove and get the last element in a list
since: 1.0.0
RPOPLPUSH source destination
summary: Remove the last element in a list, prepend it to another list and return it
since: 1.2.0
RPUSH key element [element ...]
summary: Append one or multiple elements to a list
since: 1.0.0
RPUSHX key element [element ...]
summary: Append an element to a list, only if the list exists
since: 2.2.0
2.2、 从左边插入元素
从左边插入元素,从左边依次追加进栈,先进后出,后进先出
LPUSH key element [element ...]
127.0.0.1:6379> LPUSH mylist alvin 18
3
127.0.0.1:6379> LPUSH mylist 安哈大锅饭
4
127.0.0.1:6379> LRANGE mylist 0 -1
安哈大锅饭
18
alvin
127.0.0.1:6379>
2.3、 从右边插入元素
从右边插入元素,从右边依次追加进队列,先进先出,后进后出。
RPUSH key element [element ...]
127.0.0.1:6379> RPUSH mylist linux
5
127.0.0.1:6379> RPUSH mylist python
6
127.0.0.1:6379> LRANGE mylist 0 -1
安哈大锅饭
18
alvin
上海市
linux
python
2.4、 list存在时才会从左边依次追加元素
与sting类型中的nx类似,只有当list存在时才会从左边依次追加元素。
LPUSHX key element [element ...]
127.0.0.1:6379> LPUSHX mylist Shanghai
7
127.0.0.1:6379> LRANGE mylist 0 -1
Shanghai
安哈大锅饭
18
alvin
上海市
linux
python
127.0.0.1:6379> LPUSHX testlist Shanghai
0
127.0.0.1:6379> LRANGE testlist 0 -1
127.0.0.1:6379>
2.5、 当list存在时才会从右边依次追加元素
与lpushx类似,只有当list存在时才会从右边依次追加元素。
RPUSHX key element [element ...]
127.0.0.1:6379> RPUSHX mylist QingPu
8
127.0.0.1:6379> LRANGE mylist 0 -1
Shanghai
安哈大锅饭
18
alvin
上海市
linux
python
QingPu
127.0.0.1:6379> RPUSHX testlist QingPu
0
127.0.0.1:6379> LRANGE testlist 0 -1
127.0.0.1:6379>
2.6、 从list中指定的元素的前/后 插入一个新元素
LINSERT key BEFORE|AFTER pivot element
127.0.0.1:6379> LRANGE mylist 0 -1
Shanghai
安哈大锅饭
18
alvin
上海市
linux
python
QingPu
# 在某个KEY之前插入某个值
127.0.0.1:6379> LINSERT mylist before 18 17
9
127.0.0.1:6379> LRANGE mylist 0 -1
Shanghai
安哈大锅饭
17
18
alvin
上海市
linux
python
QingPu
# 在某个值后面添加一个值
127.0.0.1:6379> LINSERT mylist after 18 16
10
127.0.0.1:6379> LRANGE mylist 0 -1
Shanghai
安哈大锅饭
17
18
16
alvin
上海市
linux
python
QingPu
127.0.0.1:6379>
2.7、 删除数据
2.7.1、 从列表左侧开始移除
LREM key count element
127.0.0.1:6379> LINSERT mylist after 18 16
14
127.0.0.1:6379> LINSERT mylist after 18 16
15
127.0.0.1:6379> LINSERT mylist after 18 16
16
127.0.0.1:6379> LRANGE mylist 0 -1
Shanghai
安哈大锅饭
17
17
17
17
18
16
16
16
16
alvin
上海市
linux
python
QingPu
127.0.0.1:6379> LREM mylist 2 16
2
127.0.0.1:6379> LRANGE mylist 0 -1
Shanghai
安哈大锅饭
17
17
17
17
18
16
16
alvin
上海市
linux
python
QingPu
127.0.0.1:6379>
2.8、 修改数据
2.8.1、 修改元素内容
LSET key index element
127.0.0.1:6379> LRANGE mylist 0 -1
Shanghai
安哈大锅饭
17
17
17
17
18
16
16
alvin
上海市
linux
python
QingPu
127.0.0.1:6379> LSET mylist 2 19
OK
127.0.0.1:6379> LRANGE mylist 0 -1
Shanghai
安哈大锅饭
19
17
17
17
18
16
16
alvin
上海市
linux
python
QingPu
127.0.0.1:6379>
2.8.2、 截取为从下标start到下标stop闭区间的列表
将原列表截取为从下标start到下标stop闭区间的列表,即原列表变为一个第start+1个到第stop+1个元素的列表。
LTRIM key start stop
127.0.0.1:6379> LTRIM mylist 2 8
OK
127.0.0.1:6379> LRANGE mylist 0 -1
19
17
17
17
18
16
16
127.0.0.1:6379>
2.9、 查询数据
2.9.1、 查看列表中元素的个数
LLEN key
127.0.0.1:6379> LLEN mylist
7
127.0.0.1:6379>
2.9.2、 根据起止下标查询列表元素
LRANGE key start stop
lrange key 0 -1 #表示查看全部元素
lrange key -1 -1 #表示查看最右边的元素
2.9.3、 根据指定的index下标查看列表中的元素
LINDEX key index
127.0.0.1:6379> LINDEX mylist 3
17
127.0.0.1:6379>
2.9.4、 从左边消费列表中的元素
从左边消费列表中的元素,消费完之后从列表中删除。
LPOP key
127.0.0.1:6379> LRANGE mylist 0 -1
17
17
17
18
16
16
127.0.0.1:6379> LPOP mylist
17
127.0.0.1:6379> LRANGE mylist 0 -1
17
17
18
16
16
127.0.0.1:6379>
2.9.5、 从右边消费列表中的元素
从右边消费列表中的元素,消费完之后从列表中删除。
RPOP key
127.0.0.1:6379> LRANGE mylist 0 -1
17
17
18
16
16
127.0.0.1:6379> RPOP mylist
16
127.0.0.1:6379> LRANGE mylist 0 -1
17
17
18
16
127.0.0.1:6379>
2.9.6、 消费列表最右边的元素并返回
消费列表A的最右边的元素返回,然后追加到列表B的最左边。
RPOPLPUSH source destination
RPOPLPUSH List_A List_B
127.0.0.1:6379> LRANGE mylist 0 -1
17
17
18
16
127.0.0.1:6379> RPOPLPUSH mylist mylist
16
127.0.0.1:6379> LRANGE mylist 0 -1
16
17
17
18
127.0.0.1:6379>
2.9.7、 从列表中左侧查询元素
从列表中左侧查询元素,返回列表的key和左侧第一个元素,若所有查询的列表中都没有元素,则会阻塞等待至设置的timeout秒之后返回空,若在这期间,这些列表新增了元素,则会立刻消费并返回该元素。
BLPOP key [key ...] timeout
127.0.0.1:6379> BLPOP mylist 10
mylist
16
127.0.0.1:6379> BLPOP mylist 10
mylist
17
127.0.0.1:6379> BLPOP mylist 10
mylist
17
127.0.0.1:6379> BLPOP mylist 10
mylist
18
127.0.0.1:6379> BLPOP mylist 10
# 从列表右侧开始消费

浙公网安备 33010602011771号