1、简单的命令
127.0.0.1:6379> set key1 v1
OK
127.0.0.1:6379> get key1
"v1"
127.0.0.1:6379> keys *
1) "age"
2) "key1"
3) "name"
127.0.0.1:6379> exists key1
(integer) 1
127.0.0.1:6379> append key1 "hello"
(integer) 7
127.0.0.1:6379> get key1
"v1hello"
127.0.0.1:6379> strlen key1
(integer) 7
2、自增、自减命令
127.0.0.1:6379> set views 0
OK
127.0.0.1:6379> get views
"0"
127.0.0.1:6379> incr views
(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
(integer) 1
127.0.0.1:6379> get views
"1"
3、自增、自减步长命令
127.0.0.1:6379> incrby views 10 # 步长加10
(integer) 11
127.0.0.1:6379> decrby views 5 # 步长减5
(integer) 6
4、获取字符串范围命令
127.0.0.1:6379> set key1 "abcdefg"
OK
127.0.0.1:6379> get key1
"abcdefg"
127.0.0.1:6379> getrange key1 0 3
"abcd"
127.0.0.1:6379> getrange key1 0 -2
"abcdef"
127.0.0.1:6379> set key2 "123456"
OK
127.0.0.1:6379> get key2
"123456"
127.0.0.1:6379> setrange key2 1 xx
(integer) 6
127.0.0.1:6379> get key2
"1xx456"
5、setex和setnx命令
127.0.0.1:6379> setex key3 30 "hello"
OK
127.0.0.1:6379> ttl key3
(integer) 25
127.0.0.1:6379> get key3
"hello"
127.0.0.1:6379> setnx mykey "redis"
(integer) 1
127.0.0.1:6379> keys *
1) "mykey"
2) "key1"
3) "key2"
127.0.0.1:6379> ttl key3
(integer) -2
6、mset 和mset 命令
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3
OK
127.0.0.1:6379> keys *
1) "k3"
2) "k2"
3) "k1"
127.0.0.1:6379> get k2
"v2"
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> mset k1 k3
1) "v1"
2) "v3"
7、getset命令
127.0.0.1:6379> getset db redis # 先获取值,没有就返回nil,然后在设置值
(nil)
127.0.0.1:6379> get db
"redis"
127.0.0.1:6379> getset db "mongdb" # 先获取值,存在就返回该值,然后在设置值
"redis"
127.0.0.1:6379> get db
"mongdb"
总结:
String类似的使用场景:value除了是我们的字符串还可以是我们的数字!
- 计数器
- 统计多单位的数量
- 粉丝数
- 对象缓存存储!