基础
127.0.0.1:6379> set name zhangsan #设置值
OK
127.0.0.1:6379> get name #获取值
"zhangsan"
127.0.0.1:6379> keys * #查看所有key
1) "name"
127.0.0.1:6379> exists name #查看某个key是否存在
(integer) 1
127.0.0.1:6379> append name "(fawaikuangtu)" #给指定Key的Value追加值(key不存在直接新建key)
(integer) 22
127.0.0.1:6379> get name
"zhangsan(fawaikuangtu)"
127.0.0.1:6379> strlen name #获取字符串长度
(integer) 22
自增 自减 设置步长
127.0.0.1:6379> set views 0 #设置初始数据
OK
127.0.0.1:6379> get views
"0"
127.0.0.1:6379> incr views #自增1
(integer) 1
127.0.0.1:6379> incr views
(integer) 2
127.0.0.1:6379> get views
"2"
127.0.0.1:6379> decr views #自减1
(integer) 1
127.0.0.1:6379> decr views
(integer) 0
127.0.0.1:6379> decr views
(integer) -1
127.0.0.1:6379> incrby views 10 #增加指定数
(integer) 9
127.0.0.1:6379> get views
"9"
127.0.0.1:6379> decrby views 5 #减去指定数
(integer) 4
127.0.0.1:6379> get views
"4"
Range 字符串范围
127.0.0.1:6379> set key helloworld
OK
127.0.0.1:6379> get key
"helloworld"
127.0.0.1:6379> getrange key 0 3 #截取字符串[0,3]
"hell"
127.0.0.1:6379> getrange key 0 -1 #获取全部字符串
"helloworld"
127.0.0.1:6379> setrange key 5 wqxxx #替换指定位置开始的字符串
(integer) 10
127.0.0.1:6379> get key
"hellowqxxx "
setex setnx
127.0.0.1:6379> setex key2 30 "hello" #设置key2的值为hello 30秒后过期
OK
127.0.0.1:6379> ttl key2
(integer) 28
127.0.0.1:6379> get key2
"hello"
127.0.0.1:6379> setnx key3 "666" #如果key3不存在 创建
(integer) 1
127.0.0.1:6379> keys *
1) "views"
2) "name"
3) "key"
4) "key3"
127.0.0.1:6379> setnx key3 "888" #如果key3存在 创建失败
(integer) 0
127.0.0.1:6379> get key3
"666"
mset mget
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 #同时设置多个值
OK
127.0.0.1:6379> keys *
1) "k3"
2) "k1"
3) "k2"
127.0.0.1:6379> mget k1 k2 k3 #同时获取多个值
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379> msetnx k1 v1 k4 v4 #msetnx是一个原子性的操作,成功都成功,失败都失败
(integer) 0
127.0.0.1:6379> get k4
(nil)
对象
127.0.0.1:6379> mset user:1:name zhangsan user:1:age 12
OK
127.0.0.1:6379> mget user:1:name user:1:age
1) "zhangsan"
2) "12"
先get再set getset
127.0.0.1:6379> getset db mongodb #如果不存在值,返回nil 设置新值
(nil)
127.0.0.1:6379> get db
"mongodb"
127.0.0.1:6379> getset db mysql #如果存在值,获取原来的值,并设置新的值
"mongodb"
127.0.0.1:6379> get db
"mysql"