一文打通redis中的String类型 曾经终败给现在 2023-10-14 22:44 57阅读 0赞 **目录** 基本介绍 基本语法 常用命令解读 概述 SETNX key value SETNX key value GETRANGE key start end GETSET key value GETBIT key offset MGET key1 \[key2..\] STRLEN key -------------------- #### 基本介绍 #### ①String是Redis最基本的类型,一个key对应一个value。 ②String类型是二进制安全的。意味着Redis的string可以包含任何数据。比如jpg图片或者序列化的对象 ③String类型是Redis最基本的数据类型,一个Redis中字符串value最多可以是512M > String的数据结构为简单动态字符串(Simple Dynamic String,缩写SDS)。是可以修改的字符串,内部结构实现上类似于Java的ArrayList #### 基本语法 #### Redis 字符串数据类型的相关命令用于管理 redis 字符串值,基本语法如下: **语法** redis 127.0.0.1:6379> COMMAND KEY_NAME **实例** redis 127.0.0.1:6379> SET w3ckey redis OK redis 127.0.0.1:6379> GET w3ckey "redis" > 在以上实例中我们使用了 **SET** 和 **GET** 命令,键为 w3ckey。 #### 常用命令解读 #### ##### 概述 ##### <table> <tbody> <tr> <th>序号</th> <th>命令及描述</th> </tr> <tr> <td>1</td> <td><a href="https://www.redis.net.cn/order/3544.html" title="SET key value" rel="nofollow">SET key value</a> 设置指定 key 的值</td> </tr> <tr> <td>2</td> <td><a href="https://www.redis.net.cn/order/3545.html" title="GET key" rel="nofollow">GET key</a> 获取指定 key 的值。</td> </tr> <tr> <td>3</td> <td><a href="https://www.redis.net.cn/order/3546.html" title="GETRANGE key start end" rel="nofollow">GETRANGE key start end</a> 返回 key 中字符串值的子字符</td> </tr> <tr> <td>4</td> <td><a href="https://www.redis.net.cn/order/3547.html" title="GETSET key value" rel="nofollow">GETSET key value</a> 将给定 key 的值设为 value ,并返回 key 的旧值(old value)。</td> </tr> <tr> <td>5</td> <td><a href="https://www.redis.net.cn/order/3548.html" title="GETBIT key offset" rel="nofollow">GETBIT key offset</a> 对 key 所储存的字符串值,获取指定偏移量上的位(bit)。</td> </tr> <tr> <td>6</td> <td><a href="https://www.redis.net.cn/order/3549.html" title="MGET key1 [key2..]" rel="nofollow">MGET key1 [key2..]</a> 获取所有(一个或多个)给定 key 的值。</td> </tr> <tr> <td>7</td> <td><a href="https://www.redis.net.cn/order/3550.html" title="SETBIT key offset value" rel="nofollow">SETBIT key offset value</a> 对 key 所储存的字符串值,设置或清除指定偏移量上的位(bit)。</td> </tr> <tr> <td>8</td> <td><a href="https://www.redis.net.cn/order/3551.html" title="SETEX key seconds value" rel="nofollow">SETEX key seconds value</a> 将值 value 关联到 key ,并将 key 的过期时间设为 seconds (以秒为单位)。</td> </tr> <tr> <td>9</td> <td><a href="https://www.redis.net.cn/order/3552.html" title="SETNX key value" rel="nofollow">SETNX key value</a> 只有在 key 不存在时设置 key 的值。</td> </tr> <tr> <td>10</td> <td><a href="https://www.redis.net.cn/order/3553.html" title="SETRANGE key offset value" rel="nofollow">SETRANGE key offset value</a> 用 value 参数覆写给定 key 所储存的字符串值,从偏移量 offset 开始。</td> </tr> <tr> <td>11</td> <td><a href="https://www.redis.net.cn/order/3554.html" title="STRLEN key" rel="nofollow">STRLEN key</a> 返回 key 所储存的字符串值的长度。</td> </tr> <tr> <td>12</td> <td><a href="https://www.redis.net.cn/order/3555.html" title="MSET key value [key value ...]" rel="nofollow">MSET key value [key value ...]</a> 同时设置一个或多个 key-value 对。</td> </tr> <tr> <td>13</td> <td><a href="https://www.redis.net.cn/order/3556.html" title="MSETNX key value [key value ...]" rel="nofollow">MSETNX key value [key value ...]</a> 同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在。</td> </tr> <tr> <td>14</td> <td><a href="https://www.redis.net.cn/order/3557.html" title="PSETEX key milliseconds value" rel="nofollow">PSETEX key milliseconds value</a> 这个命令和 SETEX 命令相似,但它以毫秒为单位设置 key 的生存时间,而不是像 SETEX 命令那样,以秒为单位。</td> </tr> <tr> <td>15</td> <td><a href="https://www.redis.net.cn/order/3558.html" title="INCR key" rel="nofollow">INCR key</a> 将 key 中储存的数字值增一。</td> </tr> <tr> <td>16</td> <td><a href="https://www.redis.net.cn/order/3559.html" title="INCRBY key increment" rel="nofollow">INCRBY key increment</a> 将 key 所储存的值加上给定的增量值(increment) 。</td> </tr> <tr> <td>17</td> <td><a href="https://www.redis.net.cn/order/3560.html" title="INCRBYFLOAT key increment" rel="nofollow">INCRBYFLOAT key increment</a> 将 key 所储存的值加上给定的浮点增量值(increment) 。</td> </tr> <tr> <td>18</td> <td><a href="https://www.redis.net.cn/order/3561.html" title="DECR key" rel="nofollow">DECR key</a> 将 key 中储存的数字值减一。</td> </tr> <tr> <td>19</td> <td><a href="https://www.redis.net.cn/order/3562.html" title="DECRBY key decrement" rel="nofollow">DECRBY key decrement</a> key 所储存的值减去给定的减量值(decrement) 。</td> </tr> <tr> <td>20</td> <td><a href="https://www.redis.net.cn/order/3563.html" title="APPEND key value" rel="nofollow">APPEND key value</a> 如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。</td> </tr> </tbody> </table> ##### SETNX key value ##### Redis Setnx(**SET** if **N**ot e**X**ists) 命令在指定的 key 不存在时,为 key 设置指定的值。 redis> EXISTS job # job 不存在 (integer) 0 redis> SETNX job "programmer" # job 设置成功 (integer) 1 redis> SETNX job "code-farmer" # 尝试覆盖 job ,失败 (integer) 0 redis> GET job # 没有被覆盖 "programmer" > 返回值:设置成功,返回 1 。 设置失败,返回 0 。 ##### SETNX key value ##### Redis Setex 命令为指定的 key 设置值及其过期时间。如果 key 已经存在, SETEX 命令将会替换旧的值。 redis 127.0.0.1:6379> SETEX mykey 60 redis OK redis 127.0.0.1:6379> TTL mykey 60 redis 127.0.0.1:6379> GET mykey "redis > 返回值:设置成功时返回 OK 。 ##### GETRANGE key start end ##### Redis Getrange 命令用于获取存储在指定 key 中字符串的子字符串。字符串的截取范围由 start 和 end 两个偏移量决定(包括 start 和 end 在内)。 redis 127.0.0.1:6379> SET mykey "This is my test key" OK redis 127.0.0.1:6379> GETRANGE mykey 0 3 "This" redis 127.0.0.1:6379> GETRANGE mykey 0 -1 "This is my test key" > 返回值:截取得到的子字符串。 ##### GETSET key value ##### Redis Getset 命令用于设置指定 key 的值,并返回 key 旧的值。 redis 127.0.0.1:6379> GETSET mynewkey "This is my test key" (nil) redis 127.0.0.1:6379> GETSET mynewkey "This is my new value to test getset" "This is my test key" > 返回值:返回给定 key 的旧值。 当 key 没有旧值时,即 key 不存在时,返回 nil 。 > > 当 key 存在但不是字符串类型时,返回一个错误。 ##### GETBIT key offset ##### Redis Getbit 命令用于对 key 所储存的字符串值,获取指定偏移量上的位(bit)。 # 对不存在的 key 或者不存在的 offset 进行 GETBIT, 返回 0 redis> EXISTS bit (integer) 0 redis> GETBIT bit 10086 (integer) 0 # 对已存在的 offset 进行 GETBIT redis> SETBIT bit 10086 1 (integer) 0 redis> GETBIT bit 10086 (integer) 1 > 返回值:字符串值指定偏移量上的位(bit)。当偏移量 OFFSET 比字符串值的长度大,或者 key 不存在时,返回 0 。 ##### MGET key1 \[key2..\] ##### Redis Mget 命令返回所有(一个或多个)给定 key 的值。 如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil 。 redis 127.0.0.1:6379> SET key1 "hello" OK redis 127.0.0.1:6379> SET key2 "world" OK redis 127.0.0.1:6379> MGET key1 key2 someOtherKey 1) "Hello" 2) "World" 3) (nil) > 返回值:一个包含所有给定 key 的值的列表。 ##### STRLEN key ##### Redis Strlen 命令用于获取指定 key 所储存的字符串值的长度。当 key 储存的不是字符串值时,返回一个错误。 # 对不存在的 key 执行 APPEND redis> EXISTS myphone # 确保 myphone 不存在 (integer) 0 redis> APPEND myphone "nokia" # 对不存在的 key 进行 APPEND ,等同于 SET myphone "nokia" (integer) 5 # 字符长度 # 对已存在的字符串进行 APPEND redis> APPEND myphone " - 1110" # 长度从 5 个字符增加到 12 个字符 (integer) 12 redis> GET myphone "nokia - 1110" > 返回值:字符串值的长度。 当 key 不存在时,返回 0。
还没有评论,来说两句吧...