Bahasa Inggeris [set] Amerika [s?t]

vt letakkan; Televisyen

adj. Tetap; terletak di...; degil; tersusun

Orang ketiga tunggal: set Plural: set Participle present: setting Kata lampau: set

arahan SET redis sintaks

Fungsi: Kaitkan nilai nilai rentetan dengan kunci . Jika kunci sudah memegang nilai lain, SET akan menimpa nilai lama, tanpa mengira jenis.

Sintaks: SET nilai kunci [EX saat] [PX milisaat] [NX|XX]

Penjelasan: Oleh kerana arahan SET boleh mencapai kesan yang sama seperti tiga arahan SETNX, SETEX dan PSETEX melalui parameter pada masa hadapan Versi Redis mungkin tidak digunakan dan akhirnya mengalih keluar arahan SETNX , SETEX dan PSETEX.

Versi yang tersedia: >= 1.0.0

Kerumitan masa: O(1)

Kembalikan: Sebelum Redis versi 2.6.12 arahan sentiasa dikembalikan, OK Bermula dari Redis versi 2.6.12, SET mengembalikan OK hanya apabila operasi tetapan selesai dengan jayanya. Jika NX atau XX ditetapkan, tetapi operasi tetapan tidak dilaksanakan kerana syarat tidak dipenuhi, arahan mengembalikan Balasan Pukal NULL.

arahan SET redis contoh

# 對不存在的鍵進(jìn)行設(shè)置
redis 127.0.0.1:6379> SET key "value"
OK
redis 127.0.0.1:6379> GET key
"value"
# 對已存在的鍵進(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è)置一個值
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 可以同時出現(xiàn),但后面給出的選項(xiàng)會覆蓋前面給出的選項(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è)置的值