redis数据类型和持久化

redis9种数据类型的基本操作

string

1.set - 设置指定 key 的值

127.0.0.1:6379> set lin 'hello'
OK

2.get - 获取指定 key 的值

127.0.0.1:6379> get lin
"hello"

3.getrange - 返回 key 中字符串值的子字符

127.0.0.1:6379> getrange lin 1 3
"ell"

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

127.0.0.1:6379> setbit lin 7 0
(integer) 1
127.0.0.1:6379> setbit lin 7 1
(integer) 0

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

127.0.0.1:6379> getbit lin 1
(integer) 1
127.0.0.1:6379> getbit lin 0
(integer) 0

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

127.0.0.1:6379> mset lin1 'hello word' lin2 "I am using redis"
OK

7.mget - 获取所有(一个或多个)给定 key 的值

127.0.0.1:6379> mget lin lin1 lin2
1) "hello"
2) "hello word"
3) "I am using redis"

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

127.0.0.1:6379> getset lin redis-cli
"hello"

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

127.0.0.1:6379> setex lin 120 'lin'
OK
127.0.0.1:6379> ttl lin
(integer) 115
127.0.0.1:6379> get lin
"lin"

10.setnx - 只有在 key 不存在时设置 key 的值

127.0.0.1:6379> setnx lin3 'lin3'
(integer) 1
127.0.0.1:6379> setnx lin3 'lin4'
(integer) 0

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

127.0.0.1:6379> get lin1
"hello word"
127.0.0.1:6379> 
127.0.0.1:6379> setrange lin1 6 redis
(integer) 11
127.0.0.1:6379> get lin1
"hello redis"

12.strlen - 返回 key 所储存的字符串值的长度

127.0.0.1:6379> get lin1
"hello redis"
127.0.0.1:6379> strlen lin1
(integer) 11

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

127.0.0.1:6379> msetnx lin 'redis' lin4 'use redis'
(integer) 1
127.0.0.1:6379> msetnx lin 'redis' lin4 'use redis'
(integer) 0

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

redis> PSETEX mykey 1000 "Hello"
"OK"
redis> PTTL mykey
(integer) 999
redis> GET mykey
"Hello"

15.incr - 将 key 中储存的数字值增一

127.0.0.1:6379> set name 11
OK
127.0.0.1:6379> get name
"11"
127.0.0.1:6379> incr name
(integer) 12
127.0.0.1:6379> get name
"12"

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

127.0.0.1:6379> incrby name 10
(integer) 22
127.0.0.1:6379> get name
"22"

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

127.0.0.1:6379> incrbyfloat name 0.5
"22.5"

18.decr - 将 key 中储存的数字值减一

127.0.0.1:6379> set num 10
OK
127.0.0.1:6379> decr num
(integer) 9

19.decrby - key 所储存的值减去给定的减量值(decrement)

127.0.0.1:6379> decrby num 3
(integer) 6
127.0.0.1:6379> get num
"6"

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

127.0.0.1:6379> get lin2
"I am using redis"
127.0.0.1:6379> append lin2 ' end'
(integer) 20
127.0.0.1:6379> get lin2
"I am using redis end"

List

1.lpush - 将一个或多个值插入到列表头部

127.0.0.1:6379> lpush list1 'list' 'list1' 'list2'
(integer) 3

2.lpop - 移出并获取列表的第一个元素

127.0.0.1:6379> lpop list1
"list2"

3.lrange - 获取列表指定范围内的元素

127.0.0.1:6379> lrange list1 0 10
1) "list1"
2) "list"

4.lpushx - 将一个值插入到已存在的列表头部

127.0.0.1:6379> lrange list1 0 10
1) "list1"
2) "list"
127.0.0.1:6379> 
127.0.0.1:6379> lpush list1 list2
(integer) 3
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list"

5.rpush - 在列表中添加一个或多个值

127.0.0.1:6379> rpush list1 'list3' 'list4'
(integer) 5
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list"
4) "list3"
5) "list4"

6.rpop - 移除列表的最后一个元素,返回值为移除的元素

127.0.0.1:6379> rpop list1
"list"

7.rpushx - 为已存在的列表添加值

