REDIS数据类型

redis数据类型的基本操作

string

String 是 redis 最基本的类型,它与 Memcached 一模一样的类型,一个 key 对应一个 value。

相关命令

1>SET key value        

设置指定 key 的值

127.0.0.1:6379> set key1 v1
OK

2>GET key               

获取指定key的值

127.0.0.1:6379> get key1
"v1"

3>GETRANGE key start end

返回 key 中字符串值的子字符,end=-1时表示全部

127.0.0.1:6379> GETRANGE key1 1 2
"1"
127.0.0.1:6379> GETRANGE key1 0 2
"v1"

4>SETBIT key offset value

对 key 所储存的字符串值,设置或清除指定偏移量上的位(bit)

5>GETBIT key offset

对 key 所储存的字符串值,获取指定偏移量上的位(bit)

首先我们先设置一个键值对

127.0.0.1:6379> SET key1 bar
OK

而我们key1的值bar所对应的二进制值为011000100110000101110010,而偏移量实际上指的就是从左往右数,偏移量是几就是第几位,偏移量0就是第0位。

由此就可以测试一下

127.0.0.1:6379> GETBIT key1 0
(integer) 0

偏移量为0是对应的是索引为0所对应的值就是0

127.0.0.1:6379> GETBIT key1 1
(integer) 1

偏移量为1是对应的是索引为1所对应的值就是1

127.0.0.1:6379> GETBIT key1 2
(integer) 1

偏移量为2是对应的是索引为2所对应的值就是1

再来看setbit的用法

127.0.0.1:6379> SETBIT key1 2 0
(integer) 1

127.0.0.1:6379> GET key1
"Bar"

我们将偏移量为2的bit设置成了0,那么所对应的二进制就是010000100110000101110010

所对应的字母就是Bar

6>MSET key value [key value ...] 

同时设置一个或多个 key-value 对

7>MGET key1 [key2..]

获取所有(一个或多个)给定 key 的值

127.0.0.1:6379> mset key1 v1 key2 v2
OK
127.0.0.1:6379> mget key1 key2
1) "v1"
2) "v2"

8>GETSET key value

将给定 key 的值设为 value ,并返回 key 的旧值(old value)

127.0.0.1:6379> get key1
"v1"
127.0.0.1:6379> getset key1 v3
"v1"
127.0.0.1:6379> get key1
"v3"

9>SETEX key seconds value

将值 value 关联到 key ,并将 key 的过期时间设为 seconds (以秒为单位)

127.0.0.1:6379> SETEX key1 5 v1
OK
127.0.0.1:6379> get key1
"v1"
5秒后

127.0.0.1:6379> get key1
(nil)

10>SETNX key value

只有在 key 不存在时设置 key 的值

Integer reply, specifically:

1 if the key was set
0 if the key was not set
127.0.0.1:6379> SETNX key1 v5
(integer) 1
127.0.0.1:6379> SETNX key1 v3
(integer) 0
127.0.0.1:6379> get key1
"v5"

11>SETRANGE key offset value

用 value 参数覆写给定 key 所储存的字符串值,从偏移量 offset 开始

127.0.0.1:6379> SET key1 hello
OK
127.0.0.1:6379> SETRANGE key1 2 word
(integer) 6
127.0.0.1:6379> get key1
"heword"

12>STRLEN key

返回 key 所储存的字符串值的长度

127.0.0.1:6379> get key1
"heword"
127.0.0.1:6379> STRLEN key1
(integer) 6

13>MSETNX key value [key value ...]

同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在

当key值存在时不成立

127.0.0.1:6379> get key1
"heword"
127.0.0.1:6379> get key2
"v2"
127.0.0.1:6379> MSETNX key1 v2 key2 v3
(integer) 0
127.0.0.1:6379> get key1
"heword"
127.0.0.1:6379> get key2
"v2"

当key值不存在时设置

127.0.0.1:6379> get key3
(nil)
127.0.0.1:6379> get key4
(nil)
127.0.0.1:6379> MSETNX key3 v3 key4 v4
(integer) 1
127.0.0.1:6379> get key3
"v3"
127.0.0.1:6379> get key4
"v4"

14>PSETEX key milliseconds value

与 SETEX 命令相似,但它以毫秒为单位设置 key 的生存时间

