SET key value [EX seconds|PX milliseconds] [NX|XX] [KEEPTTL]

Available since 1.0.0.

Time complexity: O(1)

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.

Options

The SET command supports a set of options that modify its behavior:

  • EXseconds -- Set the specified expire time, in seconds.
  • PXmilliseconds -- 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.
  • KEEPTTL -- Retain the time to live associated with the key.

Note: Since the SET command options can replace SETNXSETEXPSETEX, it is possible that in future versions of Redis these three commands will be deprecated and finally removed.

Return value

Simple string replyOK if SET was executed correctly. Null reply: a Null Bulk Reply is returned if the SET operation was not performed because the user specified the NX or XX option but the condition was not met.

History

  • >= 2.6.12: Added the EXPXNX and XX options.
  • >= 6.0: Added the KEEPTTL option.

Examples

redis> SET mykey "Hello"
"OK"
redis> GET mykey
"Hello"
redis> SET anotherkey "will expire in a minute" EX 60
"OK"
redis> 

Patterns

Note: The following pattern is discouraged in favor of the Redlock algorithm which is only a bit more complex to implement, but offers better guarantees and is fault tolerant.

The command SET resource-name anystring NX EX max-lock-time is a simple way to implement a locking system with Redis.

A client can acquire the lock if the above command returns OK (or retry after some time if the command returns Nil), and remove the lock just using DEL.

The lock will be auto-released after the expire time is reached.

It is possible to make this system more robust modifying the unlock schema as follows:

  • Instead of setting a fixed string, set a non-guessable large random string, called token.
  • Instead of releasing the lock with DEL, send a script that only removes the key if the value matches.

This avoids that a client will try to release the lock after the expire time deleting the key created by another client that acquired the lock later.

An example of unlock script would be similar to the following:

if redis.call("get",KEYS[1]) == ARGV[1]
then
    return redis.call("del",KEYS[1])
else
    return 0
end

The script should be called with EVAL ...script... 1 resource-name token-value

以上就是官方文档的搬运部分,下面将会是我尝试的翻译部分

SET key value [EX seconds|PX milliseconds] [NX|XX] [KEEPTTL]

该命令从1.0.0版本开始可用,时间复杂度是O(1)

set key用来设施string类型的value。如果这个key已经保存了一个value,那么set指令将会无视它之前保存的数据的类型,将这个key的值覆盖。并且,如果这个set操作成功了,那么之前对这个key设置的过期时间都将被废弃。

可选参数

set指令支持几个可选参数来控制在set时候的行为:

  EX seconds 为其设置过期时间,单位是second

  PX milliseconds 设置过期时间,单位是millisecond

  NX 仅仅在在key不存在的时候执行set

  XX 仅仅在key存在的时候执行

  KEEPTTL  保留之前给这个key设置的过期时间(注,在网页版的redis中(号称redis_version:999.999.999)不支持该参数,在redis6.0之后才添加的该参数)

note:

  自从set指令能够代替setnx,setex,psetex之后,官方已经不建议继续使用这三个指令,并且可能在将来的版本中移除这三个指令。

return value

  OK: 如果set执行成功

  Null reply: 如果因为用户设置了NX或者XX,但是由于条件没有达到而导致set失败,那么将会返回 Null Bulk Reply

历史版本:

  >= 2.6.12 添加了EX,PX,XX选项

  >=6.0 添加了KEEPTTL选项

模式:

  note:

    The following pattern is discouraged in favor of the Redlock algorithm which is only a bit more complex to implement, but offers better guarantees and is fault tolerant.(这句话我有点没翻译明白,原文贴上来,直接翻译出来是:下面的模式不推荐使用redlock算法,因为它仅仅提高了一点点实现的复杂度,但是保证了更好的容错性)

  SET resource-name anystring NX EX max-lock-time 是一个实现redis锁的简单方式。

  如果以上命令返回了OK(如果返回了Nil,那么在一段时间之后重试),那么可客户端就能够获取到这个lock,然后使用DEL删除这个锁。

  这个锁将在expire时间到达之后被自动释放。

  按照如下方式设置,可能使得这个系统的鲁棒性更好(或者翻译为:可以使得这个系统更加健壮。鲁棒性就是指系统在面临一些特殊情况下保持系统原本功能稳定的特性。说实话,这个音译词翻译的实在是让人一头雾水,可专业术语就叫这个):

  1.用一个随机字符串token来代替确定的字符串

  2.当key匹配的时候,用DEL来代替释放锁。

这避免了客户端在过期时间之后尝试着去释放锁,反而删除了之后获得锁的另一个客户端的key