127.0.0.1:6379> rpushx list1 'list'
(integer) 3
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list"

8.llen - 获取列表长度

127.0.0.1:6379> llen list1
(integer) 5

9.linsert - 在列表的元素前或者后插入元素

127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list"
4) "list3"
5) "list4"
127.0.0.1:6379> linsert list1 before 'list4' 'list5'
(integer) 6
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list"
4) "list3"
5) "list5"
6) "list4"
127.0.0.1:6379> linsert list1 after 'list4' 'list6'
(integer) 7
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list"
4) "list3"
5) "list5"
6) "list4"
7) "list6"

10.lindex - 通过索引获取列表中的元素

127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list"
4) "list3"
5) "list5"
6) "list4"
7) "list6"
127.0.0.1:6379> lindex list1 3
"list3"

11.lset - 通过索引设置列表元素的值

127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list"
4) "list3"
5) "list5"
6) "list4"
7) "list6"
127.0.0.1:6379> lset list1 3 'list7'
OK
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list"
4) "list7"
5) "list5"
6) "list4"
7) "list6"

12.lrem - 移除列表元素

127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list"
4) "list7"
5) "list5"
6) "list4"
7) "list6"
127.0.0.1:6379> 
127.0.0.1:6379> lrem list1 2 'list'
(integer) 1
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list7"
4) "list5"
5) "list4"
6) "list6"

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

127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list7"
4) "list5"
5) "list4"
6) "list6"
127.0.0.1:6379> 
127.0.0.1:6379> 
127.0.0.1:6379> ltrim list1 0 2
OK
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list7"

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

127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list7"
127.0.0.1:6379> blpop list1 5
1) "list1"
2) "list2"
127.0.0.1:6379> lrange list1 0 10
1) "list1"
2) "list7"

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

127.0.0.1:6379> lrange list1 0 10
1) "list1"
2) "list7"
127.0.0.1:6379> brpop list1 5
1) "list1"
2) "list7"
127.0.0.1:6379> lrange list1 0 10
1) "list1"

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

127.0.0.1:6379> rpoplpush list list1 
"list"
127.0.0.1:6379> lrange list 0 -1
1) "list5"
2) "list4"
3) "list3"
4) "list2"
5) "list1"
127.0.0.1:6379> lrange list1 0 -1
1) "list"

set

1.sadd - 向集合添加一个或多个成员

127.0.0.1:6379> sadd set 'set1' 'set2' 'set3' 'set4' 'set5'
(integer) 5

2.smembers - 返回集合中的所有成员

127.0.0.1:6379> smembers set
1) "set2"
2) "set1"
3) "set4"
4) "set3"
5) "set5"

3.scard - 获取集合的成员数

127.0.0.1:6379> scard set
(integer) 5

4.srandmember - 返回集合中一个或多个随机数

127.0.0.1:6379> srandmember set
"set2"

5.sismember - 判断 member 元素是否是集合 key 的成员

127.0.0.1:6379> sismember set set2
(integer) 1
127.0.0.1:6379> sismember set set6
(integer) 0

6.srem - 移除集合中一个或多个成员

127.0.0.1:6379> smembers set
1) "set2"
2) "set5"
3) "set1"
4) "set3"
5) "set4"
127.0.0.1:6379> srem set 'set1'
(integer) 1
127.0.0.1:6379> smembers set
1) "set2"
2) "set5"
3) "set3"
4) "set4"

7.sdiff - 返回给定所有集合的差集

127.0.0.1:6379> SMEMBERS set
1) "set2"
2) "set5"
3) "set3"
4) "set4"
127.0.0.1:6379> SMEMBERS set1
1) "set5"
2) "set2"
3) "set4"
127.0.0.1:6379> SDIFF set set1
1) "set3"

8.sdiffstore - 返回给定所有集合的差集并存储在 set1 中

SDIFFSTORE destination key1 [key2]
127.0.0.1:6379> sdiffstore set1 set
(integer) 4
127.0.0.1:6379> sdiff set
1) "set5"
2) "set2"
3) "set4"
4) "set3"
127.0.0.1:6379> sdiff set1
1) "set2"
2) "set5"
3) "set3"
4) "set4"

9.sinter - 返回给定所有集合的交集

