redis数据类型与持久化

redis9种数据类型的基本操作:

        KEY省略

1.string

  以键值对的形式存在,最大储存512m

设值、修改、删除

127.0.0.1:6379> set test1 10              
OK
127.0.0.1:6379> get test1
"10"
127.0.0.1:6379> getset test1 20
"10"
127.0.0.1:6379> get test1
"20"

127.0.0.1:6379> incrby test1 10
(integer) 30
127.0.0.1:6379> get test1
"30"
127.0.0.1:6379> decrby test1 10
(integer) 20
127.0.0.1:6379> get test1
"20"

27.0.0.1:6379> del test1
(integer) 1

设置过期时间

127.0.0.1:6379> setex test2 10 10
OK
127.0.0.1:6379> get test2
(nil)

验证存在与否

127.0.0.1:6379> setnx test1 100
(integer) 0
127.0.0.1:6379> setnx test2 100
(integer) 1

2.List

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

设值、修改、删除、获取长度

27.0.0.1:6379> lpush test1 1 2 3
(integer) 3
127.0.0.1:6379> lrange test 0 2
(empty array)
127.0.0.1:6379> lrange test1 0 2
1) "3"
2) "2"
3) "1"
127.0.0.1:6379> lpop test1 1
1) "3"
127.0.0.1:6379> lrange test 0 -1
(empty array)
127.0.0.1:6379> lrange test1 0 -1
1) "2"
2) "1"
127.0.0.1:6379> lpushx test1 9
(integer) 3
127.0.0.1:6379> lrange test1 0 -1
1) "9"
2) "2"
3) "1"
127.0.0.1:6379> rpush test1 8
(integer) 4
127.0.0.1:6379> lrange test1 0 -1
1) "9"
2) "2"
3) "1"
4) "8"
127.0.0.1:6379> llen test1
(integer) 4
127.0.0.1:6379> ltrim test1 0 -1
OK
127.0.0.1:6379> lrange test1
(error) ERR wrong number of arguments for 'lrange' command
127.0.0.1:6379> lrange test1 0 -1
1) "9"
2) "2"
3) "1"
4) "8"
127.0.0.1:6379> del test1
(integer) 1

3.Set

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

设值、查看并集和交集、删除值、检验

127.0.0.1:6379> sadd test1 a b c
(integer) 3
127.0.0.1:6379> smembers test1
1) "a"
2) "c"
3) "b"
127.0.0.1:6379> scard test1
(integer) 3
127.0.0.1:6379> sismember test1
(error) ERR wrong number of arguments for 'sismember' command
127.0.0.1:6379> sismember test1 a
(integer) 1
127.0.0.1:6379> sadd test2 b c d
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> sadd test2 b c d e
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> sadd test3 b c d e
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> del test2
(integer) 1
127.0.0.1:6379> sadd test2 b c d e
(integer) 4
127.0.0.1:6379> sinter test1 test2
1) "c"
2) "b"
127.0.0.1:6379> sunion test1 test2
1) "c"
2) "b"
3) "a"
4) "e"
5) "d"
127.0.0.1:6379> spop test1 a
(error) ERR value is out of range, must be positive
127.0.0.1:6379> spop test1 1
1) "a"
127.0.0.1:6379>

4.Zset

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

设值、查询值、删除值

127.0.0.1:6379> zadd test1 10 'a' 20 'b' 30 'c'
(integer) 3
127.0.0.1:6379> zcard test1
(integer) 3
127.0.0.1:6379> zcount 10 30
(error) ERR wrong number of arguments for 'zcount' command
127.0.0.1:6379> zcount test1 10 30
(integer) 3
127.0.0.1:6379> zincrby test1 40 'd'
"40"
127.0.0.1:6379> zrange test1 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
127.0.0.1:6379> zrange test1 0 -1 withscores
1) "a"
2) "10"
3) "b"
4) "20"
5) "c"
6) "30"
7) "d"
8) "40"
127.0.0.1:6379> zrem test1 'a'
(integer) 1
127.0.0.1:6379>

5.Hash

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

设值、查询、验证、删除值

127.0.0.1:6379> hset test1 id 1 name xiaoming gender 'm'
(integer) 3
127.0.0.1:6379> hget test1 id
"1"
127.0.0.1:6379> hgetall test1
1) "id"
2) "1"
3) "name"
4) "xiaoming"
5) "gender"
6) "m"
127.0.0.1:6379> hexisit test1 id
(error) ERR unknown command 'hexisit', with args beginning with: 'test1' 'id'
127.0.0.1:6379> hexisit test1 1
(error) ERR unknown command 'hexisit', with args beginning with: 'test1' '1'
127.0.0.1:6379> hexists test1 1
(integer) 0
127.0.0.1:6379> hkeys test1
1) "id"
2) "name"
3) "gender"
127.0.0.1:6379> hvals test1
1) "1"
2) "xiaoming"
3) "m"
127.0.0.1:6379> hdel test1 id
(integer) 1
127.0.0.1:6379>

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 不能像集合那样,返回输入的各个元素。
 

设值、查看个数、合并

127.0.0.1:6379> pfadd test1 id 1
(integer) 1
127.0.0.1:6379> pfcount test1
(integer) 2
127.0.0.1:6379> del test2
(integer) 1
127.0.0.1:6379> pfadd test2 id 2
(integer) 1
127.0.0.1:6379> pfmerge test1 test2
OK
127.0.0.1:6379> pfcount test1
(integer) 3
127.0.0.1:6379>

8.Geospatial

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

redis持久化:

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

路径:/etc/redis.conf

RDB:

查看生成文件:

 

AOF:

在redis.conf文件中:

查看文件:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值