127.0.0.1:6379> PSETEX key1 5000 v2
OK
127.0.0.1:6379> get key1
"v2"
127.0.0.1:6379> get key1
(nil)

15>INCR key

将 key 中储存的数字值增一

127.0.0.1:6379> set key1 5
OK
127.0.0.1:6379> INCR key1
(integer) 6
127.0.0.1:6379> INCR key1
(integer) 7

16>INCRBY key increment

将 key 所储存的值加上给定的增量值(increment)

127.0.0.1:6379> get key1
"7"
127.0.0.1:6379> INCRBY key1 10
(integer) 17

17>INCRBYFLOAT key increment

将 key 所储存的值加上给定的浮点增量值(increment)

127.0.0.1:6379> get key1
127.0.0.1:6379> 
127.0.0.1:6379> INCRBYFLOAT key1 1.1
"18.1"

18>DECR key

将 key 中储存的数字值减一

127.0.0.1:6379> set key1 17
OK
127.0.0.1:6379> DECR key1
(integer) 16

19>DECRBY key decrement

key 所储存的值减去给定的减量值(decrement)

127.0.0.1:6379> get key1
"16"
127.0.0.1:6379> DECRBY key1 10
(integer) 6

20>APPEND key value

如果 key 已经存在并且是一个字符串,APPEND 命令将指定的 value 追加到该 key 原来值 value 的末尾

127.0.0.1:6379> set key1 hello
OK
127.0.0.1:6379> APPEND key1 world
(integer) 10
127.0.0.1:6379> get key1
"helloworld"

21>GETDEL

获取当前key所对应的值并且删除

127.0.0.1:6379> GETDEL key1
"helloworld"
127.0.0.1:6379> get key1
(nil)

22>GETEX

获取key的值,并选择性地设置其过期时间。GETEX 类似于 GET,但是一个带有附加选项的写入命令

The GETEX command supports a set of options that modify its behavior:

EX seconds -- Set the specified expire time, in seconds.
PX milliseconds -- Set the specified expire time, in milliseconds.
EXAT timestamp-seconds -- Set the specified Unix time at which the key will expire, in seconds.
PXAT timestamp-milliseconds -- Set the specified Unix time at which the key will expire, in milliseconds.
PERSIST -- Remove the time to live associated with the key.

GETEX 命令支持一组修改其行为的选项:

EX 秒 -- 设置指定的过期时间(以秒为单位)。
PX 毫秒 -- 设置指定的过期时间(以毫秒为单位)。
EXAT 时间戳-秒 -- 设置密钥过期的指定 Unix 时间(以秒为单位)。
PXAT 时间戳-毫秒 -- 设置密钥过期的指定 Unix 时间(以毫秒为单位)。
PERSIST -- 删除与密钥关联的生存时间。

127.0.0.1:6379> GETEX key1 EX 5
"v1"
5s后
127.0.0.1:6379> GETEX key1 
(nil)

总结

含有SETEX的是设置key生存的时间

含有SETNX的是对当key不存在时对其进行设置key-values对

在某个命令前家M代表多个的意思

list
Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)一个列表最多可以包含 2^32^ - 1 个元素 (4294967295, 每个列表超过40亿个元素)。

1>LPUSH key value1 [value2]

将一个或多个值插入到列表头部

2>将一个或多个值插入到列表头部

在列表尾部添加一个或多个值

3>LRANGE key start stop

获取列表指定范围内的元素

127.0.0.1:6379> LPUSH key1 5 4
(integer) 2
127.0.0.1:6379> RPUSH key1 6 7
(integer) 4
127.0.0.1:6379> LRANGE key1 0 -1
1) "4"
2) "5"
3) "6"
4) "7"
127.0.0.1:6379> LRANGE key1 0 2
1) "4"
2) "5"
3) "6"

4>LPUSHX key value

将一个值插入到已存在的列表头部

5>RPUSHX key value

将一个值插入到已存在的列表尾部

127.0.0.1:6379> LRANGE key1 0 -1
1) "4"
2) "5"
3) "6"
127.0.0.1:6379> LPUSH key1 3
(integer) 4
127.0.0.1:6379> RPUSH key1 7
(integer) 5
127.0.0.1:6379> LRANGE key1 0 -1
1) "3"
2) "4"
3) "5"
4) "6"
5) "7"

