redis数据类型操作之String类型常用操作


redis 127.0.0.1:6379> set name "zhangshan" //给key name赋值zhangshan
OK
redis 127.0.0.1:6379> get name
"zhangshan"

getrange 字符串截取
redis 127.0.0.1:6379> getrange name 0 3 //截取 0-3的子字符串
"zhan"

getset 设置新值并返回老值
redis 127.0.0.1:6379> getset name "wangwu" //给指定的key赋值并返回之前的值
"zhangshan"
redis 127.0.0.1:6379> get name //再查看name的值变成了wamgwu
"wangwu"

setex 设置key值并设置过期时间
redis 127.0.0.1:6379> setex name 30 "zbb" //设置key为name的key的值为zbb,并设置过期时间3秒
OK
redis 127.0.0.1:6379> get name
"zbb"
redis 127.0.0.1:6379> ttl name //剩下13秒后过期
(integer) 13

strlen 获得字符串值的长度。
redis 127.0.0.1:6379> set name "zbbbbb"
OK
redis 127.0.0.1:6379> strlen name //获得key为name存贮的字符串值的长度
(integer) 6

mset 同时设置多个键值对
redis 127.0.0.1:6379> mset key1 "c++" key2 "c#" key3 "java" key4 "node.js"
OK
redis 127.0.0.1:6379> get key1
"c++"
redis 127.0.0.1:6379> get key2
"c#"
redis 127.0.0.1:6379> get key3
"java"
redis 127.0.0.1:6379> get key4
"node.js"

append 追加字符串
redis 127.0.0.1:6379> set zbb_key "ku bi cheng xu yuan !"
OK
redis 127.0.0.1:6379> append zbb_key " yes we all" //在key zbb_key存贮的字符串后追加" yes we all"
(integer) 29
redis 127.0.0.1:6379> get zbb_key //再查看结果,已经追加上去了
"ku bi cheng xu yuan ! yes we all"
redis 127.0.0.1:6379>

你可能感兴趣的:(redis)