string类型操作指南(更新版)

SET (添加String类型的 键值对)

set 命令 用于添加键值对,值 为string类型

语法:

<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 秒数 / PX 毫秒数 NX/XX</pre>

  • set key value: set 键 值

  • EX 秒数 / PX 毫秒数 : 设置键值对的生存时间, ex 后面写的时间单位为秒, px 后面写的单位时间 为毫秒, 两个单位 2选1 (生存时间结束,会自动删除键值对)

  • NX/XX : 同样是2选1, nx 代表 键不存在 才可以设置,相当于create, xx 代表 键存在时 才可以设置,相当于update

注意: value的大小不能超过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 命令在设置成功时返回 1 , 设置失败时返回 0 。

只在键 key 不存在的情况下, 将键 key 的值设置为 value 。 若键 key 已经存在, 则 SETNX 命令不做任何动作。 SETNX 是『SET if Not eXists』(如果不存在,则 SET)的简写。

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 秒数 value</pre>

效果等同于 : SET key value EX 秒数 命令在设置成功时返回 OK 。 当 seconds 参数不合法时, 命令将返回一个错误.

将键 key 的值设置为 value , 并将键 key 的生存时间设置为 seconds 秒钟。 如果键 key 已经存在, 那么 SETEX 命令将覆盖已有的值。 SETEX 命令的效果和以下两个命令的效果类似:

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 毫秒数 value</pre>

效果等同于 : SET key value PX 秒数 命令在设置成功时返回 OK 。

这个命令和 SETEX 命令相似, 但它以毫秒为单位设置 key 的生存时间, 而不是像 SETEX 命令那样以秒为单位进行设置。

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 的生存时间,单位为秒

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 的生存时间,单位为毫秒

实战

使用xshell连接centos 使用redis-cli 登录 redis,并验证密码

  1. set 键值对 a 123,设置生存时间为 20 秒

  2. 查看 a 的生存时间,单位为秒

  3. 查看 a 的生存时间,单位为毫秒

  4. PSETEX 键值对 b 456 ,设置生存时间为 10000毫秒

image.png

重点

掌握set语法

SET key value EX 秒数 / PX 毫秒数 NX/XX

掌握TTL,PTTL命令

作业

  1. 插入数据 key为 userToken:1 ,value为sjfl2skg246iuio,设置生存时间30分钟
SET userToken:1 sjfl2skg246iuio EX 1800

  1. 描述这个命令的含义: SET a 123 NX
    :键a不存在时,插入key为a value为123
  2. 描述这个命令的含义: SETEX b 600 123
    :插入键为b 值为123 生存时间为600秒
  3. 写出命令: 查看key 为 b的生存时间,单位为秒
TTL b
  1. 写出命令: 查看key 为 b的生存时间,单位为毫秒
PTTL b
QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空