6>LPOP key

移出并获取列表的第一个元素

7>RPOP key

移出并获取列表的最后一个元素

127.0.0.1:6379> LRANGE key1 0 -1
1) "3"
2) "4"
3) "5"
4) "6"
5) "7"
127.0.0.1:6379> LPOP key1
"3"
127.0.0.1:6379> rPOP key1
"7"

8>LLEN key

获取列表长度

127.0.0.1:6379> LRANGE key1 0 -1
1) "4"
2) "5"
3) "6"
127.0.0.1:6379> LLEN key1
(integer) 3
 

9>LINSERT key BEFORE|AFTER pivot value

在列表的元素前或者后插入元素

127.0.0.1:6379> LRANGE key1 0 -1
1) "4"
2) "5"
3) "6"

127.0.0.1:6379> LINSERT key1 before 4 3
(integer) 4
127.0.0.1:6379> linsert key1 after 4 5
(integer) 5
127.0.0.1:6379> LRANGE key1 0 -1
1) "3"
2) "4"
3) "5"
4) "5"
5) "6"

10>LINDEX key index

通过索引获取列表中的元素

127.0.0.1:6379> LINDEX key1 0
"3"
127.0.0.1:6379> LINDEX key1 1
"4"

11>LSET key index value

通过索引设置列表元素的值

127.0.0.1:6379> LRANGE key1 0 -1
1) "3"
2) "4"
3) "5"
4) "5"
5) "6"

127.0.0.1:6379> LSET key1 0 5
OK
127.0.0.1:6379> LRANGE key1 0 -1
1) "5"
2) "4"
3) "5"
4) "5"
5) "6"

12>LREM key count value

移除列表元素

Removes the first count occurrences of elements equal to element from the list stored at key. The count argument influences the operation in the following ways:

count > 0: Remove elements equal to element moving from head to tail.
count < 0: Remove elements equal to element moving from tail to head.
count = 0: Remove all elements equal to element.
count 的正负代表从头向尾还是从尾向头进行删除
count的大小代表删除多少个
127.0.0.1:6379> lpush key1 "hello"
(integer) 1
127.0.0.1:6379> lpush key1 "hello"
(integer) 2
127.0.0.1:6379> lpush key1 "world"
(integer) 3
127.0.0.1:6379> lpush key1 "hello"
(integer) 4
127.0.0.1:6379> lpush key1 "world"
(integer) 5
127.0.0.1:6379> LRANGE key1 0 -1
1) "world"
2) "hello"
3) "world"
4) "hello"
5) "hello"
127.0.0.1:6379> LREM key1 -1 hello
(integer) 1
127.0.0.1:6379> LRANGE key1 0 -1
1) "world"
2) "hello"
3) "world"
4) "hello"
127.0.0.1:6379> LREM key1 -2 hello
(integer) 2
127.0.0.1:6379> LRANGE key1 0 -1
1) "world"
2) "world"
127.0.0.1:6379> LREM key1 1 world
(integer) 1
127.0.0.1:6379> LRANGE key1 0 -1
1) "world"

13>LTRIM key start stop

对一个列表进行修剪,就是让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除

127.0.0.1:6379> LPUSH key1 1
(integer) 1
127.0.0.1:6379> LPUSH key1 2
(integer) 2
127.0.0.1:6379> LPUSH key1 3
(integer) 3
127.0.0.1:6379> LPUSH key1 4
(integer) 4
127.0.0.1:6379> LPUSH key1 5
(integer) 5
127.0.0.1:6379> LPUSH key1 6
(integer) 6
127.0.0.1:6379> LTRIM key1 2 4
OK
127.0.0.1:6379> LRANGE key1 0 -1
1) "4"
2) "3"
3) "2"

14>BLPOP key1 [key2 ] timeout

移出并获取列表的第一个元素,如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止

127.0.0.1:6379> LPUSH key1 1
(integer) 1
127.0.0.1:6379> BLPOP key1 5
1) "key1"
2) "1"
127.0.0.1:6379> BLPOP key1 5
(nil)
(5.04s)

15>BRPOP key1 [key2 ] timeout

移出并获取列表的最后一个元素,如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止