127.0.0.1:6379> SMEMBERS set
1) "set2"
2) "set5"
3) "set3"
4) "set4"
127.0.0.1:6379> SMEMBERS set1
1) "set5"
2) "set2"
3) "set4"
127.0.0.1:6379> sinter set set1
1) "set5"
2) "set2"
3) "set4"

10.SINTERSTORE - 返回给定所有集合的交集并存储在 set3中

127.0.0.1:6379> SMEMBERS set
1) "set2"
2) "set5"
3) "set3"
4) "set4"
127.0.0.1:6379> SMEMBERS set1
1) "set5"
2) "set2"
3) "set4"
127.0.0.1:6379> 
127.0.0.1:6379> 
127.0.0.1:6379> SINTERSTORE set3 set set1
(integer) 3
127.0.0.1:6379> SMEMBERS set3
1) "set2"
2) "set5"
3) "set4"

11.SUNION - 返回所有给定集合的并集

127.0.0.1:6379> SMEMBERS set
1) "set2"
2) "set5"
3) "set3"
4) "set4"
127.0.0.1:6379> SADD set4 'set6' 'set7' 'set8'
(integer) 3
127.0.0.1:6379> SUNION set set4
1) "set2"
2) "set5"
3) "set7"
4) "set6"
5) "set3"
6) "set4"
7) "set8"

12.SUNIONSTORE - 所有给定集合的并集存储在 set5 集合中

127.0.0.1:6379> SUNIONSTORE set5 set set4
(integer) 7
127.0.0.1:6379> SMEMBERS set5
1) "set2"
2) "set5"
3) "set7"
4) "set6"
5) "set3"
6) "set4"
7) "set8"

13.smove - 将 member 元素从 source 集合移动到 destination 集合

SMOVE source destination member
127.0.0.1:6379> SMEMBERS set
1) "set2"
2) "set5"
3) "set3"
4) "set4"
127.0.0.1:6379> SMEMBERS set1
1) "set5"
2) "set2"
3) "set4"
127.0.0.1:6379> SMOVE set set1 'set3'
(integer) 1
127.0.0.1:6379> SMEMBERS set1
1) "set5"
2) "set2"
3) "set3"
4) "set4"

14.spop - 移除并返回集合中的一个随机元素

127.0.0.1:6379> spop set1
"set5"
127.0.0.1:6379> SMEMBERS set1
1) "set2"
2) "set3"
3) "set4"

15.sscan - 迭代集合中的元素

127.0.0.1:6379> SSCAN set1 0
1) "0"
2) 1) "set2"
   2) "set3"
   3) "set4"

zset

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

127.0.0.1:6379> ZADD zset 1 'one' 2 'two'
(integer) 2
127.0.0.1:6379> ZADD zset 4 'four'
(integer) 1
127.0.0.1:6379> ZRANGE zset 0 10
1) "one"
2) "two"
3) "four"
127.0.0.1:6379> ZRANGE zset 0 10 WITHSCORES
1) "one"
2) "1"
3) "two"
4) "2"
5) "four"
6) "4"

2.ZCARD - 获取有序集合的成员数

127.0.0.1:6379> ZCARD zset
(integer) 3

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

127.0.0.1:6379> ZCOUNT zset 0 10
(integer) 3

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

ZINCRBY key increment member

127.0.0.1:6379> ZRANGE zset 0 10 WITHSCORES
1) "one"
2) "1"
3) "two"
4) "2"
5) "four"
6) "4"
127.0.0.1:6379> ZINCRBY zset 2 'four'
"6"
127.0.0.1:6379> ZRANGE zset 0 10 WITHSCORES
1) "one"
2) "1"
3) "two"
4) "2"
5) "four"
6) "6"

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

127.0.0.1:6379> ZLEXCOUNT zset - +
(integer) 3
127.0.0.1:6379> ZLEXCOUNT zset [o [f
(integer) 0
127.0.0.1:6379> ZLEXCOUNT zset [a [z
(integer) 3

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

127.0.0.1:6379> ZRANGE zset 0 10 WITHSCORES
1) "one"
2) "1"
3) "two"
4) "2"
5) "four"
6) "6"
127.0.0.1:6379> ZRANGE zset 0 10 
1) "one"
2) "two"
3) "four"

