redis SET command
Translation results:
英[set] 美[sɛt]
vt. Set; place, arrange; put in a certain situation; place tableware
vi.fall; set off; condense
n. Collection; a set, a pair; a set; a television set
adj. Fixed; located in...; stubborn; arranged
Third person singular: sets plural : sets present participle: setting past tense: set past participle: set
redis SET commandsyntax
Function: Associate the string value value to key. If key already holds another value, SET overwrites the old value, regardless of type.
Syntax: SET key value [EX seconds] [PX milliseconds] [NX|XX]
Description: Because the SET command can pass parameters To achieve the effect of the three commands SETNX , SETEX and PSETEX , so future Redis versions may abandon and eventually remove the three commands SETNX , SETEX and PSETEX .
Available versions: >= 1.0.0
Time complexity: O(1)
Return: Before Redis version 2.6.12, the SET command always returned OK. Starting from Redis version 2.6.12, SET returns OK only when the setting operation completes successfully. If NX or XX is set, but the setting operation is not executed because the conditions are not met, the command returns a NULL Bulk Reply.
redis SET commandexample
# 对不存在的键进行设置 redis 127.0.0.1:6379> SET key "value" OK redis 127.0.0.1:6379> GET key "value" # 对已存在的键进行设置 redis 127.0.0.1:6379> SET key "new-value" OK redis 127.0.0.1:6379> GET key "new-value" # 使用 EX 选项 redis 127.0.0.1:6379> SET key-with-expire-time "hello" EX 10086 OK redis 127.0.0.1:6379> GET key-with-expire-time "hello" redis 127.0.0.1:6379> TTL key-with-expire-time (integer) 10069 # 使用 PX 选项 redis 127.0.0.1:6379> SET key-with-pexpire-time "moto" PX 123321 OK redis 127.0.0.1:6379> GET key-with-pexpire-time "moto" redis 127.0.0.1:6379> PTTL key-with-pexpire-time (integer) 111939 # 使用 NX 选项 redis 127.0.0.1:6379> SET not-exists-key "value" NX OK # 键不存在,设置成功 redis 127.0.0.1:6379> GET not-exists-key "value" redis 127.0.0.1:6379> SET not-exists-key "new-value" NX (nil) # 键已经存在,设置失败 redis 127.0.0.1:6379> GEt not-exists-key "value" # 维持原值不变 # 使用 XX 选项 redis 127.0.0.1:6379> EXISTS exists-key (integer) 0 redis 127.0.0.1:6379> SET exists-key "value" XX (nil) # 因为键不存在,设置失败 redis 127.0.0.1:6379> SET exists-key "value" OK # 先给键设置一个值 redis 127.0.0.1:6379> SET exists-key "new-value" XX OK # 设置新值成功 redis 127.0.0.1:6379> GET exists-key "new-value" # NX 或 XX 可以和 EX 或者 PX 组合使用 redis 127.0.0.1:6379> SET key-with-expire-and-NX "hello" EX 10086 NX OK redis 127.0.0.1:6379> GET key-with-expire-and-NX "hello" redis 127.0.0.1:6379> TTL key-with-expire-and-NX (integer) 10063 redis 127.0.0.1:6379> SET key-with-pexpire-and-XX "old value" OK redis 127.0.0.1:6379> SET key-with-pexpire-and-XX "new value" PX 123321 OK redis 127.0.0.1:6379> GET key-with-pexpire-and-XX "new value" redis 127.0.0.1:6379> PTTL key-with-pexpire-and-XX (integer) 112999 # EX 和 PX 可以同时出现,但后面给出的选项会覆盖前面给出的选项 redis 127.0.0.1:6379> SET key "value" EX 1000 PX 5000000 OK redis 127.0.0.1:6379> TTL key (integer) 4993 # 这是 PX 参数设置的值 redis 127.0.0.1:6379> SET another-key "value" PX 5000000 EX 1000 OK redis 127.0.0.1:6379> TTL another-key (integer) 997 # 这是 EX 参数设置的值