127.0.0.1:6379> RPUSH key1 3
(integer) 1
127.0.0.1:6379> RPUSH key1 4
(integer) 2
127.0.0.1:6379> BRPOP key1 3
1) "key1"
2) "4"
127.0.0.1:6379> BRPOP key1 3
1) "key1"
2) "3"
127.0.0.1:6379> BRPOP key1 3
(nil)
(3.07s)

16>BRPOPLPUSH source destination timeout

从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它;如果列表没有元素会阻塞列表直到等待超时或发现可lpu弹出元素为止

127.0.0.1:6379> RPUSH key1 5
(integer) 1

127.0.0.1:6379> LPUSH key2 3
(integer) 1
127.0.0.1:6379> BRPOPLPUSH key1 key2 5
"5"
127.0.0.1:6379> LRANGE key2 0 -1
1) "5"
2) "3"
127.0.0.1:6379> LRANGE key1 0 -1
(empty array)
127.0.0.1:6379> BRPOPLPUSH key1 ket2 5
(nil)
(5.09s)

17>RPOPLPUSH source destination

移除列表的最后一个元素,并将该元素添加到另一个列表并返回

127.0.0.1:6379> LPUSH key1 1 2 3
(integer) 3
127.0.0.1:6379> RPOPLPUSH key1 key2
"1"
127.0.0.1:6379> LRANGE key2 0 -1
1) "1"
2) "5"
3) "3"

19>lmove

redis> RPUSH mylist "one"
(integer) 1
redis> RPUSH mylist "two"
(integer) 2
redis> RPUSH mylist "three"
(integer) 3
redis> LMOVE mylist myotherlist RIGHT LEFT
"three"
redis> LMOVE mylist myotherlist LEFT RIGHT
"one"
redis> LRANGE mylist 0 -1
1) "two"
redis> LRANGE myotherlist 0 -1
1) "three"
2) "one"
20>lpushx

仅在key已存在且包含列表时,在存储在键的列表的头部插入指定的值。与 LPUSH 相反,当密钥尚不存在时,不会执行任何操作

127.0.0.1:6379> LRANGE key1 0 -1
(empty array)
127.0.0.1:6379> LPUSHX key1 1
(integer) 0
127.0.0.1:6379> LPUSH key1 1
(integer) 1
127.0.0.1:6379> LPUSHX key1 2
(integer) 2
127.0.0.1:6379> LPUSHX key1 3
(integer) 3
127.0.0.1:6379> LRANGE key1 0 -1
1) "3"
2) "2"
3) "1"

set
Redis 的 Set 是 String 类型的无序集合。集合中成员是唯一的,这就意味着集合中不能出现重复的数据。Redis 中集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。 集合中最大的成员数为 2^32^ - 1 (4294967295, 每个集合可存储40多亿个成员)

 1>SADD key member1 [member2]

向集合添加一个或多个成员

2>SMEMBERS key

返回集合中的所有成员

127.0.0.1:6379> SADD key1 1 2 3
(integer) 3
127.0.0.1:6379> SMEMBERS key1
1) "1"
2) "2"
3) "3"
 

3>SCARD key

获取集合的成员数

127.0.0.1:6379> SCARD key1
(integer) 3

4>SRANDMEMBER key [count]

返回集合中一个或多个随机数

127.0.0.1:6379> SMEMBERS key1
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> SRANDMEMBER key1 1
1) "1"
127.0.0.1:6379> SRANDMEMBER key1 2
1) "2"
2) "1"
 

5>SISMEMBER key member

判断 member 元素是否是集合 key 的成员

127.0.0.1:6379> SISMEMBER key1 4
(integer) 0
127.0.0.1:6379> SISMEMBER key1 1
(integer) 1
127.0.0.1:6379> SISMEMBER key1 2
(integer) 1

6>SREM key member1 [member2]

移除集合中一个或多个成员

127.0.0.1:6379> SREM key1 1
(integer) 1
127.0.0.1:6379> SREM key1 2 3
(integer) 2

7>SDIFF key1 [key2]

返回给定所有集合的差集

127.0.0.1:6379> SADD key1 1 2 3
(integer) 3
127.0.0.1:6379> SADD key2 3 4 5
(integer) 3
127.0.0.1:6379> SDIFF key1 key2
1) "1"
2) "2"
127.0.0.1:6379> SDIFF key2 key1
1) "4"
2) "5"

