英[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 command syntax

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 command example

# 對(duì)不存在的鍵進(jìn)行設(shè)置
redis 127.0.0.1:6379> SET key "value"
OK
redis 127.0.0.1:6379> GET key
"value"
# 對(duì)已存在的鍵進(jìn)行設(shè)置
redis 127.0.0.1:6379> SET key "new-value"
OK
redis 127.0.0.1:6379> GET key
"new-value"
# 使用 EX 選項(xiàng)
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 選項(xiàng)
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 選項(xiàng)
redis 127.0.0.1:6379> SET not-exists-key "value" NX
OK      # 鍵不存在,設(shè)置成功
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)   # 鍵已經(jīng)存在,設(shè)置失敗
redis 127.0.0.1:6379> GEt not-exists-key
"value" # 維持原值不變
# 使用 XX 選項(xiàng)
redis 127.0.0.1:6379> EXISTS exists-key
(integer) 0
redis 127.0.0.1:6379> SET exists-key "value" XX
(nil)   # 因?yàn)殒I不存在,設(shè)置失敗
redis 127.0.0.1:6379> SET exists-key "value"
OK      # 先給鍵設(shè)置一個(gè)值
redis 127.0.0.1:6379> SET exists-key "new-value" XX
OK      # 設(shè)置新值成功
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 可以同時(shí)出現(xiàn),但后面給出的選項(xiàng)會(huì)覆蓋前面給出的選項(xiàng)
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 參數(shù)設(shè)置的值
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 參數(shù)設(shè)置的值