关于redis使用set时设置超时时间的问题
来源:互联网 发布:三菱plc步进编程实例 编辑:程序博客网 时间:2024/05/18 03:34
redis的官网(http://redis.io)中是这样描述的:
SET key value [EX seconds] [PX milliseconds] [NX|XX]
Set key
to hold the string value
. If key
already holds a value, it is overwritten, regardless of its type. Any previous time to live associated with the key is discarded on successful SEToperation.
Options
Starting with Redis 2.6.12 SET 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.NX
-- Only set the key if it does not already exist.XX
-- Only set the key if it already exist.
Note: Since the SET command options can replace SETNX, SETEX, PSETEX, it is possible that in future versions of Redis these three commands will be deprecated and finally removed.
Return value
Status code reply: OK
if SET was executed correctly. Null multi-bulk reply: a Null Bulk Reply is returned if the SET operation was not performed becase the user specified the NX
or XX
option but the condition was not met.
Examples
OKredis> GET mykey
"Hello"
如果按上面的描述,我们想给一个key设置1.5秒的超时时间,那么一种可能是:SET name zl EX 100 PX 500,这时我们的期望是得到1秒加500毫秒的超时时间,执行结果如下:
127.0.0.1:6380[5]> SET name zl EX 100 PX 500
OK
127.0.0.1:6380[5]> pttl name
(integer) -2
127.0.0.1:6380[5]> pttl name
(integer) -2
127.0.0.1:6380[5]> set name zl ex 100
OK
127.0.0.1:6380[5]> pttl name
(integer) 97949
127.0.0.1:6380[5]> pttl name
(integer) 93010
但实际中,当同时设置了秒和毫秒时,只有毫秒起了作用,也就是说,接口的描述应该改为:
SET key value [EX seconds | PX milliseconds] [NX|XX]
/* SET key value [NX] [XX] [EX <seconds>] [PX <milliseconds>] */void setCommand(redisClient *c) { int j; robj *expire = NULL; int unit = UNIT_SECONDS; int flags = REDIS_SET_NO_FLAGS; for (j = 3; j < c->argc; j++) { char *a = c->argv[j]->ptr; robj *next = (j == c->argc-1) ? NULL : c->argv[j+1]; if ((a[0] == 'n' || a[0] == 'N') && (a[1] == 'x' || a[1] == 'X') && a[2] == '\0') { flags |= REDIS_SET_NX; } else if ((a[0] == 'x' || a[0] == 'X') && (a[1] == 'x' || a[1] == 'X') && a[2] == '\0') { flags |= REDIS_SET_XX; } else if ((a[0] == 'e' || a[0] == 'E') && (a[1] == 'x' || a[1] == 'X') && a[2] == '\0' && next) { unit = UNIT_SECONDS; expire = next; j++; } else if ((a[0] == 'p' || a[0] == 'P') && (a[1] == 'x' || a[1] == 'X') && a[2] == '\0' && next) { unit = UNIT_MILLISECONDS; expire = next; j++; } else { addReply(c,shared.syntaxerr); return; } } c->argv[2] = tryObjectEncoding(c->argv[2]); setGenericCommand(c,flags,c->argv[1],c->argv[2],expire,unit,NULL,NULL);}
请大家在使用时务必要注意!- 关于redis使用set时设置超时时间的问题
- 关于redis使用set时设置超时时间的问题
- redis设置value的超时时间
- 使用pthread_cond_timedwait()超时时间设置问题
- 关于session的超时设置问题
- redis 下载启动,设置、查询超时时间
- redis 下载启动,设置、查询超时时间
- curl的超时时间设置
- curl的超时时间设置
- 设置nginx的超时时间
- curl的超时时间设置
- 设置请求超时的时间
- 设置请求超时的时间
- 设置AFNetworking的超时时间
- curl的超时时间设置
- 设置AFNetWork的超时时间
- curl的超时时间设置
- session超时时间的设置
- 《黑马程序员》 交通管理灯项目
- Tab折页选项卡面板
- 当应用程序开始执行时,系统方法调用的先后顺序
- 手机产品设计中的反馈提示
- DB2数据库备份与恢复
- 关于redis使用set时设置超时时间的问题
- Linux文件系统管理
- JavaScript中的this关键字
- Redesign Your App for iOS 7 之 页面布局
- 市委组织部项目--EasyUI批量添加数据
- 2014年1月13日随笔
- 新版本?
- 数据结构实验之链表三:链表的逆置
- opencv学习笔记3——对Mat::at(x,y)的测试