8>SDIFFSTORE destination key1 [key2]

返回给定所有集合的差集并存储在 destination 中

127.0.0.1:6379> SDIFFSTORE key3 key1 key2
(integer) 2
127.0.0.1:6379> SMEMBERS key3
1) "1"
2) "2"

9>SINTER key1 [key2]

返回给定所有集合的交集

127.0.0.1:6379> SINTER key1 key2
1) "3"

10>SINTERSTORE destination key1 [key2]

返回给定所有集合的交集并存储在 destination 中

127.0.0.1:6379> SINTERSTORE key3 key1 key2
(integer) 1
127.0.0.1:6379> SMEMBERS key3
1) "3"

11>SUNION key1 [key2]

返回所有给定集合的并集

127.0.0.1:6379> SUNION key1 key2
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"

12>SUNIONSTORE destination key1 [key2]

所有给定集合的并集存储在 destination 集合中

127.0.0.1:6379> SUNIONSTORE key3 key1 key2
(integer) 5
127.0.0.1:6379> SMEMBERS key3
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"

13>SMOVE source destination member

将 member 元素从 source 集合移动到 destination 集合

127.0.0.1:6379> SMOVE key1 key2 2
(integer) 1
127.0.0.1:6379> SMEMBERS key1
1) "1"
2) "3"
127.0.0.1:6379> SMEMBERS key2
1) "2"
2) "3"
3) "4"
4) "5"

14>SPOP key

移除并返回集合中的一个随机元素

127.0.0.1:6379> SPOP key2
"5"

15>SSCAN key cursor [MATCH pattern] [COUNT count]

迭代集合中的元素

 SSCAN 命令基本语法如下: SSCAN key cursor [MATCH pattern] [COUNT count] cursor - 游标。 pattern - 匹配的模式。 count - 指定从数据集里返回多少元素,默认值为 10 

127.0.0.1:6379> SSCAN fruit 0 match p* count 3
1) "1"
2) 1) "peach"
127.0.0.1:6379> SSCAN fruit 1 match p* count 3
1) "3"
2) 1) "pear"
127.0.0.1:6379> SSCAN fruit 3 match p* count 3
1) "0"
2) (empty array)

zset
Redis 有序集合和集合一样也是string类型元素的集合且不允许重复的成员。不同的是每个元素都会关联一个==double类型的分数==。redis正是通过分数来为集合中的成员进行从小到大的排序。有序集合的成员是唯一的,但分数(score)却可以重复

1>ZADD key score1 member1 [score2 member2]

向有序集合添加一个或多个成员,或者更新已存在成员的分数

127.0.0.1:6379> ZADD key1 1 "one"
(integer) 1
127.0.0.1:6379> ZADD key1 2 "two"
(integer) 1
127.0.0.1:6379> ZADD key1 3 "three" 4 "four"
(integer) 2

2>ZCARD key

获取有序集合的成员数

127.0.0.1:6379> ZCARD key1
(integer) 4

3>ZCOUNT key min max

计算在有序集合中指定区间分数的成员数

127.0.0.1:6379> ZCOUNT key1 1 3
(integer) 3
127.0.0.1:6379> ZCOUNT key1 1 6
(integer) 4

127.0.0.1:6379> ZCOUNT key1 2 4
(integer) 3

4>ZINCRBY key increment member

有序集合中对指定成员的分数加上增量 increment

127.0.0.1:6379> ZINCRBY key1 5 "five"
"5"
127.0.0.1:6379> ZRANGE key1 0 -1
1) "one"
2) "two"
3) "three"
4) "four"
5) "five"

5>ZLEXCOUNT key min max

在有序集合中计算指定字典区间内成员数量

