Redis data type operation and persistence

Table of contents

1. Data type

1、String

 basic operation

2、List

basic operation 

3、Set

basic operation

4、Set

basic operation 

5、Hash

basic operation

6、Bitmaps

7、HyperLogLog

basic operation 

8、Geospatial

basic operation 

2. Redis data persistence

1、RDB

2、AOF


1. Data type

1、String

The String type is the most basic data type of Redis. A key corresponds to a value. The value of the String type can store up to 512MB.

The String type is generally used for caching, current limiting, counters, distributed locks, and distributed sessions.

serial number command syntax describe
1 SET key value Set the value of the specified key
2 GET key Get the value of the specified key
3 GETRANGE key start end Returns the subcharacters of the string value in the key, when end=-1 means all
4 SETBIT key offset value For the string value stored by key, set or clear the bit at the specified offset
5 GETBIT key offset For the string value stored by key, get the bit at the specified offset (bit)
6 MSET key value [key value ...] Set one or more key-value pairs at the same time
7 MGET key1 [key2..] Get all (one or more) values ​​for a given key
8 GETSET key value Set the value of the given key to value and return the old value of the key (old value)
9 SETEX key seconds value Associate the value value with the key, and set the expiration time of the key to seconds (in seconds)
10 SETNX key value Only set the value of the key if the key does not exist
11 SETRANGE key offset value Overwrites the string value stored at the given key with the value parameter, starting at offset offset
12 STRLEN key Returns the length of the string value stored by key
13 MSETNX key value [key value ...] Simultaneously set one or more key-value pairs if and only if none of the given keys exist
14 PSETEX key milliseconds value Similar to the SETEX command, but it sets the key's lifetime in milliseconds
15 INCR key Increment the numeric value stored in key by one
16 INCRBY key increment Add the value stored by the key to the given increment value (increment)
17 INCRBYFLOAT key increment Add the value stored by the key to the given floating point increment value (increment)
18 DECR key Decrease the numeric value stored in the key by one
19 DECRBY key decrement The value stored by the key minus the given decrement value (decrement)
20 APPEND key value If the key already exists and is a string, the APPEND command will append the specified value to the end of the original value of the key

 basic operation

127.0.0.1:6379> set k1 v1               #设置k1的值
OK
127.0.0.1:6379> get k1                  #获取k1的值
"v1"
127.0.0.1:6379> mset k2 v2 k3 v3        #同时设置k2,k3的值
OK
127.0.0.1:6379> mget k2 k3              #获取k2,k3的值    
1) "v2"
2) "v3"
127.0.0.1:6379> getset k1 v11           #更改k1的值,并返回k1的旧值
"v1"
127.0.0.1:6379> setex k4 10 v4          #设置k4的值,并将k4的过期时间设为10秒
OK
127.0.0.1:6379> get k4                  #10秒后获取k4的值,为空
(nil)
127.0.0.1:6379> setnx k4 v4             #当k4不存在时,设置k4的值,设置成功
(integer) 1
127.0.0.1:6379> setnx k1 v1             #k1存在,设置失败
(integer) 0
127.0.0.1:6379> get k4
"v4"
127.0.0.1:6379> get k1
"v11"
127.0.0.1:6379> set k5 100
OK
127.0.0.1:6379> incr k5                 #将k5中储存的数值加1
(integer) 101
127.0.0.1:6379> get k5
"101"
127.0.0.1:6379> incrby k5 19            #将k5中储存的数值加19
(integer) 120
127.0.0.1:6379> get k5
"120"
127.0.0.1:6379> decr k5                 #将k5中储存的数值减1
(integer) 119
127.0.0.1:6379> get k5
"119"
127.0.0.1:6379> decrby k5 19            #将k5中储存的数值减19
(integer) 100
127.0.0.1:6379> get k5
"100"
127.0.0.1:6379> append k1 v22           #如果 key 已经存在并且是一个字符串,APPEND 命令将指                
(integer) 6                             #定的 value 追加到该 key 原来值 value 的末尾
127.0.0.1:6379> get k1
"v11v22"

2、List

Redis lists are simply lists of strings, sorted by insertion order. You can add an element to the head (left) or tail (right) of the list. A list can contain up to 2^32^ - 1 elements (4294967295, each list is over 4 billion elements).

The List type is generally used for following people, simple queues, etc.

