Redis set命令详解

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 -
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值