# String类型
将字符串值 value 关联到 key 。
时间复杂度: O(1)
# SET
redis> SET key "value"
OK
可选参数
- EX seconds : 将键的过期时间设置为 seconds 秒。
- PX milliseconds : 将键的过期时间设置为 milliseconds 毫秒。
- NX : 只在键不存在时, 才对键进行设置操作。
- XX : 只在键已经存在时, 才对键进行设置操作。
示例
使用 EX 选项:
redis> SET key-with-expire-time "hello" EX 10086 等同于 redis> SETEX key-with-expire-time 10086 "hello"使用 PX 选项:
redis> SET key-with-expire-time "hello" PX 10086 等同于 redis> PSETEX key-with-expire-time 10086 "hello"使用 NX 选项:
redis> SET not-exists-key "value" NX OK # 键不存在时,返回OK, 设置成功 等同于 redis> SETNX not-exists-key "value"
# STRLEN key
返回键 key 储存的字符串值的长度。
复杂度: O(1)
示例
redis> SET key "Hello Redis"
OK
redis> STRLEN key
(integer) 11
# APPEND key value
把 value 追加到键 key 现有值的末尾。如果 key 不存在, APPEND 就简单地将键 key 的值设为 value , 就像执行 SET key value 一样。
时间复杂度: 平摊O(1)
示例
APPEND phone "iphone" # 对不存在的 key(phone) 进行 APPEND ,等同于 SET phone "iphone"
(integer) 6 # 返回字符长度
# 对已存在的字符串进行 APPEND
redis> APPEND phone "11" # 长度从 6 个字符增加到 8 个字符
(integer) 8
redis> GET phone
"iphone11"