serial number command syntax describe
1 LPUSH key value1 [value2] Insert one or more values ​​at the head of the list
2 LPOP key Move out and get the first element of the list
3 LRANGE key start stop Get the elements in the specified range of the list
4 LPUSHX key value Insert a value at the head of an existing list
5 RPUSH key value1 [value2] Add one or more values ​​to the list
6 RPOP key Remove the last element of the list, the return value is the removed element
7 RPUSHX key value Add value to an existing list
8 LLEN key get list length
9 LINSERT key BEFORE|AFTER pivot value Insert an element before or after an element in a list
10 LINDEX key index Get an element in a list by index
11 LSET key index value Set the value of a list element by index
12 LREM key count value remove list element
13 LTRIM key start stop To trim a list is to keep the list only the elements within the specified range, and the elements not within the specified range will be deleted
14 BLPOP key1 [key2 ] timeout Move out and get the first element of the list. If there is no element in the list, the list will be blocked until the wait times out or a pop-up element is found
15 BRPOP key1 [key2 ] timeout Move out and get the last element of the list. If there is no element in the list, the list will be blocked until the wait times out or a pop-up element is found
16 BRPOPLPUSH source destination timeout Pop a value from the list, insert the popped element into another list and return it; if there is no element in the list, the list will be blocked until the wait times out or an element that can be popped by lpu is found
17 RPOPLPUSH source destination Removes the last element of a list and adds that element to another list and returns

basic operation 

127.0.0.1:6379> lpush k1 1 2 3 4 5            #将一个或多个值插入到列表头部
(integer) 5
127.0.0.1:6379> lrange k1 0 -1                #获取k1内的元素,(0,-1)代表所有
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379> lpop k1 2                     #移出k1前2个元素,并返回该值
1) "5"
2) "4"
127.0.0.1:6379> lrange k1 0 -1
1) "3"
2) "2"
3) "1"
127.0.0.1:6379> lpushx k1 4 5                 #将4,5插入到已存在的k1键头部
(integer) 5
127.0.0.1:6379> lrange k1 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379> rpush k1 6 7                  #将6,7添加到k1尾部
(integer) 7
127.0.0.1:6379> lrange k1 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
6) "6"
7) "7"
127.0.0.1:6379> rpop k1 2                     #移出k1尾部2个元素
1) "7"
2) "6"
127.0.0.1:6379> lrange k1 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379> llen k1                       #获取k1的长度
(integer) 5
127.0.0.1:6379> ltrim k1 1 -2                 #删除在范围(1,-2)以外的元素
OK
127.0.0.1:6379> lrange k1 0 -1
1) "4"
2) "3"
3) "2"


3、Set

Redis's Set is an unordered collection of String types. The members in the set are unique, which means that there cannot be duplicate data in the set. Collections in Redis are implemented through hash tables, so the complexity of adding, deleting, and searching is O(1). The maximum number of members in a set is 2^32^ - 1 (4294967295, each set can store more than 4 billion members).

The Set type is generally used for likes, dislikes, tags, friendships, etc.

serial number command syntax describe
1 SADD key member1 [member2] Add one or more members to the set
2 SMEMBERS key Returns all members of the collection
3 SCARD key Get the number of members of a collection
4 SRANDMEMBER key [count] Returns one or more random numbers in the collection
5 SISMEMBER key member Determine whether the member element is a member of the collection key
6 SREM key member1 [member2] Remove one or more members from the set
7 SDIFF key1 [key2] Returns the difference of all sets given
8 SDIFFSTORE destination key1 [key2] 返回给定所有集合的差集并存储在 destination 中
9 SINTER key1 [key2] 返回给定所有集合的交集
10 SINTERSTORE destination key1 [key2] 返回给定所有集合的交集并存储在 destination 中
11 SUNION key1 [key2] 返回所有给定集合的并集
12 SUNIONSTORE destination key1 [key2] 所有给定集合的并集存储在 destination 集合中
13 SMOVE source destination member 将 member 元素从 source 集合移动到 destination 集合
14 SPOP key 移除并返回集合中的一个随机元素
15 SSCAN key cursor [MATCH pattern] [COUNT count] 迭代集合中的元素

基础操作

