(二)Redis数据类型和操作
1、String
1、set/get
2、mget:获取多个key值
3、incr && incrby:自增操作 / 增加指定值
4、decr && decry:自减 / 减指定值
5、setnx 【set if not exist】:如果不存在则设置该值,返回1,否则不设置,返回0
6、setex:set expire 单位:秒;设置有效期
7、getrange: 获取key对应value的子字符串
8、mset:批量设置多个key值,失败返回0;原子性
9、msetnx:不存在则批量设置
10、getset:设置key的值,并返回旧的key值
11、append:给指定字符串追加新的字符串,并返回字符串的长度
127.0.0.1:6379> set name zuizui
OK
127.0.0.1:6379> get name
"zuizui"
127.0.0.1:6379> set age 12
OK
127.0.0.1:6379> mget name age
1) "zuizui"
2) "12"
127.0.0.1:6379> incr age
(integer) 13
127.0.0.1:6379> incr age
(integer) 14
127.0.0.1:6379> incr age
(integer) 15
127.0.0.1:6379> incrby age 5
(integer) 20
127.0.0.1:6379> decr age
(integer) 19
127.0.0.1:6379>
127.0.0.1:6379> decr age
(integer) 18
127.0.0.1:6379> decrby age 5
(integer) 13
127.0.0.1:6379> setnx name aaa
(integer) 0
127.0.0.1:6379> get name
"zuizui"
127.0.0.1:6379> setnx gender 1
(integer) 1
127.0.0.1:6379> get gender
"1"
127.0.0.1:6379> setex name 10 zuier
OK
127.0.0.1:6379> get name
"zuier"
127.0.0.1:6379> get name
"zuier"
127.0.0.1:6379> get name
"zuier"
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> set name zuizui
OK
127.0.0.1:6379> getrange name 1 2
"ui"
127.0.0.1:6379> mset a 1 b 2 c 3 d 4
OK
127.0.0.1:6379> mget a b c d
1) "1"
2) "2"
3) "3"
4) "4"
127.0.0.1:6379> msetnx g 7 h 8 a 1
(integer) 0
127.0.0.1:6379> getset name heihei
"hehe"
127.0.0.1:6379> getset name haha
"heihei"
127.0.0.1:6379> append name aaaa
(integer) 8
127.0.0.1:6379> get name
"hahaaaaa"
2、HASH
127.0.0.1:6379> hset user name zuizui
(integer) 1
127.0.0.1:6379> hset user age 15
(integer) 1
127.0.0.1:6379> hget user name
"zuizui"
127.0.0.1:6379> hgetall user
1) "name"
2) "zuizui"
3) "age"
4) "15"
127.0.0.1:6379> hmset user1 name hehe age 16
OK
127.0.0.1:6379> hgetall user1
1) "name"
2) "hehe"
3) "age"
4) "16"
127.0.0.1:6379> hlen user
(integer) 2
127.0.0.1:6379> hlen user1
(integer) 2
127.0.0.1:6379> hset user gender 1
(integer) 1
127.0.0.1:6379> hlen user
(integer) 3
3、List
1、lpush:在list的头部添加一个元素
2、lrange:获取指定下标范围的元素
3、lpop:从尾部删除一个元素
4、rpush:尾部添加一个元素
5、rpop:从尾部删除一个元素并返回
6、blpop、brpop:lpop的阻塞版,
- blpop key timeout :timeout为阻塞时间,timeout=0为永远不阻塞
127.0.0.1:6379> lpush list 1 2 3 4 5
(integer) 5
127.0.0.1:6379> lrange list 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379> lrange list 0 5
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379> lpush list 6 7 8 9
(integer) 9
- Lpush + lpop = Stack
- Lpush + Rpop = Queue
- Lpush + Ltrim. = Capped Collection
- Lpush + Brpop = Message Queue
4、Set
1、sadd:添加到集合
2、spop:随机移除一个元素
3、
- smembers:列出集合中所有元素
- scard:计算集合大小
- Srandmember count 从集合中随机挑出count个元素
- spop:从集合中随机弹出一个元素
4、sdiff set1 set2:求两个集合的差集
5、sunion set1 set2:并集
6、sinter set1 set2:交集
127.0.0.1:6379> sadd set 1 2 3 4
(integer) 4
127.0.0.1:6379> smembers set
1) "1"
2) "2"
3) "3"
4) "4"
127.0.0.1:6379> spop set
"3"
127.0.0.1:6379> smembers set
1) "1"
2) "2"
3) "4"
127.0.0.1:6379> sadd set1 3 4 5 6
(integer) 4
127.0.0.1:6379> sdiff set set1
1) "1"
2) "2"
5、ZSet
1、zadd:在key对应的zset中添加一个元素
2、zrange:获取key对应的zset中指定范围的元素,-1表示获取所有元素
3、zrem:删除key对应的zset中的一个元素
4、zrangebyscore:返回有序集key小红,指定分数范围的元素列表
5、zrank:返回指定member的排名,其中member按score值递增
127.0.0.1:6379> zadd zset 1 "one" 2 "two"
(integer) 2
127.0.0.1:6379> zadd zset 3 "three"
(integer) 1
127.0.0.1:6379> zrange zset 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379> zrange zset 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
127.0.0.1:6379> zrem zset one
(integer) 1
127.0.0.1:6379> zrange zset 0 -1
1) "two"
2) "three"
127.0.0.1:6379> zrangebyscore zset 0 10
1) "two"
2) "three"
# 获取名次
127.0.0.1:6379> zrank zset three
(integer) 1
127.0.0.1:6379> zrank zset two
(integer) 0