127.0.0.1:6379> ZADD key5 0 a 1 b 2 c
(integer) 3
127.0.0.1:6379> ZRANGE key5 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379> ZLEXCOUNT key5 - +
(integer) 5
127.0.0.1:6379> ZLEXCOUNT key5 [a [d
(integer) 4
127.0.0.1:6379> ZLEXCOUNT key5 [c [d
(integer) 2
127.0.0.1:6379> 

6>ZRANGE key start stop [WITHSCORES]

通过索引区间返回有序集合指定区间内的成员

127.0.0.1:6379> ZRANGE key5 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"

7>ZRANGEBYLEX key min max [LIMIT offset count]

通过字典区间返回有序集合的成员

127.0.0.1:6379> ZRANGEBYLEX key5 [a [e 
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"

8>ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT]

通过分数返回有序集合指定区间内的成员

127.0.0.1:6379> ZRANGEBYSCORE key5 -inf +inf
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379> ZRANGEBYSCORE key5 (1  3
1) "c"
2) "d"
127.0.0.1:6379> ZRANGEBYSCORE key5 (1  (3
1) "c"
 

9>ZRANK key member

返回有序集合中指定成员的索引

127.0.0.1:6379> ZRANK key1 two
(integer) 1
127.0.0.1:6379> ZRANK key1 three
(integer) 2

10>ZREM key member [member ...]

移除有序集合中的一个或多个成员

127.0.0.1:6379> ZREM key1 one two three
(integer) 3

11>ZREMRANGEBYLEX key min max

移除有序集合中给定的字典区间的所有成员

看的是后面的值

127.0.0.1:6379> ZREMRANGEBYLEX key5 [a [c
(integer) 3

127.0.0.1:6379> ZRANGE key5 0 -1
1) "d"
2) "e"

12>ZREMRANGEBYRANK key start stop

移除有序集合中给定的排名区间的所有成员

看的是下标

127.0.0.1:6379> ZREMRANGEBYRANK key1 0 2
(integer) 3

13>ZREMRANGEBYSCORE key min max

移除有序集合中给定的分数区间的所有成员

看的是分数

127.0.0.1:6379> ZREMRANGEBYSCORE key1 3 5
(integer) 2

14>ZREVRANGE key start stop [WITHSCORES]

返回有序集中指定区间内的成员,通过索引,分数从高到低

127.0.0.1:6379> zadd key1 1 a 2 b 3 c 4 d 5 e
(integer) 5
127.0.0.1:6379> ZREVRANGE key1 0 5
1) "e"
2) "d"
3) "c"
4) "b"
5) "a"

15>ZREVRANGEBYSCORE key max min [WITHSCORES]

返回有序集中指定分数区间内的成员,分数从高到低排序

127.0.0.1:6379> ZREVRANGEBYSCORE key1 5 3
1) "e"
2) "d"
3) "c"

16>ZREVRANK key member

返回有序集合中指定成员的排名,有序集成员按分数值递减(从大到小)排序

127.0.0.1:6379> ZREVRANK key1 a
(integer) 4
127.0.0.1:6379> ZREVRANK key1 e
(integer) 0
127.0.0.1:6379> ZREVRANK key1 d
(integer) 1

17>ZSCORE key member

返回有序集中,成员的分数值

127.0.0.1:6379> ZSCORE key1 a
"1"
127.0.0.1:6379> ZSCORE key1 c
"3"

18>ZINTERSTORE destination numkeys key [key ...]

计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中

127.0.0.1:6379> ZINTERSTORE key7 2 key1 key5
(integer) 2
127.0.0.1:6379> ZRANGE key7 0 -1
1) "d"
2) "e"
127.0.0.1:6379> ZRANGE key1 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379> ZRANGE key5 0 -1
1) "d"
2) "e"

19>ZUNIONSTORE destination numkeys key [key ...]

计算给定的一个或多个有序集的并集,并存储在新的 key 中

127.0.0.1:6379> ZUNIONSTORE key8 2 key1 key5
(integer) 5
127.0.0.1:6379> ZRANGE key8 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"

20>ZSCAN key cursor [MATCH pattern] [COUNT count]

迭代有序集合中的元素(包括元素成员和元素分值)

127.0.0.1:6379> ZSCAN key1 0
1) "0"
2)  1) "a"
    2) "1"
    3) "b"
    4) "2"
    5) "c"
    6) "3"
    7) "d"
    8) "4"
    9) "e"
   10) "5"

hash
Redis hash 是一个 string 类型的 field 和 value 的映射表,hash 特别适合用于存储对象。Redis 中每个 hash 可以存储 2^32^ - 1 键值对(40多亿)。

1>HSET key field value

将哈希表 key 中的字段 field 的值设为 value

127.0.0.1:6379> HSET hash1 name 'zhangsan' age 18
(integer) 2
 

2>HGET key field

获取存储在哈希表中指定字段的值