7.ZRANGEBYLEX - 通过字典区间返回有序集合的成员

127.0.0.1:6379> ZRANGEBYLEX zset - +
1) "one"
2) "two"
3) "four"
127.0.0.1:6379> 
127.0.0.1:6379> 
127.0.0.1:6379> ZRANGEBYLEX zset [a [f
(empty array)
127.0.0.1:6379> ZRANGEBYLEX zset [a [z
1) "one"
2) "two"
3) "four"

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

127.0.0.1:6379> ZRANGEBYSCORE zset 0 10
1) "one"
2) "two"
3) "four"
127.0.0.1:6379> ZRANGEBYSCORE zset 0 5
1) "one"
2) "two"

9.ZRANK - 返回有序集合中指定成员的索引

127.0.0.1:6379> ZRANK zset one
(integer) 0
127.0.0.1:6379> ZRANK zset two
(integer) 1
127.0.0.1:6379> ZRANK zset four
(integer) 2

10.ZREM - 移除有序集合中的一个或多个成员

127.0.0.1:6379> ZREM zset one
(integer) 1
127.0.0.1:6379> ZRANGE zset 0 10 
1) "two"
2) "four"

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

127.0.0.1:6379> ZRANGE zset 0 10 withscores 
1) "two"
2) "2"
3) "four"
4) "6"
127.0.0.1:6379> ZREMRANGEBYLEX zset [a [z
(integer) 2
127.0.0.1:6379> ZRANGE zset 0 10 withscores 
(empty array)

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

127.0.0.1:6379> ZADD zset 0 one 6 two 5 three 3 four 2 five 4 fix
(integer) 6
127.0.0.1:6379> ZRANGE zset 0 10 withscores 
 1) "one"
 2) "0"
 3) "five"
 4) "2"
 5) "four"
 6) "3"
 7) "fix"
 8) "4"
 9) "three"
10) "5"
11) "two"
12) "6"
127.0.0.1:6379> ZREMRANGEBYRANK zset 0 10
(integer) 6
127.0.0.1:6379> ZRANGE zset 0 10 withscores 
(empty array)
127.0.0.1:6379> 
127.0.0.1:6379> 
127.0.0.1:6379> ZADD zset 0 one 6 two 5 three 3 four 2 five 4 fix
(integer) 6
127.0.0.1:6379> ZREMRANGEBYRANK zset 0 3
(integer) 4
127.0.0.1:6379> ZRANGE zset 0 10 withscores 
1) "three"
2) "5"
3) "two"
4) "6"

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

127.0.0.1:6379> ZADD zset 0 one 6 two 5 three 3 four 2 five 4 fix
(integer) 6
127.0.0.1:6379> ZRANGE zset 0 10 withscores 
 1) "one"
 2) "0"
 3) "five"
 4) "2"
 5) "four"
 6) "3"
 7) "fix"
 8) "4"
 9) "three"
10) "5"
11) "two"
12) "6"
127.0.0.1:6379> ZREMRANGEBYSCORE zset 0 3
(integer) 3
127.0.0.1:6379> ZRANGE zset 0 10 withscores 
1) "fix"
2) "4"
3) "three"
4) "5"
5) "two"
6) "6"

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

127.0.0.1:6379> ZRANGE zset 0 10 withscores 
 1) "one"
 2) "0"
 3) "five"
 4) "2"
 5) "four"
 6) "3"
 7) "fix"
 8) "4"
 9) "three"
10) "5"
11) "two"
12) "6"
127.0.0.1:6379> ZREVRANGE zset 0 10
1) "two"
2) "three"
3) "fix"
4) "four"
5) "five"
6) "one"
127.0.0.1:6379> ZREVRANGE zset 0 10 WITHSCORES
 1) "two"
 2) "6"
 3) "three"
 4) "5"
 5) "fix"
 6) "4"
 7) "four"
 8) "3"
 9) "five"
10) "2"
11) "one"
12) "0"

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

127.0.0.1:6379> ZREVRANGEBYSCORE zset 10 0 WITHSCORES
 1) "two"
 2) "6"
 3) "three"
 4) "5"
 5) "fix"
 6) "4"
 7) "four"
 8) "3"
 9) "five"