127.0.0.1:6379> sadd k1 a b c d e            #向k1集合添加成员
(integer) 5
127.0.0.1:6379> SMEMBERS k1                  #查看k1中所有的成员
1) "d"
2) "a"
3) "c"
4) "b"
5) "e"
127.0.0.1:6379> SCARD k1                     #获取集合的成员数
(integer) 5
127.0.0.1:6379> SISMEMBER k1 f               #查看f是否是k1中的成员(0代表否,1代表是)
(integer) 0
127.0.0.1:6379> SISMEMBER k1 e
(integer) 1
127.0.0.1:6379> sadd k2 a c e f g
(integer) 5
127.0.0.1:6379> SDIFF k1 k2                  #返回k1与k2的差集,k1在前,k2在后,返回的是k2
1) "d"                                       #中没有k1的成员,这里返回k2中没有k1的b、d
2) "b"
127.0.0.1:6379> SDIFF k2 k1
1) "f"
2) "g"
127.0.0.1:6379> SINTER k1 k2                 #返回k1、k2的交集
1) "a"
2) "c"
3) "e"
127.0.0.1:6379> SUNION k1 k2                 #返回k1、k2的并集
1) "g"
2) "a"
3) "c"
4) "e"
5) "d"
6) "f"
7) "b"
127.0.0.1:6379> spop k1 1                    #随机删除k1中的一个成员并返回该值
1) "b"
127.0.0.1:6379> spop k1 1
1) "d"

4、Zset

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

集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。 集合中最大的成员数为 2^32^ - 1 (4294967295, 每个集合可存储40多亿个成员)。

Zset类型一般用于排行榜等。

序号 命令语法 描述
1 ZADD key score1 member1 [score2 member2] 向有序集合添加一个或多个成员,或者更新已存在成员的分数
2 ZCARD key 获取有序集合的成员数
3 ZCOUNT key min max 计算在有序集合中指定区间分数的成员数
4 ZINCRBY key increment member 有序集合中对指定成员的分数加上增量 increment
5 ZLEXCOUNT key min max 在有序集合中计算指定字典区间内成员数量
6 ZRANGE key start stop [WITHSCORES] 通过索引区间返回有序集合指定区间内的成员
7 ZRANGEBYLEX key min max [LIMIT offset count] 通过字典区间返回有序集合的成员
8 ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] 通过分数返回有序集合指定区间内的成员
9 ZRANK key member 返回有序集合中指定成员的索引
10 ZREM key member [member ...] 移除有序集合中的一个或多个成员
11 ZREMRANGEBYLEX key min max 移除有序集合中给定的字典区间的所有成员
12 ZREMRANGEBYRANK key start stop 移除有序集合中给定的排名区间的所有成员
13 ZREMRANGEBYSCORE key min max 移除有序集合中给定的分数区间的所有成员
14 ZREVRANGE key start stop [WITHSCORES] 返回有序集中指定区间内的成员,通过索引,分数从高到低
15 ZREVRANGEBYSCORE key max min [WITHSCORES] 返回有序集中指定分数区间内的成员,分数从高到低排序
16 ZREVRANK key member 返回有序集合中指定成员的排名,有序集成员按分数值递减(从大到小)排序
17 ZSCORE key member 返回有序集中,成员的分数值
18 ZINTERSTORE destination numkeys key [key ...] 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中
19 ZUNIONSTORE destination numkeys key [key ...] 计算给定的一个或多个有序集的并集,并存储在新的 key 中
20 ZSCAN key cursor [MATCH pattern] [COUNT count] 迭代有序集合中的元素(包括元素成员和元素分值)

基础操作 

127.0.0.1:6379> zadd k1 60 'zhangsan' 70 'lisi' 80 'wangwu' 90 'xiaohong'
(integer) 4                                     #添加成绩以及成员
127.0.0.1:6379> ZCARD k1                        #返回k1的成员数
(integer) 4
127.0.0.1:6379> ZCOUNT k1 60 80                 #返回指定分数返回的成员数
(integer) 3
127.0.0.1:6379> ZINCRBY k1 5 'zhangsan'         #增加并返回指定成员的指定分数
"65"
127.0.0.1:6379> ZRANGE k1 0 -1                  #返回索引区间内的成员
1) "zhangsan"
2) "lisi"
3) "wangwu"
4) "xiaohong"
127.0.0.1:6379> ZRANGE k1 0 -1 withscores       #返回索引区间内的成员及分数
1) "zhangsan"
2) "65"
3) "lisi"
4) "70"
5) "wangwu"
6) "80"
7) "xiaohong"
8) "90"
127.0.0.1:6379> zrem k1 'xiaohong'              #删除k1中的成员以及分数
(integer) 1
127.0.0.1:6379> ZREVRANK k1 'zhangsan'          #返回有序集合中指定成员的排名,有序集成员按
(integer) 2                                     #分数值递减(从大到小)排序
127.0.0.1:6379> ZREVRANK k1 'wangwu'
(integer) 0
127.0.0.1:6379> ZSCORE k1 'lisi'                #返回k1中,成员的分数值
"70"

5、Hash

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

Hash类型一般用于存储用户信息、用户主页访问量、组合查询等。

