Redis使用:聚合类型为空时,会自动被Redis删除
项目中使用Redis来记录用户的上线和下线信息,其中用到了集合(sets)类型,某用户上线时,向sets中添加数据,下线时将相应数据从sets中删除,考虑当该用户的所有实例都下线时,需要将sets删除。
因为是并发操作,就考虑到判断为空后进行删除操作的原子性,查了一堆资料,都已经写好了相应的lua脚本(因为Redis执行lua脚本能保证原子性):
if redis.call('scard',KEYS[]) ==
then
return redis.call('del',KEYS[])
else
return
end
结果后来发现Redis中已经自动实现了该功能,也就是所有聚合类型:lists,sets, Sorted Sets 和 Hashes,当为空时,都会被自动删除!
服!!!
Automatic creation and removal of keys
So far in our examples we never had to create empty lists before pushing elements, or removing empty lists when they no longer have elements inside. It is Redis' responsibility to delete keys when lists are left empty, or to create an empty list if the key does not exist and we are trying to add elements to it, for example, with LPUSH.
This is not specific to lists, it applies to all the Redis data types composed of multiple elements -- Sets, Sorted Sets and Hashes.
Basically we can summarize the behavior with three rules:
- When we add an element to an aggregate data type, if the target key does not exist, an empty aggregate data type is created before adding the element.
- When we remove elements from an aggregate data type, if the value remains empty, the key is automatically destroyed.
- Calling a read-only command such as LLEN (which returns the length of the list), or a write command removing elements, with an empty key, always produces the same result as if the key is holding an empty aggregate type of the type the command expects to find.
Examples of rule 1:
> del mylist
(integer) 1
> lpush mylist 1 2 3
(integer) 3
However we can't perform operations against the wrong type if the key exists:
> set foo bar
OK
> lpush foo 1 2 3
(error) WRONGTYPE Operation against a key holding the wrong kind of value
> type foo
string
Example of rule 2:
> lpush mylist 1 2 3
(integer) 3
> exists mylist
(integer) 1
> lpop mylist
"3"
> lpop mylist
"2"
> lpop mylist
"1"
> exists mylist
(integer) 0
The key no longer exists after all the elements are popped.
Example of rule 3:
> del mylist
(integer) 0
> llen mylist
(integer) 0
> lpop mylist
(nil)
https://redis.io/topics/data-types-intro
Redis使用:聚合类型为空时,会自动被Redis删除的更多相关文章
- sql server 之函数小技巧 && 整数类型为空是用空字符串替代实现
1.判空函数 说明:使用指定的替换值替换 NULL. 语法:ISNULL ( check_expression , replacement_value ) 参数: check_expression:将 ...
- mybatis中查询结果为空时不同返回类型对应返回值
今天在别人的代码基础上实现新需求,看到对于mybatis查询结果的判断不是很正确,如果查询结果为空就会异常,不知道大家有没有这样的疑惑:mybatis中resultType有多种返回类型,对于每种不同 ...
- json-lib反序列化时(JSONObject.toBean),时间类型为空的处理
需求: 在我们的项目里希望JsonString传入日期类型值为空时,JSONObject.toBean时可以将Java对象的该日期属性设为null. 解决过程: json-lib反序列化Json字符串 ...
- redis的set类型!!!!
一.概述 在Redis中,我们可以将Set类型看作为没有排序的字符集合,和List类型一样,我们也可以在该类型的数据值上执行添加.删除或判断某一元素是否存在等操作.需要说明的是,这些操作的时间复杂度为 ...
- 直接在安装了redis的Linux机器上操作redis数据存储类型--set类型
一.概述: 在Redis中,我们可以将Set类型看作为没有排序的字符集合,和List类型一样,我们也可以在该类型的数据值上执行添加.删除或判断某一元素是否存在等操作.需要说明的是,这些操作的时间复 ...
- 【C语言入门教程】2.1 数据类型(5种基本数据类型),聚合类型与修饰符
C语言有5种基本的数据类型,分别为 字符型.整型.单精度浮点型.双精度浮点型.空类型. 在不同的操作系统或硬件平台中,这些数据类型的值域范围和所占用的内存是有差异的.这种差异影响了C语言的可移植性能, ...
- redis的list类型
1.简单介绍 redis的list类型其实就是一个每个元素都是string类型的双向链表.所以lpush.rpush.lpop和rpop命令的时间复杂度是O(1),list会记录链表的长度,所以lle ...
- redis数据类型-字符串类型
Redis数据类型 字符串类型 字符串类型是Redis中最基本的数据类型,它能存储任何形式的字符串,包括二进制数据.你可以用其存储用户的邮箱.JSON化的对象甚至是一张图片.一个字符串类型键允许存储的 ...
- 第二百九十五节,python操作redis缓存-字符串类型
python操作redis缓存-字符串类型 首先要安装redis-py模块 python连接redis方式,有两种连接方式,一种是直接连接,一张是通过连接池连接 注意:以后我们都用的连接池方式连接,直 ...
随机推荐
- jmeter-测试https请求
找了一个HTTPS请求的网站尝试. 我用的是chrome,点这个小锁(如果是IE也可以在网页上右键,属性,高级,证书) 或 看到下图,点击“复制到文件” 或 把导出的证书打成.store 用此命令进行 ...
- 批量处理数据 SqlBulkCopy
string connectionString = new PublicDBHelper().GetCon(System.Configuration.ConfigurationManager.AppS ...
- Shuffle过程详解
- NOIP2018游记 & 退役记
NOIP2018游记 & 退役记 我是一名来自湖北武汉华中师大一附中的高二\(OIer\),学习\(OI\)一年,今年去参加\(NOIP\),然后退役.这是一篇\(NOIP2018\)的游记, ...
- Luogu P2822 组合数问题(前缀和)
P2822 组合数问题 题意 题目描述 组合数\(C_n^m\)表示的是从\(n\)个物品中选出\(m\)个物品的方案数.举个例子,从\((1,2,3)\)三个物品中选择两个物品可以有\((1,2), ...
- js把时间转化为 ‘2019-07-01’ 格式
将new Date()数据转化为‘2019-07-01’格式 //时间 function formatDate(date) { var y = date.getFullYear(); ; m = m ...
- 【JZOJ3318】Brunhilda的生日
description 除去对铁质盔甲强烈的热爱,Brunhilda是一个正常的7岁女孩.近期,她正在策划一个完美的生日派对.她发明了如下的一个游戏:所有的孩子在一个数k被宣读之前不停地跑来跑去.当这 ...
- window 批量修改或去除文件后缀名
for /r %i in (*.!ut) do ren "%i" *. 转自:https://blog.csdn.net/zhang_ruisen/article/details/ ...
- CSS自动换行、强制不换行、强制断行、超出显示省略号
转自:https://blog.csdn.net/liuyan19891230/article/details/50969393 P标签是默认是自动换行的,因此设置好宽度之后,能够较好的实现效果,但是 ...
- 大数据、AI“武装”企业服务:风控、检索、安全
大数据.AI“武装”企业服务:风控.检索.安全 小饭桌创业课堂2017-05-06 15:26:42阅读(127)评论(0) + - 文|吴杨可月 - - 小饭桌创业研究院出品 - 两件秘闻,将美国大 ...