10) "2"
11) "one"
12) "0"

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

127.0.0.1:6379> ZREVRANK zset five
(integer) 4

17.ZSCORE - 返回有序集中,成员的分数值

127.0.0.1:6379> ZSCORE zset five
"2"

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

127.0.0.1:6379> ZRANGE zset 0 10
1) "one"
2) "five"
3) "four"
4) "fix"
5) "three"
6) "two"
127.0.0.1:6379> ZRANGE zset1 0 10
1) "one"
2) "two"
3) "nink"
4) "server"
5) "link"
127.0.0.1:6379> ZINTERSTORE zset3 2 zset1 zset
(integer) 2
127.0.0.1:6379> 
127.0.0.1:6379> ZRANGE zset3 0 10
1) "one"
2) "two"

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

127.0.0.1:6379> ZRANGE zset 0 10
1) "one"
2) "five"
3) "four"
4) "fix"
5) "three"
6) "two"
127.0.0.1:6379> ZRANGE zset1 0 10
1) "one"
2) "two"
3) "nink"
4) "server"
5) "link"
127.0.0.1:6379> ZUNIONSTORE zset2 2 zset zset1
(integer) 9
127.0.0.1:6379> 
127.0.0.1:6379> 
127.0.0.1:6379> ZRANGE zset2 0 10
1) "one"
2) "five"
3) "four"
4) "fix"
5) "three"
6) "nink"
7) "server"
8) "link"
9) "two"

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

127.0.0.1:6379> ZSCAN zset2 0
1) "0"
2)  1) "one"
    2) "0"
    3) "five"
    4) "2"
    5) "four"
    6) "3"
    7) "fix"
    8) "4"
    9) "three"
   10) "5"
   11) "nink"
   12) "7"
   13) "server"
   14) "8"
   15) "link"
   16) "9"
   17) "two"
   18) "12"

Hash

1.HSET - 将哈希表 key 中的字段 field 的值设为 value

127.0.0.1:6379> HSET hset name 'lin'
(integer) 1
127.0.0.1:6379> HGETALL hset
1) "name"
2) "lin"

2.HGET - 获取存储在哈希表中指定字段的值

127.0.0.1:6379> HGET hset name
"lin"

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

127.0.0.1:6379> HGETALL hset
1) "name"
2) "lin"

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

127.0.0.1:6379> HEXISTS hset name
(integer) 1
127.0.0.1:6379> HEXISTS hset na
(integer) 0

5.HSETNX - 只有在字段 field 不存在时,设置哈希表字段的值

127.0.0.1:6379> HSETNX hset name 'hello'
(integer) 0
127.0.0.1:6379> HSETNX hset age 18
(integer) 1
127.0.0.1:6379> HGETALL hset
1) "name"
2) "lin"
3) "age"
4) "18"

6.HKEYS - 获取所有哈希表中的字段

127.0.0.1:6379> HKEYS hset
1) "name"
2) "age"

7.HVALS - 获取哈希表中所有值

127.0.0.1:6379> HVALS hset
1) "lin"
2) "18"

8.HLEN - 获取哈希表中字段的数量

127.0.0.1:6379> HLEN hset
(integer) 2

9.HMGET - 获取所有给定字段的值

127.0.0.1:6379> HMGET hset name age
1) "lin"
2) "18"

10.HMSET - 同时将多个 field-value (域-值)对设置到哈希表 key 中

127.0.0.1:6379> HMSET hset gander '男' date '2023-7-21'
OK
127.0.0.1:6379> 
127.0.0.1:6379> HVALS hset
1) "lin"
2) "18"
3) "\xe7\x94\xb7"
4) "2023-7-21"

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

127.0.0.1:6379> HINCRBY hset age 3
(integer) 21
127.0.0.1:6379> HVALS hset
1) "lin"
2) "21"
3) "\xe7\x94\xb7"
4) "2023-7-21"

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

127.0.0.1:6379> HINCRBYFLOAT hset float 1.5
"14"
127.0.0.1:6379> HGETALL hset
 1) "name"
 2) "lin"
 3) "age"
 4) "21"
 5) "gander"
 6) "\xe7\x94\xb7"
 7) "date"
 8) "2023-7-21"
 9) "float"