序号 命令语法 描述
1 HSET key field value 将哈希表 key 中的字段 field 的值设为 value
2 HGET key field 获取存储在哈希表中指定字段的值
3 HGETALL key 获取在哈希表中指定 key 的所有字段和值
4 HEXISTS key field 查看哈希表 key 中,指定的字段是否存在
5 HSETNX key field value 只有在字段 field 不存在时,设置哈希表字段的值
6 HKEYS key 获取所有哈希表中的字段
7 HVALS key 获取哈希表中所有值
8 HLEN key 获取哈希表中字段的数量
9 HMGET key field1 [field2] 获取所有给定字段的值
10 HMSET key field1 value1 [field2 value2] 同时将多个 field-value (域-值)对设置到哈希表 key 中
11 HINCRBY key field increment 为哈希表 key 中的指定字段的整数值加上增量 increment
12 HINCRBYFLOAT key field increment 为哈希表 key 中的指定字段的浮点数值加上增量 increment
13 HDEL key field1 [field2] 删除一个或多个哈希表字段
14 HSCAN key cursor [MATCH pattern] [COUNT count] 迭代哈希表中的键值对

基础操作

127.0.0.1:6379> hset k1 id 1 name 'zhangsan' gender 'M' 
(integer) 3                                       #设置k1中field和value的值
127.0.0.1:6379> hget k1 name                      #获取k1中name的值
"zhangsan"
127.0.0.1:6379> HGETALL k1                        #获取k1所有的field以及值
1) "id"
2) "1"
3) "name"
4) "zhangsan"
5) "gender"
6) "M"
127.0.0.1:6379> HEXISTS k1 birth                  #判断k1中是否存在birth字段
(integer) 0
127.0.0.1:6379> HEXISTS k1 id
(integer) 1
127.0.0.1:6379> hsetnx k1 name 'lisi'             #当name字段不存在时,才设置该字段以及值,
(integer) 0                                       #当name字段存在时,对原数据不影响
127.0.0.1:6379> HGETALL k1
1) "id"
2) "1"
3) "name"
4) "zhangsan"
5) "gender"
6) "M"
7) "birth"
8) "2000-01-01"
127.0.0.1:6379> hkeys k1                          #获取k1中的所有字段
1) "id"
2) "name"
3) "gender"
4) "birth"
127.0.0.1:6379> HVALS k1                          #获取k1中的所有值
1) "1"
2) "zhangsan"
3) "M"
4) "2000-01-01"
127.0.0.1:6379> hlen k1                           #获取k1中字段的数量
(integer) 4
127.0.0.1:6379> hmget k1 name id                  #获取指定字段的值
1) "zhangsan"
2) "1"
127.0.0.1:6379> HINCRBY k1 id 2                   #对k1中的id字段加2
(integer) 3
127.0.0.1:6379> hget k1 id
"3"
127.0.0.1:6379> hdel k1 birth                     #删除k1中的birth字段
(integer) 1
127.0.0.1:6379> hkeys k1
1) "id"
2) "name"
3) "gender"

 6、Bitmaps

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

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

7、HyperLogLog

在工作当中,我们经常会遇到与统计相关的功能需求,比如统计网站PV(PageView页面访问量),可以使用Redis的incr、incrby轻松实现。

但像UV(UniqueVisitor,独立访客)、独立IP数、搜索记录数等需要去重和计数的问题如何解决?这种求集合中不重复元素个数的问题称为基数问题。

什么是基数?

比如数据集 {1, 3, 5, 7, 5, 7, 8},那么这个数据集的基数集为 {1, 3, 5 ,7, 8},基数(不重复元素)为5。 基数估计就是在误差可接受的范围内,快速计算基数。

解决基数问题有很多种方案:

1)数据存储在MySQL表中,使用distinct count计算不重复个数

2)使用Redis提供的hash、set、bitmaps等数据结构来处理

以上的方案结果精确,但随着数据不断增加,导致占用空间越来越大,对于非常大的数据集是不切实际的。

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

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

在 Redis 里面,每个 HyperLogLog 键只需要花费 12 KB 内存,就可以计算接近 2^64 个不同元素的基数。这和计算基数时,元素越多耗费内存就越多的集合形成鲜明对比。

但是,因为 HyperLogLog 只会根据输入元素来计算基数,而不会储存输入元素本身,所以 HyperLogLog 不能像集合那样,返回输入的各个元素。

序号 命令语法 描述
1 PFADD key element [element ...] 添加指定元素到 HyperLogLog 中
2 PFCOUNT key [key ...] 返回给定 HyperLogLog 的基数估算值
3 PFMERGE destkey sourcekey [sourcekey ...] 将多个 HyperLogLog 合并为一个 HyperLogLog

