Redis 从入门到放弃02——String 数据类型
String 数据类型的介绍
Introduction to Redis strings(String 数据类型介绍):
Redis strings store sequences of bytes, including text, serialized objects, and binary arrays. As such, strings are the simplest type of value you can associate with a Redis key. They're often used for caching, but they support additional functionality that lets you implement counters and perform bitwise operations, too.
Since Redis keys are strings, when we use the string type as a value too, we are mapping a string to another string. The string data type is useful for a number of use cases, like caching HTML fragments or pages.
Let's play a bit with the string type, using
redis-cli
(all the examples will be performed viaredis-cli
in this tutorial).
Redis字符串存储字节序列,包括文本、序列化对象和二进制数组。因此,字符串是你可以与Redis键关联的最简单的值类型。它们通常用于缓存,但它们也支持其他功能,允许您实现计数器和执行按位操作。
由于Redis键是字符串,当我们使用字符串类型作为值时,我们将一个字符串映射到另一个字符串。字符串数据类型对于许多用例都很有用,比如缓存HTML片段或页面。
让我们使用' redis-cli '来玩一下字符串类型(本教程中所有的示例都将通过' redis-cli '来执行)
Limits(限制)
By default, a single Redis string can be a maximum of 512 MB.
默认情况下,单个Redis字符串的最大容量为512mb
常用String 命令
查看String数据类型下所有命令 :help @String
127.0.0.1:6379> help @String
APPEND key value
summary: Append a value to a key
since: 2.0.0
BITCOUNT key [start end]
summary: Count set bits in a string
since: 2.6.0
BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL]
summary: Perform arbitrary bitfield integer operations on strings
since: 3.2.0
BITOP operation destkey key [key ...]
summary: Perform bitwise operations between strings
since: 2.6.0
BITPOS key bit [start] [end]
summary: Find first bit set or clear in a string
since: 2.8.7
DECR key
summary: Decrement the integer value of a key by one
since: 1.0.0
DECRBY key decrement
summary: Decrement the integer value of a key by the given number
since: 1.0.0
GET key
summary: Get the value of a key
since: 1.0.0
GETBIT key offset
summary: Returns the bit value at offset in the string value stored at key
since: 2.2.0
GETDEL key
summary: Get the value of a key and delete the key
since: 6.2.0
GETEX key [EX seconds|PX milliseconds|EXAT timestamp|PXAT milliseconds-timestamp|PERSIST]
summary: Get the value of a key and optionally set its expiration
since: 6.2.0
GETRANGE key start end
summary: Get a substring of the string stored at a key
since: 2.4.0
GETSET key value
summary: Set the string value of a key and return its old value
since: 1.0.0
INCR key
summary: Increment the integer value of a key by one
since: 1.0.0
INCRBY key increment
summary: Increment the integer value of a key by the given amount
since: 1.0.0
INCRBYFLOAT key increment
summary: Increment the float value of a key by the given amount
since: 2.6.0
MGET key [key ...]
summary: Get the values of all the given keys
since: 1.0.0
MSET key value [key value ...]
summary: Set multiple keys to multiple values
since: 1.0.1
MSETNX key value [key value ...]
summary: Set multiple keys to multiple values, only if none of the keys exist
since: 1.0.1
PSETEX key milliseconds value
summary: Set the value and expiration in milliseconds of a key
since: 2.6.0
SET key value [EX seconds|PX milliseconds|EXAT timestamp|PXAT milliseconds-timestamp|KEEPTTL] [NX|XX] [GET]
summary: Set the string value of a key
since: 1.0.0
SETBIT key offset value
summary: Sets or clears the bit at offset in the string value stored at key
since: 2.2.0
SETEX key seconds value
summary: Set the value and expiration of a key
since: 2.0.0
SETNX key value
summary: Set the value of a key, only if the key does not exist
since: 1.0.0
SETRANGE key offset value
summary: Overwrite part of a string at key starting at the specified offset
since: 2.2.0
STRALGO LCS algo-specific-argument [algo-specific-argument ...]
summary: Run algorithms (currently LCS) against strings
since: 6.0.0
STRLEN key
summary: Get the length of the value stored in a key
since: 2.2.0
SET key value [EX seconds|PX milliseconds|EXAT timestamp|PXAT milliseconds-timestamp|KEEPTTL] [NX|XX] [GET] : 为当前 key 赋值
# 直接赋值
127.0.0.1:6379> set k1 v1
OK
# 赋值加上过期时间(秒)EX
127.0.0.1:6379> set k2 v2 ex 20
OK
# 赋值加上过期时间(毫秒)PX
127.0.0.1:6379> set k3 v3 px 30000
OK
# 赋值加上过期时间 (unix 时间戳-秒)
127.0.0.1:6379> set k5 v5 EXAT 1689322421
OK
127.0.0.1:6379> ttl k5
(integer) 47
# 赋值加上过期时间 (unix 时间戳-毫秒)
127.0.0.1:6379> set k6 v6 pxat 1689322794416
OK
127.0.0.1:6379> pttl k6
(integer) 287833
# 锦当 key 不存在时赋值 NX
127.0.0.1:6379> set k4 v4 nx
OK
# 仅当 key 存在时赋值 XX
127.0.0.1:6379> set k5 v5 xx
(nil)
# 为 key 赋新值, 返回旧值
127.0.0.1:6379> set k4 v5 get
"v4"
get key :获取key的值
127.0.0.1:6379> get k1
"v1"
strlen key:获取字符串的长度,这里的长度是指字符串占几个字节
127.0.0.1:6379> set k3 22
OK
127.0.0.1:6379> strlen k3
(integer) 2
setex key seconds value: 赋值并且设置key的过期时间 -秒 和set key value ex seconds 效果一样
127.0.0.1:6379> setex f6 200 aiguo
OK
127.0.0.1:6379> ttl f6
(integer) 140
psetex key milliseconds value :赋值并且设置key的过期时间 -毫秒 和set key value px millseconds效果一样
127.0.0.1:6379> psetex f7 200000 duli
OK
127.0.0.1:6379> pttl f7
(integer) 181792
setrange key offset value :设置字符串指定位置的值
127.0.0.1:6379> set f1 lisi
OK
127.0.0.1:6379> setrange f1 3 a
(integer) 4
127.0.0.1:6379> get f1
"lisa"
getrange key start end :返回 key 中字符串值指定下标范围的子字符,效果类似js substring 函数
127.0.0.1:6379> getrange f1 0 2
"lis"
getset key value : 设置key 的值设为 value ,并返回 key 的旧值(old value)
127.0.0.1:6379> getset f1 z3
"lisa"
mset key value [key value ...] :一次性设置多个key-value
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3
OK
mset key value [key value ...] :获取多个key的值
127.0.0.1:6379> keys *
1) "k3"
2) "k1"
3) "k2"
127.0.0.1:6379> mget k1 k2
1) "v1"
2) "v2"
setnx key value :只有在 key 不存在时设置 key 的值 (用于分布式锁)和 set key value nx 效果一致
127.0.0.1:6379> keys *
1) "k3"
2) "k1"
3) "k2"
127.0.0.1:6379> setnx k1 zhaosi
(integer) 0
127.0.0.1:6379> setnx k4 zhaoliu
(integer) 1
msetnx key value [key value ...]:同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在 其中一个失败则全部失败
127.0.0.1:6379> keys *
1) "k3"
2) "k1"
3) "k2"
127.0.0.1:6379> msetnx k1 v1 k2 v2 k5 v5
(integer) 0
127.0.0.1:6379> msetnx k5 v5 k6 v6
(integer) 1
incr key :设置当前键值以 1 递增 并返回自增后的值 (如果key不存在会新增一个key;如果当前key的值不是整数,则会提示报错)
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> incr k1
(error) ERR value is not an integer or out of range
127.0.0.1:6379> set k7 1.0
OK
127.0.0.1:6379> get k7
"1.0"
127.0.0.1:6379> incr k7
(error) ERR value is not an integer or out of range
127.0.0.1:6379> set k7 1
OK
127.0.0.1:6379> incr k7
(integer) 2
incrby key increment:设置当前键值以指定的步长递增 并返回自增后的值
127.0.0.1:6379> get k7
"2"
127.0.0.1:6379> incrby k7 4
(integer) 6
incrbyfloat :设置当前键值以指定的步长(浮点数)递增 并返回自增后的值
127.0.0.1:6379> incrbyfloat k8 0.5
"0.5"
127.0.0.1:6379> incrbyfloat k8 1
"1.5"
decr key :设置当前键值以 1 自减 并返回自减后的值
127.0.0.1:6379> decr k7
(integer) 5
decrby key decrement 设置当前键值以指定的 步长 自减 并返回自减后的值
127.0.0.1:6379> decrby k7 2
(integer) 3
append key value :对key后面追加指定的字符串 并返回字符串追加后的长度(如果key不存在则相当于set key value 并返回字符串追加后的长度)
127.0.0.1:6379> append k9 lisa
(integer) 4
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> append k1 vv
(integer) 4
应用场景
- 抖音点赞、某个商品点一下添加一次
- 文章点赞数、喜欢数、阅读数等:只要点击了点赞按钮、喜欢或者rest地址,直接可以使用incr key 命令增加一个数字1,完成记录数字。