Redis笔记(4)redis五大数据类型1----string(字符串)

string(字符串)

string类型使用场景

  • 计数器
  • 统计数量
  • 对象存储
append key value    字符串追加,返回字符串长度
strlen key      	返回字符串长度
127.0.0.1:6379> set key1 abc  //设置
OK
127.0.0.1:6379> get key1
"abc"
127.0.0.1:6379> append key1 ddd
(integer) 6
127.0.0.1:6379> get key1
"abcddd"
127.0.0.1:6379> strlen key1
(integer) 6
incr key     自增1
decr key     自减1
incrby    	 步长增加
decrby       步长减少
127.0.0.1:6379> set num 0
OK
127.0.0.1:6379> incr num
(integer) 1
127.0.0.1:6379> incr num
(integer) 2
127.0.0.1:6379> get num
"2"
127.0.0.1:6379> decr num
(integer) 1
127.0.0.1:6379> incrby num 10
(integer) 11
127.0.0.1:6379> decrby num 5
(integer) 6
GETRANGE  key start end     字符串截取,下标开始和结束,-1相当于get key
SETRANGE key offset value   字符串替换,开始的下标,替换的字符串
127.0.0.1:6379> get key1
"abcddd"
127.0.0.1:6379> GETRANGE key1 0 2
"abc"
127.0.0.1:6379> GETRANGE key1 0 -1
"abcddd"
127.0.0.1:6379> SETRANGE key1 1 xx
(integer) 6
127.0.0.1:6379> get key1
"axxddd"
setex key seconds value   设置一个值多少秒过期
setnx key value           如果key不存在就设置,存在会设置失败
127.0.0.1:6379> setex key2 30 hello
OK
127.0.0.1:6379> ttl key2  //查看剩余时间
(integer) 25
127.0.0.1:6379> setnx key3 redis
(integer) 1
127.0.0.1:6379> get key3
"redis"
127.0.0.1:6379> setnx key3 mongodb
(integer) 0
127.0.0.1:6379> get key3
"redis"
127.0.0.1:6379> 
mset key value [key ···]   批量设置
mget key [key ···]    	   批量获取
msetnx key value [key ···] 批量检测设置,一个失败即全部失败
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3
OK
127.0.0.1:6379> keys * 
1) "k1"
2) "k3"
3) "k2"
127.0.0.1:6379> mget k1 k2 k3
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379> msetnx k1 123 k4 456
(integer) 0
127.0.0.1:6379> keys *
1) "k1"
2) "k3"
3) "k2"
127.0.0.1:6379> mset user:1:name zhangsan user:1:age 16 //可用作保存对象
OK
127.0.0.1:6379> mget user:1:name user:1:age
1) "zhangsan"
2) "16"
getset key value   先拿值,在设值
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> getset key1 123
(nil)
127.0.0.1:6379> get key1
"123"
127.0.0.1:6379> getset key1 456
"123"
127.0.0.1:6379> get key1
"456"
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页