127.0.0.1:6379[1]> KEYS *
1) "zhangsan"
2) "lisi"
3) "wangwu"
127.0.0.1:6379[1]> DEL zhangsan
(integer) 1
127.0.0.1:6379[1]> KEYS *
1) "lisi"
2) "wangwu"
127.0.0.1:6379[1]> EXISTS zhangsan
(integer) 0
127.0.0.1:6379[1]> EXISTS lisi wangwu
(integer) 2
127.0.0.1:6379[1]> EXPIRE lisi 20
(integer) 1
127.0.0.1:6379[1]> TTL lisi
(integer) 16
127.0.0.1:6379[1]> EXISTS lisi
(integer) 1
127.0.0.1:6379[1]> TTL lisi
(integer) -2
127.0.0.1:6379[1]> EXISTS lisi
(integer) 0
127.0.0.1:6379[1]> KEYS *
1) "wangwu"
字符串类型,value为字符串,格式有三种,本质上都是字符串
string:普通字符串
int:整数类型,可以做自增、自减操作
float:浮点类型,可以做自增、自减操作
127.0.0.1:6379[1]> KEYS *
1) "wangwu"
127.0.0.1:6379[1]> SET zhangsan 20
OK
127.0.0.1:6379[1]> KEYS *
1) "zhangsan"
2) "wangwu"
127.0.0.1:6379[1]> SET zhangsan 15
OK
127.0.0.1:6379[1]> GET zhangsan
"15"
127.0.0.1:6379[1]> GET wangwu
"18"
127.0.0.1:6379[1]> KEYS *
1) "zhangsan"
2) "wangwu"
127.0.0.1:6379[1]> MSET lisi 20 zhangsan 25 zhaoliu 22
OK
127.0.0.1:6379[1]> KEYS *
1) "zhangsan"
2) "lisi"
3) "zhaoliu"
4) "wangwu"
127.0.0.1:6379[1]> MGET zhangsan lisi zhaoliu
1) "25"
2) "20"
3) "22"
127.0.0.1:6379[1]> GET zhangsan
"25"
127.0.0.1:6379[1]> INCR zhangsan
(integer) 26
127.0.0.1:6379[1]> INCR zhangsan
(integer) 27
127.0.0.1:6379[1]> GET zhangsan
"27"
127.0.0.1:6379[1]> INCRBY zhangsan 2
(integer) 29
127.0.0.1:6379[1]> INCRBY zhangsan -1
(integer) 28
127.0.0.1:6379[1]> INCRBY zhangsan -1
(integer) 27
127.0.0.1:6379[1]> SET float 17.5
OK
127.0.0.1:6379[1]> GET float
"17.5"
127.0.0.1:6379[1]> INCRBYFLOAT float 1.5
"19"
127.0.0.1:6379[1]>
127.0.0.1:6379[1]> INCRBYFLOAT float 1.5
"20.5"
127.0.0.1:6379[1]> KEYS *
1) "zhangsan"
2) "lisi"
3) "zhaoliu"
4) "wangwu"
127.0.0.1:6379[1]> SETNX zhangsan 11
(integer) 0
127.0.0.1:6379[1]> GET zhangsan
"27"
127.0.0.1:6379[1]> SETNX child 11
(integer) 1
127.0.0.1:6379[1]> GET child
"11"
127.0.0.1:6379[1]> SETEX tom 10 aaa
OK
127.0.0.1:6379[1]> TTL tom
(integer) 5
如果想存储两个相同的KEY,可以用:隔开多个单词形成层级结构,在图形化的页面上可以看到层级结构
127.0.0.1:6379[1]> set com:redisStudy1:zhangsan 20
OK
127.0.0.1:6379[1]> set com:redisStudy2:zhangsan 22
OK
127.0.0.1:6379[1]> set com:redisStudy1:lisi 20
OK
127.0.0.1:6379[1]> set com:redisStudy2:lisi 30
OK
Hash类型,也叫散列,其value是一个无序字典,类似于Java中的HashMap结构。String结构是将对象序列化为JSON字符串后存储,当需要修改对象某个字段时需要将整个字符串替代,很不方便。Hash结构可以将对象中的每个字段独立存储,可以针对单个字段做CRUD。
127.0.0.1:6379[2]> hset zhangsan age 18 sex 0
(integer) 2
127.0.0.1:6379[2]> KEYS *
1) "zhangsan"
127.0.0.1:6379[2]> HGET zhangsan age
"18"
127.0.0.1:6379[2]> HGETALL zhangsan
1) "age"
2) "18"
3) "sex"
4) "0"
127.0.0.1:6379[2]> HKEYS zhangsan
1) "age"
2) "sex"
127.0.0.1:6379[2]> HVALS zhangsan
1) "18"
2) "0"
127.0.0.1:6379[2]> HINCRBY zhangsan age 1
(integer) 19
127.0.0.1:6379[2]> HINCRBY zhangsan age 1
(integer) 20
Redis中的list类型类似双向链表结构,支持正向搜索和反向搜索。特点:
127.0.0.1:6379[3]> LPUSH namelist zhangsan lisi
(integer) 2
127.0.0.1:6379[3]> LRANGE namelist 0 1
1) "lisi"
2) "zhangsan"
127.0.0.1:6379[3]> LPOP namelist
"lisi"
127.0.0.1:6379[3]> LRANGE namelist 0 2
1) "zhangsan"
127.0.0.1:6379[3]> RPUSH namelist wangwu zhangsan zhaoliu
(integer) 4
127.0.0.1:6379[3]> LRANGE namelist 0 5
1) "zhangsan"
2) "wangwu"
3) "zhangsan"
4) "zhaoliu"
127.0.0.1:6379[3]> RPOP namelist
"zhaoliu"
127.0.0.1:6379[3]> LRANGE namelist 0 5
1) "zhangsan"
2) "wangwu"
3) "zhangsan"
127.0.0.1:6379[3]> LRANGE namelist 0 5
1) "zhangsan"
2) "wangwu"
3) "zhangsan"
127.0.0.1:6379[3]> LRANGE namelist 0 5
1) "lisi"
2) "zhangsan"
127.0.0.1:6379[3]> LSET namelist 0 wangwu
OK
127.0.0.1:6379[3]> LRANGE namelist 0 5
1) "wangwu"
2) "zhangsan"
set可以看做一个value都为空的hashMap,set类型特点:
127.0.0.1:6379[4]> SADD zhangsan lisi wangwu
(integer) 2
127.0.0.1:6379[4]> SADD lisi wangwu zhangsan zhaoliu mazi
(integer) 4
127.0.0.1:6379[4]> SMEMBERS zhangsan
1) "lisi"
2) "wangwu"
127.0.0.1:6379[4]> SMEMBERS lisi
1) "zhangsan"
2) "mazi"
3) "zhaoliu"
4) "wangwu"
127.0.0.1:6379[4]> SREM zhangsan wangwu
(integer) 1
127.0.0.1:6379[4]> SMEMBERS zhangsan
1) "lisi"
127.0.0.1:6379[4]> SCARD lisi
(integer) 4
127.0.0.1:6379[4]> SISMEMBER zhangsan wangwu
(integer) 0
127.0.0.1:6379[4]> SISMEMBER lisi wangwu
(integer) 1
127.0.0.1:6379[4]> SMEMBERS lisi
1) "zhangsan"
2) "mazi"
3) "zhaoliu"
4) "wangwu"
127.0.0.1:6379[4]> SINTER zhangsan lisi
(empty array)
127.0.0.1:6379[4]> SDIFF zhangsan lisi
1) "lisi"
127.0.0.1:6379[4]> SDIFF lisi zhangsan
1) "zhangsan"
2) "wangwu"
3) "zhaoliu"
4) "mazi"
127.0.0.1:6379[4]> SUNION zhangsan lisi
1) "zhangsan"
2) "lisi"
3) "zhaoliu"
4) "mazi"
5) "wangwu"
ZSET是一个可排序的SET,SET里面的每一个元素有一个score属性,可以根据score属性排序,ZSET特点:
ZSET常被用来实现排行榜功能
127.0.0.1:6379[5]> ZADD nameset 100 zhangsan 98 lisi
(integer) 2
127.0.0.1:6379[5]> ZREM nameset zhangsan
(integer) 1
127.0.0.1:6379[5]> ZSCORE nameset lisi
"98"
127.0.0.1:6379[5]> ZRANGEBYSCORE nameset 1 100
1) "wangwu"
2) "zhangsan"
3) "zhaoliu"
4) "mazi"
5) "lisi"
127.0.0.1:6379[5]> ZRANK nameset mazi
(integer) 3
127.0.0.1:6379[5]> ZREVRANK nameset mazi
(integer) 1
127.0.0.1:6379[5]> ZCARD nameset
(integer) 5
127.0.0.1:6379[5]> ZCOUNT nameset 0 100
(integer) 5
127.0.0.1:6379[5]> ZSCORE nameset zhangsan
"66"
127.0.0.1:6379[5]> ZINCRBY nameset 2 zhangsan
"68"
127.0.0.1:6379[5]> ZINCRBY nameset 2 zhangsan
"70"
127.0.0.1:6379[5]> ZRANGE nameset 0 3
1) "wangwu"
2) "zhangsan"
3) "zhaoliu"
4) "mazi"
127.0.0.1:6379[5]> ZREVRANGE nameset 0 3
1) "lisi"
2) "mazi"
3) "zhaoliu"
4) "zhangsan"
127.0.0.1:6379[5]> ZRANGEBYSCORE nameset 0 100
1) "wangwu"
2) "zhangsan"
3) "zhaoliu"
4) "mazi"
5) "lisi"
127.0.0.1:6379[5]> ZREVRANGEBYSCORE nameset 100 0
1) "lisi"
2) "mazi"
3) "zhaoliu"
4) "zhangsan"
5) "wangwu"