127.0.0.1:6379> HGET hash1 age
"18"
127.0.0.1:6379> HGET hash1 name
"zhangsan"

3>HGETALL key

获取在哈希表中指定 key 的所有字段和值

127.0.0.1:6379> HGETALL hash1
1) "name"
2) "zhangsan"
3) "age"
4) "18"

4>HEXISTS key field

查看哈希表 key 中,指定的字段是否存在

127.0.0.1:6379> HEXISTS hash1 name
(integer) 1
127.0.0.1:6379> HEXISTS hash1 gander
(integer) 0

5>HSETNX key field value

HSETNX key field value

127.0.0.1:6379> HSETNX hash1 name 'lisi'
(integer) 0
127.0.0.1:6379> HSETNX hash1 gender "M"
(integer) 1

6>HKEYS key

获取所有哈希表中的字段

127.0.0.1:6379> HKEYS hash1
1) "name"
2) "age"
3) "gender"

7>HVALS key

获取哈希表中所有值

127.0.0.1:6379> HVALS hash1
1) "zhangsan"
2) "18"
3) "M"

8>HLEN key

获取哈希表中字段的数量

127.0.0.1:6379> HLEN hash1
(integer) 3

9>HMGET key field1 [field2]

获取所有给定字段的值

127.0.0.1:6379> HMGET hash1 name age
1) "zhangsan"
2) "18"

10>HMSET key field1 value1 [field2 value2]

127.0.0.1:6379> HMSET hash1 name "zahngsan" age 18
OK

11>HINCRBY key field increment

为哈希表 key 中的指定字段的整数值加上增量 increment

127.0.0.1:6379> HINCRBY hash1 age 2
(integer) 20

12>HINCRBYFLOAT key field increment

为哈希表 key 中的指定字段的浮点数值加上增量 increment

127.0.0.1:6379> Hsetnx hash1 fight 1.8
(integer) 1
127.0.0.1:6379> HINCRBYFLOAT hash1 fight 0.2
"2"

13>HDEL key field1 [field2]

删除一个或多个哈希表字段

127.0.0.1:6379> HDEL hash1 age
(integer) 1

14>HSCAN key cursor [MATCH pattern] [COUNT count]

迭代哈希表中的键值对

127.0.0.1:6379> HSCAN hash1 0 match "n*"
1) "0"
2) 1) "name"
   2) "zahngsan"
   3) "name1"
   4) "lisi"

bitmaps
现代计算机用二进制(位) 作为信息的基础单位, 1个字节等于8位, 例如“abc”字符串是由3个字节组成, 但实际在计算机存储时将其用二进制表示, “abc”分别对应的ASCII码分别是97、 98、 99, 对应的二进制分别是01100001、 01100010和01100011,如下图:

合理地使用操作位能够有效地提高内存使用率和开发效率。

Redis 6 中提供了 Bitmaps 这个“数据类型”可以实现对位的操作:

1)Bitmaps本身不是一种数据类型,实际上它就是字符串(key-value),但是它可以对字符串的位进行操作。

2)Bitmaps单独提供了一套命令,所以在Redis中使用Bitmaps和使用字符串的方法不太相同。 可以把Bitmaps想象成一个以位为单位的数组, 数组的每个单元只能存储0和1, 数组的下标在Bitmaps中叫做偏移量。

1>setbit

setbit <key> <offset> <value>

这个命令用于设置Bitmaps中某个偏移量的值(0或1),offset偏移量从0开始

127.0.0.1:6379> SETBIT mykey 7 1
(integer) 0
127.0.0.1:6379> SETBIT mykey 7 0
(integer) 1
127.0.0.1:6379> get mykey
"\x00"

2>getbit

getbit <key> <offset>

这个命令用于获取Bitmaps中某个偏移量的值。

127.0.0.1:6379> SETBIT mykey 7 1
(integer) 0
127.0.0.1:6379> GETBIT mykey 0
(integer) 0
127.0.0.1:6379> GETBIT mykey 7
(integer) 1

3>bitcount

bitcount <key> [start end]

这个命令用于统计字符串被设置为1的bit数。

