set 命令
SET key value [EX seconds] [PX milliseconds] [NX|XX]
设置key为保持字符串value。如果key已经拥有一个值,则无论其类型如何,它都会被覆盖。
对于某个原本带有生存时间(TTL)的键来说, 当 SET 命令成功在这个键上执行时, 这个键原有的 TTL 将被清除。
从 Redis 2.6.12 版本开始, SET 命令可以传递参数
可选参数
- EX second :设置键的过期时间为 second 秒。 SET key value EX second 效果等同于 SETEX key second value 。
- PX millisecond :设置键的过期时间为 millisecond 毫秒。 SET key value PX millisecond 效果等同于 PSETEX key millisecond value 。
- NX :只在键不存在时,才对键进行设置操作。 SET key value NX 效果等同于 SETNX key value 。
- XX :只在键已经存在时,才对键进行设置操作。
SET、SETNX 、 SETEX 和 PSETEX
- 更多说明请参考官网
历史
>= 2.6.12
增加了EX,PX,NX和XX选项。>= 6.0
添加了KEEPTTL选项。>= 6.2
添加了GET,EXAT和PXAT选项。>= 7.0
允许NX和GET选项一起使用。
PhpRedis 扩展用法介绍
set
Description: Set the string value in argument as value of the key. If you’re using Redis >= 2.6.12, you can pass extended options as explained below
Parameters
Key
Value
Timeout or Options Array (optional). If you pass an integer, phpredis will redirect to SETEX, and will try to use Redis >= 2.6.12 extended options if you pass an array with valid values
Return value
Bool TRUE
if the command is successful.
Examples
// Simple key -