文档分值:0

Redis >>> Redis >>> redis命令相关

课程目录

数据类型
redis命令相关
Redis客户端链接
Redis数据库备份
初学redis必读
Redis集群
SET

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 SET operation.

如果 key 已经保存了一个值,那么这个操作会直接覆盖原来的值,并且忽略原始类型。 当set命令执行成功之后,之前设置的过期时间都将失效

选项

从2.6.12版本开始,redis为SET命令增加了一系列选项:

  • 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.
  • EX seconds – 设置键key的过期时间,单位时秒
  • PX milliseconds – 设置键key的过期时间,单位时毫秒
  • NX – 只有键key不存在的时候才会设置key的值
  • XX – 只有键key存在的时候才会设置key的值

注意: 由于SET命令加上选项已经可以完全取代SETNX, SETEX, PSETEX的功能,所以在将来的版本中,redis可能会不推荐使用并且最终抛弃这几个命令。

返回值

simple-string-reply:如果SET命令正常执行那么回返回OK,否则如果加了NX 或者 XX选项,但是没有设置条件。那么会返回nil。

例子 redis> SET mykey "Hello" OK redis> GET mykey "Hello" redis>

[ 该条目创建时间:2016-09-30 08:37:20 ]