10) "14"

13.HDEL - 删除一个或多个哈希表字段

127.0.0.1:6379> HDEL hset gander
(integer) 1
127.0.0.1:6379> HGETALL hset
1) "name"
2) "lin"
3) "age"
4) "21"
5) "date"
6) "2023-7-21"
7) "float"
8) "14"

14.HSCAN - 迭代哈希表中的键值对

127.0.0.1:6379> HSCAN hset 1
1) "0"
2) 1) "name"
   2) "lin"
   3) "age"
   4) "21"
   5) "date"
   6) "2023-7-21"
   7) "float"
   8) "14"

Bitmaps

1.setbit

setbit   
127.0.0.1:6379> setbit bit1 10 1
(integer) 0

2.getbit

getbit  
127.0.0.1:6379> GETBIT bit1 10
(integer) 1

3.bitcount

bitcount  [start end]
127.0.0.1:6379> BITCOUNT bit1
(integer) 4

4.bitop

bitop and(or/not/xor)  [key…]
127.0.0.1:6379> bitop and dest lin2
(integer) 20

HyperLogLog

1.PFADD - 添加指定元素到 HyperLogLog 中

PFADD key element [element ...]
127.0.0.1:6379> PFADD hyper 'redis' 'mysql' 'mongodb'
(integer) 1

2.PFCOUNT - 返回给定 HyperLogLog 的基数估算值

PFCOUNT key [key ...]
127.0.0.1:6379> PFCOUNT hyper
(integer) 3

3.PFMERGE - 将多个 HyperLogLog 合并为一个 HyperLogLog

PFMERGE destkey sourcekey [sourcekey ...]
127.0.0.1:6379> PFMERGE hyper hyper1
OK

Geospatial

1.geoadd - 添加地理位置(经度,纬度,名称)

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 114.05 22.52 shenzhen 116.38 39.90 beijing
(integer) 3

2.geopos - 获得指定地区的坐标值

geopos  key  member  [member...]
127.0.0.1:6379> GEOPOS china:city shanghai chongqing shenzhen beijing
1) 1) "121.47000163793563843"
   2) "31.22999903975783553"
2) 1) "106.49999767541885376"
   2) "29.52999957900659211"
3) 1) "114.04999762773513794"
   2) "22.5200000879503861"
4) 1) "116.38000041246414185"
   2) "39.90000009167092543"

3.geodist - 获取两个位置之间的直线距离

geodist  key  member1  member2 [m\|km\|ft\|mi]
127.0.0.1:6379> GEODIST china:city shanghai chongqing
"1447673.6920"

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

georadius  key   longitude  latitude  radius  [m\|km\|ft\|mi]
127.0.0.1:6379> georadius china:city 110 30 1000 km
1) "chongqing"
2) "shenzhen"

redis持久化:分别启用rdb和aof,并查看是否有对应文件生成

rdb的启用

1.配置文件名

在redis.conf中配置

# The filename where to dump the DB
dbfilename dump.rdb

2.配置文件路径

在redis.conf中配置

# Note that you must specify a directory here, not a file name.
# dir ./
dir '/usr/local/redis/data/' # 文件配置路径

3.save

格式:save 秒钟 写操作次数

save 900 1                       # 在900s内如果有1条数据被写入,则产生一次快照。
save 300 10                      # 在300s内如果有10条数据被写入,则产生一次快照
save 60 10000                    # 在60s内如果有10000条数据被写入,则产生一次快照

stop-writes-on-bgsave-error yes  # 如果为yes则表示,当备份进程出错的时候,主进程就停止进行接受新的写入操作,这样是为了保护持久化的数据一致性的问题。

4.修改好的保存并重启 redis 服务

aof的启用

在redis.conf中配置

# appendonly no 
appendonly yes # 修改为yes

配置选项

选项 同步频率
always 每个redis写命令都要同步写入硬盘,严重降低redis速度
everysec 每秒执行一次同步显式的将多个写命令同步到磁盘
no 由操作系统决定何时同步

你可能感兴趣的:(数据库,redis,bootstrap,数据库)