SET (添加String類型的 鍵值對(duì))
set 命令 用于添加鍵值對(duì),值 為string類型
語(yǔ)法:
<pre class="prettyprint linenums prettyprinted" style="margin: 0px; border: 1px solid rgb(226, 226, 226) !important; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important;">SET key value EX 秒數(shù) / PX 毫秒數(shù) NX/XX</pre>
set key value: set 鍵 值
EX 秒數(shù) / PX 毫秒數(shù) : 設(shè)置鍵值對(duì)的生存時(shí)間, ex 后面寫(xiě)的時(shí)間單位為秒, px 后面寫(xiě)的單位時(shí)間 為毫秒, 兩個(gè)單位 2選1 (生存時(shí)間結(jié)束,會(huì)自動(dòng)刪除鍵值對(duì))
NX/XX : 同樣是2選1, nx 代表 鍵不存在 才可以設(shè)置,相當(dāng)于create, xx 代表 鍵存在時(shí) 才可以設(shè)置,相當(dāng)于update
注意: value的大小不能超過(guò)512M
SETNX
<pre class="prettyprint linenums prettyprinted" style="margin: 0px; border: 1px solid rgb(226, 226, 226) !important; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important;">SETNX key value</pre>
效果等同于 : SET key value NX 命令在設(shè)置成功時(shí)返回 1 倡怎, 設(shè)置失敗時(shí)返回 0 贱枣。
只在鍵 key 不存在的情況下纽哥, 將鍵 key 的值設(shè)置為 value 。 若鍵 key 已經(jīng)存在晓避, 則 SETNX 命令不做任何動(dòng)作。 SETNX 是『SET if Not eXists』(如果不存在俏拱,則 SET)的簡(jiǎn)寫(xiě)。
SETEX
<pre class="prettyprint linenums prettyprinted" style="margin: 0px; border: 1px solid rgb(226, 226, 226) !important; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important;">SETEX key 秒數(shù) value</pre>
效果等同于 : SET key value EX 秒數(shù) 命令在設(shè)置成功時(shí)返回 OK 事格。 當(dāng) seconds 參數(shù)不合法時(shí)驹愚, 命令將返回一個(gè)錯(cuò)誤.
將鍵 key 的值設(shè)置為 value 劣纲, 并將鍵 key 的生存時(shí)間設(shè)置為 seconds 秒鐘癞季。 如果鍵 key 已經(jīng)存在, 那么 SETEX 命令將覆蓋已有的值。 SETEX 命令的效果和以下兩個(gè)命令的效果類似:
PSETEX
<pre class="prettyprint linenums prettyprinted" style="margin: 0px; border: 1px solid rgb(226, 226, 226) !important; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important;">PSETEX key 毫秒數(shù) value</pre>
效果等同于 : SET key value PX 秒數(shù) 命令在設(shè)置成功時(shí)返回 OK 辉巡。
這個(gè)命令和 SETEX 命令相似蕊退, 但它以毫秒為單位設(shè)置 key 的生存時(shí)間, 而不是像 SETEX 命令那樣以秒為單位進(jìn)行設(shè)置净蚤。
TTL
<pre class="prettyprint linenums prettyprinted" style="margin: 0px; border: 1px solid rgb(226, 226, 226) !important; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important;">TTL key</pre>
返回key 的生存時(shí)間,單位為秒
PTTL
<pre class="prettyprint linenums prettyprinted" style="margin: 0px; border: 1px solid rgb(226, 226, 226) !important; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important;">PTTL key</pre>
返回key 的生存時(shí)間,單位為毫秒
實(shí)戰(zhàn)
使用xshell連接centos 使用redis-cli 登錄 redis,并驗(yàn)證密碼
set 鍵值對(duì) a 123,設(shè)置生存時(shí)間為 20 秒
查看 a 的生存時(shí)間,單位為秒
查看 a 的生存時(shí)間,單位為毫秒
PSETEX 鍵值對(duì) b 456 ,設(shè)置生存時(shí)間為 10000毫秒
重點(diǎn)
掌握set語(yǔ)法
SET key value EX 秒數(shù) / PX 毫秒數(shù) NX/XX
掌握TTL,PTTL命令
作業(yè)
- 插入數(shù)據(jù) key為 userToken:1 ,value為sjfl2skg246iuio,設(shè)置生存時(shí)間30分鐘
SET userToken:1 sjfl2skg246iuio EX 1800
- 描述這個(gè)命令的含義: SET a 123 NX
:鍵a不存在時(shí),插入key為a value為123 - 描述這個(gè)命令的含義: SETEX b 600 123
:插入鍵為b 值為123 生存時(shí)間為600秒 - 寫(xiě)出命令: 查看key 為 b的生存時(shí)間,單位為秒
TTL b
- 寫(xiě)出命令: 查看key 為 b的生存時(shí)間,單位為毫秒
PTTL b