哈希类型是指键值对里的value本身存储的也是一个个的KV键值对,类似于python中的dict和java中的map集合。
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
hexists key field
127.0.0.1:6379> HEXISTS csdn name
1
127.0.0.1:6379> HEXISTS csdn company
1
HSET key field value [field value ...]
hset csdn id 1 name Alvin company 安哈大锅饭
HSETNX key field value
HDEL key field1 [field2 ...]
127.0.0.1:6379> HDEL csdn name
1
127.0.0.1:6379> HEXISTS csdn name
0
HSET key field value [field value ...]
HGET key field
127.0.0.1:6379> HGET csdn company
安哈大锅饭
HMGET key field [field ...]
127.0.0.1:6379> HMGET csdn company id
安哈大锅饭
1
HGETALL key
127.0.0.1:6379> HGETALL csdn
id
1
company
安哈大锅饭
HKEYS key
127.0.0.1:6379> HKEYS csdn
id
company
HVALS key
127.0.0.1:6379> HVALS csdn
1
安哈大锅饭
与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
与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
HLEN key
127.0.0.1:6379> HLEN csdn
2
HSTRLEN key field
127.0.0.1:6379> HSTRLEN csdn id
4
127.0.0.1:6379> HSTRLEN csdn company
15
我们可以看到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
Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)一个列表最多可以包含 2^32 - 1 个元素 (4294967295, 每个列表超过40亿个元素)。
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
从左边插入元素,从左边依次追加进栈,先进后出,后进先出
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>
从右边插入元素,从右边依次追加进队列,先进先出,后进后出。
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
与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>
与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>
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>
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>
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>
将原列表截取为从下标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>
LLEN key
127.0.0.1:6379> LLEN mylist
7
127.0.0.1:6379>
LRANGE key start stop
lrange key 0 -1 #表示查看全部元素
lrange key -1 -1 #表示查看最右边的元素
LINDEX key index
127.0.0.1:6379> LINDEX mylist 3
17
127.0.0.1:6379>
从左边消费列表中的元素,消费完之后从列表中删除。
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>
从右边消费列表中的元素,消费完之后从列表中删除。
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>
消费列表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>
从列表中左侧查询元素,返回列表的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
# 从列表右侧开始消费
原文:https://www.cnblogs.com/zyc99/p/14151544.html