127.0.0.1:6379> set mykey "foobar"
OK
127.0.0.1:6379> BITCOUNT mykey
(integer) 26
127.0.0.1:6379> BITCOUNT mykey 0 0
(integer) 4
127.0.0.1:6379> BITCOUNT mykey 1 1
(integer) 6
127.0.0.1:6379> BITCOUNT mykey 1 1 byte
(integer) 6
127.0.0.1:6379> BITCOUNT mykey 5 30
(integer) 4
127.0.0.1:6379> BITCOUNT mykey 5 30 bit
(integer) 17

4>bitop

bitop and(or/not/xor) <destkey> [key…]

 这个命令是一个复合操作, 它可以做多个Bitmaps的and(交集) 、 or(并集) 、 not(非) 、 xor(异或) 操作并将结果保存在destkey中。

127.0.0.1:6379> set mykey1 "foobar"
OK
127.0.0.1:6379> set mykey2 "abcdef"
OK
127.0.0.1:6379> BITOP and dest mykey1 mykey2
(integer) 6
127.0.0.1:6379> get dest
"`bc`ab"

hyperloglog
为了能够降低一定的精度来平衡存储空间,Redis推出了HyperLogLog。

HyperLogLog 是用来做基数统计的算法,HyperLogLog 的优点是:在输入元素的数量或者体积非常非常大时,计算基数所需的空间总是固定的、并且是很小的。

1>PFADD key element [element ...]

添加指定元素到 HyperLogLog 中

127.0.0.1:6379> PFADD hyper1 "redis"
(integer) 1
127.0.0.1:6379> PFADD hyper1 "mondodb"
(integer) 1
127.0.0.1:6379> PFADD hyper1 "rhce"
(integer) 1

2>PFCOUNT key [key ...]

127.0.0.1:6379> PFCOUNT hyper1 
(integer) 3

3>PFMERGE destkey sourcekey [sourcekey ...]

将多个 HyperLogLog 合并为一个 HyperLogLog

127.0.0.1:6379> PFMERGE hyper1 hyper2
OK
127.0.0.1:6379> PFCOUNT hyper1
(integer) 4

geospatatial
Redis 3.2 中增加了对GEO类型的支持。GEO,Geographic,地理信息的缩写。该类型,就是元素的2维坐标,在地图上就是经纬度。redis基于该类型,提供了经纬度设置,查询,范围查询,距离查询,经纬度Hash等常见操作。

1>geoadd key longitude latitude member [longitude latitude member...]

添加地理位置(经度,纬度,名称)

127.0.0.1:6379> GEOADD china:city 121.47 31.23 shanghai
(integer) 1
127.0.0.1:6379> GEOADD china:city 106.50 29.53 chongqing
(integer) 1

2>geopos key member [member...]

获得指定地区的坐标值

127.0.0.1:6379> GEOPOS china:city chongqing
1) 1) "106.49999767541885376"
   2) "29.52999957900659211"
127.0.0.1:6379> GEOPOS china:city shanghai
1) 1) "121.47000163793563843"
   2) "31.22999903975783553"

3>geodist key member1 member2 [m|km|ft|mi]

获取两个位置之间的直线距离

127.0.0.1:6379> GEODIST china:city shanghai chongqing km
"1447.6737"

4>georadius key longitude latitude radius [m|km|ft|mi]

以给定的经纬度为中心,找出某一半径内的元素

127.0.0.1:6379> GEORADIUS china:city 110 30 1000 km
1) "chongqing"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Redis支持多种数据类型,包括string、hash、list、set和sorted set等。其中,string是最基本、最简单的数据类型,用于存储字符串。 Hash是用于存储键值对的数据结构,其中的value只能存储字符串,不允许存储其他数据类型,也不存在嵌套现象。每个hash可以存储232 - 1个键值对,并可以灵活添加或删除对象属性。但需要注意的是,hash类型并不适合存储大量对象,也不应该将hash作为对象列表使用,因为遍历整体数据的效率可能会较低。 除了string和hash类型,Redis还支持list、set和sorted set等数据类型。List是一个有序的字符串列表,可以进行插入、删除和查找等操作。Set是一个无序的字符串集合,可以进行元素的添加、删除和查找操作,并且不允许重复元素的存在。Sorted Set是一个有序的字符串集合,每个元素都有一个对应的score,可以根据score进行排序和范围查找。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Redis 数据类型](https://blog.csdn.net/weixin_52851967/article/details/122670564)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Faith丶信仰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值