更多资料请前往–》软考历年真题
Redis 支持五种核心数据类型,每种类型都有特定的应用场景和命令集,以下是详细分类说明:
- String(字符串)
应用场景:存储简单值(如计数器、缓存数据)
常用命令:
SET key value:设置键值对
GET key:获取值
SET mykey "Hello" # 设置键值
GET mykey # 返回"Hello"
INCR/DECR:对整数值自增1或自减1,若key不存在则初始化为0后操作
SET counter 10
INCR counter #返回11(自增1)
DECR counter #返回10(自减1)
INCRBY/DECRBY:按指定步长增减整数值
SET inventory 50
INCRBY inventory 5 # 返回55(增加5)
DECRBY inventory 8 # 返回47(减少8)
INCRBYFLOAT:对浮点数值增减指定浮点数
SET temperature 36.5
INCRBYFLOAT temperature 0.7 # 返回37.2(增加0.7)
INCRBYFLOAT temperature -1.2 # 返回36.0(减少1.2)
APPEND key value:追加字符串
APPEND
SET greeting "Hello"
APPEND greeting " World" # 值变为"Hello World"
不存在的key会自动创建
APPEND new_key “Redis” # 创建新键并设置值为"Redis"
二进制安全示例
SET binary_data “\x00\x01”
APPEND binary_data “\x02\x03” # 可追加二进制数据
STRLEN key:获取字符串长度
常规字符串
SET message “Redis”
STRLEN message # 返回5(字节数)


1万+

被折叠的 条评论
为什么被折叠?