基础操作 

127.0.0.1:6379> PFADD course "redis"            #添加指定元素
(integer) 1
127.0.0.1:6379> PFADD course "mongodb"
(integer) 1
127.0.0.1:6379> PFADD course "mysql"
(integer) 1
127.0.0.1:6379> PFADD course "redis"
(integer) 0
redis 127.0.0.1:6379> PFCOUNT course            #返回给定 HyperLogLog 的基数估算值
(integer) 3
127.0.0.1:6379> PFADD course1 "redis"
(integer) 0
redis 127.0.0.1:6379> pfmerge course course1    #将多个 HyperLogLog 合并为一个 HyperLogLog
(integer) 1
redis 127.0.0.1:6379> PFCOUNT course
(integer) 3

8、Geospatial

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

序号 命令语法 描述
1 geoadd key longitude latitude member [longitude latitude member...] 添加地理位置(经度,纬度,名称)
2 geopos key member [member...] 获得指定地区的坐标值
3 geodist key member1 member2 [m|km|ft|mi] 获取两个位置之间的直线距离
4 georadius key longitude latitude radius [m|km|ft|mi] 以给定的经纬度为中心,找出某一半径内的元素

基础操作 

127.0.0.1:6379> geoadd china:city 121.47 31.23 shanghai
127.0.0.1:6379> geoadd china:city 106.50 29.53 chongqing 114.05 22.52 shenzhen 116.38 39.90                     
                beijing
127.0.0.1:6379> geopos china:city beijing shanghai km    #获取两城市之间的直线距离

#有效的经度从 -180 度到 180 度。有效的纬度从 -85.05112878 度到 85.05112878 度。
当坐标位置超出指定范围时,该命令将会返回一个错误。
已经添加的数据,是无法再次往里面添加的。

二、Redis数据持久化

Redis的配置文件默认是安装目录下的redis.conf文件,我们可以使用cp命令把配置文件移动到/etc/目录下

1、RDB

RDB 方式适合大规模的数据恢复,并且对数据完整性和一致性要求不高更适合使用。

优势:

  • 节省磁盘空间
  • 恢复速度快

劣势

  • Fork的时候,内存中的数据被克隆了一份,大致2倍的膨胀性需要考虑
  • 虽然Redis在fork时使用了写时拷贝技术,但是如果数据庞大时还是比较消耗性能。
  • 在备份周期在一定间隔时间做一次备份,所以如果Redis意外down掉的话,就会丢失最后一次快照后的所有修改。

vim /etc/redis.conf

save "" 代表RDB备份方式关闭,以下是配置示例

save 900 1                       # 在900s内如果有1条数据被写入,则产生一次快照。
save 300 10                      # 在300s内如果有10条数据被写入,则产生一次快照
save 60 10000                    # 在60s内如果有10000条数据被写入,则产生一次快照
stop-writes-on-bgsave-error yes  # 如果为yes则表示,当备份进程出错的时候,主进程就停止进行接受新的写入操作,这样是为了保护持久化的数据一致性的问题。

我们把持久化的策略修改为 save 30 5,表示 30 秒内写入 5 条数据就产生一次快照,也就是生成 rdb 文件。设置后使用systemctl restart redis命令重启服务

我们发现在默认安装包路径下,有了dump.rdb文件

2、AOF

以日志的形式来记录每个写操作(增量保存),将Redis执行过的所有写指令记录下来(读操作不记录), 只追加文件但不可以改写文件,Redis启动之初会读取该文件重新构建数据。简单说,Redis 重启时会根据日志文件的内容将写指令从前到后执行一次以完成数据的恢复工作。

在Redis的默认配置中AOF(Append Only File)持久化机制是没有开启的,要想使用AOF持久化需要先开启此功能。AOF持久化会将被执行的写命令写到AOF文件末尾,以此来记录数据发生的变化,因此只要Redis从头到尾执行一次AOF文件所包含的所有写命令,就可以恢复AOF文件的记录的数据集。

修改 redis.conf 配置文件:

  • 通过修改redis.conf配置中appendonly yes来开启AOF持久化

  • 通过appendfilename指定日志文件名字(默认为appendonly.aof)

  • 通过appendfsync指定日志记录频率

 重启服务后,我们发现默认安装路径下多了appendonlydir目录

 

进入redis,设置多个键,让备份文件有记录,然后退出杀死redis进程,重启服务

 这时候再查看appendonly.aof.1.incr.aof文件

*n代表一共有两部分

$n代表一共几个字符

Guess you like

Origin blog.csdn.net/CQ17743254852/article/details/131826767