前言:
推荐免费Redis基础讲解视频:【狂神说Java】Redis最新超详细版教程通俗易懂_哔哩哔哩_bilibili
String字符串类型详解
常用命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 127.0.0.1:6379> set name jokerdig # 设置值 OK 127.0.0.1:6379> get name # 获取值 "jokerdig" 127.0.0.1:6379> keys * # 获取所有key 1) "mylist" 2) "counter:__rand_int__" 3) "key:__rand_int__" 4) "name" 5) "myhash" 127.0.0.1:6379> exists name # 判断key是否存在 (integer) 1 127.0.0.1:6379> append name ".com" # 追加字符串,当key不存在,就同set (integer) 20 127.0.0.1:6379> get name "jokerdig.com" 127.0.0.1:6379> strlen name # 获得字符串的长度 (integer) 20 127.0.0.1:6379>
i++实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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> get views "-1" 127.0.0.1:6379> incrby views 10 # 设置步长 (integer) 9 127.0.0.1:6379> decrby views 10 (integer) -1 127.0.0.1:6379>
字符串范围 range
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 127.0.0.1:6379[1]> keys * (empty array) 127.0.0.1:6379[1]> set key1 "jokerdig.com" OK 127.0.0.1:6379[1]> get keys (nil) 127.0.0.1:6379[1]> get key1 "jokerdig.com" 127.0.0.1:6379[1]> getrange key1 0 5 # 截取字符串(0,3] "jokerd" 127.0.0.1:6379[1]> getrange key1 0 -1 # 获取全部字符串 "jokerdig.com" 127.0.0.1:6379[1]> setrange key1 2 xx # 替换指定位置的字符串 (integer) 12 127.0.0.1:6379[1]> get key1 "joxxrdig.com" 127.0.0.1:6379[1]>
设置有效时间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 127.0.0.1:6379[1]> setex key3 20 "hello" # 指定key3的值,并设置30s后过期 OK 127.0.0.1:6379[1]> ttl key3 (integer) 13 127.0.0.1:6379[1]> setnx key4 "redis" # 如果key4不存在,就创建key4 (integer) 1 127.0.0.1:6379[1]> keys * 1) "key1" 2) "key4" 127.0.0.1:6379[1]> ttl kye3 (integer) -2 127.0.0.1:6379[1]> setnx key4 "db" # 如果key4存在,就创建失败 (integer) 0 127.0.0.1:6379[1]> get key4 "redis" 127.0.0.1:6379[1]>
批量设置和获取值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 127.0.0.1:6379[1]> flushdb # 清空 OK 127.0.0.1:6379[1]> keys * (empty array) 127.0.0.1:6379[1]> mset k1 v1 k2 v2 k3 v3 # 同时设置多个值 OK 127.0.0.1:6379[1]> keys * 1) "k3" 2) "k2" 3) "k1" 127.0.0.1:6379[1]> mget k1 k2 k3 # 同时获取多个值 1) "v1" 2) "v2" 3) "v3" 127.0.0.1:6379[1]> msetnx k1 v1 k4 v4 # msetnx是原子性操作 (integer) 0 127.0.0.1:6379[1]> get k4 (nil) 127.0.0.1:6379[1]>
设置对象
1 2 3 4 5 6 7 8 9 # 常用设置对象方法 # set user:1{name:jokerdig,age:18} # 还可以这样设置对象 127.0.0.1:6379[1]> mset user:1:name jokerdig user:1:age 18 OK 127.0.0.1:6379[1]> mget user:1:name user:1:age 1) "jokerdig" 2) "18" 127.0.0.1:6379[1]>
组合命令
1 2 3 4 5 6 7 8 9 127.0.0.1:6379[1]> getset db redis # 如果不存在值,就返回nil (nil) 127.0.0.1:6379[1]> get db "redis" 127.0.0.1:6379[1]> getset db mongdb # 如果存在值,就获取原来的值,并设置新的值 "redis" 127.0.0.1:6379[1]> get db "mongdb" 127.0.0.1:6379[1]>
List列表类型详解
常用基本命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 127.0.0.1:6379[1]> LPUSH list one # 将一个或多个值插入列表头部 (integer) 1 127.0.0.1:6379[1]> LPUSH list two (integer) 2 127.0.0.1:6379[1]> LPUSH list three (integer) 3 127.0.0.1:6379[1]> LRANGE list 0 -1 # 获取范围内的值,0 -1获取所有值 1) "three" 2) "two" 3) "one" # 127.0.0.1:6379[1]> RPUSH list four # 将一个或多个值,插入列表尾部 (integer) 4 127.0.0.1:6379[1]> LRANGE list 0 -1 1) "three" 2) "two" 3) "one" 4) "four" 127.0.0.1:6379[1]> LPUSH list five # 将一个或多个值,插入列表头部 (integer) 5 127.0.0.1:6379[1]> LRANGE list 0 -1 1) "five" 2) "three" 3) "two" 4) "one" 5) "four" # 127.0.0.1:6379[1]> RPOP list # 移除最后一个元素 "four" 127.0.0.1:6379[1]> LPOP list # 移除第一个元素 "five" # 127.0.0.1:6379[1]> LRANGE list 0 -1 1) "three" 2) "two" 3) "one" 127.0.0.1:6379[1]> lindex list 0 # 通过下标获取值 "three" # 127.0.0.1:6379[1]> llen list # 返回列表长度 (integer) 3 # 127.0.0.1:6379[1]> LPUSH list three (integer) 4 127.0.0.1:6379[1]> LRANGE list 0 -1 1) "three" 2) "three" 3) "two" 4) "one" 127.0.0.1:6379[1]> lrem list 1 one # 按指定数量移除指定的值 (integer) 1 127.0.0.1:6379[1]> LRANGE list 0 -1 1) "three" 2) "three" 3) "two" 127.0.0.1:6379[1]> lrem list 2 three (integer) 2 127.0.0.1:6379[1]> LRANGE list 0 -1 1) "two"
截断列表元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 127.0.0.1:6379[1]> Rpush list1 "jokerdig" (integer) 1 127.0.0.1:6379[1]> Rpush list1 "jokerdig1" (integer) 2 127.0.0.1:6379[1]> Rpush list1 "jokerdig2" (integer) 3 127.0.0.1:6379[1]> Rpush list1 "jokerdig3" (integer) 4 127.0.0.1:6379[1]> Rpush list1 "jokerdig4" (integer) 5 127.0.0.1:6379[1]> LRANGE list1 0 -1 1) "jokerdig" 2) "jokerdig1" 3) "jokerdig2" 4) "jokerdig3" 5) "jokerdig4" 127.0.0.1:6379[1]> ltrim list1 1 3 # 根据指定范围截取指定列表长度,list1已经被改变 OK 127.0.0.1:6379[1]> LRANGE list1 0 -1 1) "jokerdig1" 2) "jokerdig2" 3) "jokerdig3"
rpoplpush
1 2 3 4 5 6 7 8 9 10 11 12 13 14 # 移除列表最后一个元素,并添加一个新的列表中 127.0.0.1:6379[1]> rpush list2 "hello1" (integer) 1 127.0.0.1:6379[1]> rpush list2 "hello2" (integer) 2 127.0.0.1:6379[1]> rpush list2 "hello3" (integer) 3 127.0.0.1:6379[1]> rpoplpush list2 list2_1 "hello3" 127.0.0.1:6379[1]> lrange list2 0 -1 1) "hello1" 2) "hello2" 127.0.0.1:6379[1]> lrange list2_1 0 -1 1) "hello3"
lset
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # 将列表中指定下标的值替换为另一个值(更新) 127.0.0.1:6379[1]> exists list # 判断列表是否存在 (integer) 0 127.0.0.1:6379[1]> lset list 0 hello # 如果不存在,会报错 (error) ERR no such key 127.0.0.1:6379[1]> lpush list value1 (integer) 1 127.0.0.1:6379[1]> lrange list 0 -1 1) "value1" 127.0.0.1:6379[1]> lset list 0 hello OK 127.0.0.1:6379[1]> lrange list 0 -1 1) "hello" 127.0.0.1:6379[1]> lset list 1 hello1 (error) ERR index out of range
linsert
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 # 将某个具体的value插入到列表中某个元素的前面或后面 127.0.0.1:6379[1]> rpush list1 "hello" (integer) 1 127.0.0.1:6379[1]> rpush list1 "hello1" (integer) 2 127.0.0.1:6379[1]> linsert list1 before hello1 hello2 (integer) 3 127.0.0.1:6379[1]> lrange list1 0 -1 1) "hello" 2) "hello2" 3) "hello1" 127.0.0.1:6379[1]> linsert list1 after hello1 hello3 (integer) 4 127.0.0.1:6379[1]> lrange list1 0 -1 1) "hello" 2) "hello2" 3) "hello1" 4) "hello3"
小结
list实际上是一个链表,before Node after,left,right都可以插入值;
如果key不存在,就创建新的链表;
如果key存在,新增内容;
如果移除所有值(空链表),也代表不存在;
在两边插入值效率高,插入中间元素,效率偏低;
Set集合类型详解
set无序不重复集合
基本命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 127.0.0.1:6379[1]> sadd myset "hello" # set集合中添加值 (integer) 1 127.0.0.1:6379[1]> sadd myset "jokerdig" (integer) 1 127.0.0.1:6379[1]> sadd myset "joker" (integer) 1 127.0.0.1:6379[1]> smembers myset # 查看指定set的所有值 1) "jokerdig" 2) "joker" 3) "hello" 127.0.0.1:6379[1]> sismember myset hello # 判断某个值是否在set集合 (integer) 1 127.0.0.1:6379[1]> sismember myset world (integer) 0
获取和移除set元素
1 2 3 4 5 6 7 127.0.0.1:6379[1]> srem myset hello # 移除set集合中指定的元素 (integer) 1 127.0.0.1:6379[1]> scard myset # 获取set集合中的元素个数 (integer) 2 127.0.0.1:6379[1]> smembers myset 1) "jokerdig" 2) "joker"
srandmember
1 2 3 4 5 6 7 8 # 随机抽选结合中的一个元素 127.0.0.1:6379[1]> srandmember myset "joker" 127.0.0.1:6379[1]> srandmember myset "jokerdig" 127.0.0.1:6379[1]> srandmember myset 2 # 随机抽出指定个数的元素 1) "jokerdig" 2) "joker"
spop
1 2 3 4 5 # 随机删除set 集合中的元素 127.0.0.1:6379[1]> spop myset "joker" 127.0.0.1:6379[1]> smembers myset 1) "jokerdig"
移动值到另一个集合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 127.0.0.1:6379[1]> sadd myset "hello" (integer) 1 127.0.0.1:6379[1]> sadd myset "world" (integer) 1 127.0.0.1:6379[1]> sadd myset "jokerdig" (integer) 1 127.0.0.1:6379[1]> smove myset myset2 jokerdig (integer) 1 127.0.0.1:6379[1]> keys * 1) "myset2" 2) "myset" 127.0.0.1:6379[1]> smembers myset2 1) "jokerdig" 127.0.0.1:6379[1]> smembers myset 1) "world" 2) "hello"
两个集合值的关系
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 127.0.0.1:6379[1]> sadd key1 a (integer) 1 127.0.0.1:6379[1]> sadd key1 b (integer) 1 127.0.0.1:6379[1]> sadd key1 c (integer) 1 127.0.0.1:6379[1]> sadd key2 b (integer) 1 127.0.0.1:6379[1]> sadd key2 c (integer) 1 127.0.0.1:6379[1]> sadd key2 d (integer) 1 127.0.0.1:6379[1]> sdiff key1 key2 # 查集 1) "a" 127.0.0.1:6379[1]> sinter key1 key2 # 交集 1) "c" 2) "b" 127.0.0.1:6379[1]> sunion key1 key2 # 并集 1) "c" 2) "b" 3) "d" 4) "a"
Hash哈希类型详解
Map集合:key-map(key-value)
基本命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 127.0.0.1:6379[1]> hset myhash field1 jokerdig # set一个具体的key-value (integer) 1 127.0.0.1:6379[1]> hget myhash field1 # 获取一个字段值 "jokerdig" 127.0.0.1:6379[1]> hmset myhash field1 hello field1 world # set多个key-value OK 127.0.0.1:6379[1]> hmget myhash field1 field2# 获取多个字段值 1) "hello" 2) "world" 127.0.0.1:6379[1]> hgetall myhash# 获取全部字段值 1) "field1" 2) "hello" 3) "field2" 4) "world" 127.0.0.1:6379[1]> hdel myhash field1 # 删除指定的值 (integer) 1 127.0.0.1:6379[1]> hgetall myhash 1) "field2" 2) "world" 127.0.0.1:6379[1]>
获取长度
1 2 127.0.0.1:6379[1]> hlen myhash # 获取字段长度 (integer) 1
判断字段是否存在
1 2 3 4 127.0.0.1:6379[1]> hexists myhash field1 (integer) 0 127.0.0.1:6379[1]> hexists myhash field2 (integer) 1
获得所有key和value
1 2 3 4 127.0.0.1:6379[1]> hkeys myhash 1) "field2" 127.0.0.1:6379[1]> hvals myhash 1) "world"
指定增量及hsetnx
1 2 3 4 5 6 7 8 9 10 11 127.0.0.1:6379[1]> hset myhash field2 3 # 指定增量 (integer) 0 127.0.0.1:6379[1]> hset myhash field1 3 (integer) 1 127.0.0.1:6379[1]> hincrby myhash field1 1 (integer) 4 127.0.0.1:6379[1]> hsetnx myhash field2 world # 如果存在则设置失败 (integer) 0 127.0.0.1:6379[1]> hsetnx myhash field3 world # 不存在则设置成功 (integer) 1 127.0.0.1:6379[1]>
哈希更适合存放对象
Zset有序集合详解
在set的基础上增加一个值,可以排序
基本命令
1 2 3 4 5 6 7 8 9 10 127.0.0.1:6379[1]> zadd myzset 1 one (integer) 1 127.0.0.1:6379[1]> zadd myzset 2 two 3 three (integer) 2 127.0.0.1:6379[1]> zrange myzset 0 -1 1) "one" 2) "two" 3) "three" 127.0.0.1:6379[1]> zcard myzset # 获取有序结合个数 (integer) 3
排序实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 127.0.0.1:6379[1]> zadd salary 2500 joker (integer) 1 127.0.0.1:6379[1]> zadd salary 5000 zhangsan (integer) 1 127.0.0.1:6379[1]> zadd salary 1000 tiangou (integer) 1 127.0.0.1:6379[1]> zrangebyscore salary -inf +inf # 正序排序 条件是最小值和最大值,可以是负无穷-正无穷 1) "tiangou" 2) "joker" 3) "zhangsan" 127.0.0.1:6379[1]> zrevrange salary 0 -1 # 负序排序 1) "joker" 2) "zhangsan" 3) "tiangou" 127.0.0.1:6379[1]> zrangebyscore salary -inf +inf withscores # 正序排序并附带信息 1) "tiangou" 2) "1000" 3) "joker" 4) "2500" 5) "zhangsan" 6) "5000"
移除元素
1 2 3 4 5 6 7 8 9 127.0.0.1:6379[1]> zrange salary 0 -1 1) "tiangou" 2) "joker" 3) "zhangsan" 127.0.0.1:6379[1]> zrem salary tiangou (integer) 1 127.0.0.1:6379[1]> zrange salary 0 -1 1) "joker" 2) "zhangsan"
区间获取
1 2 3 4 5 6 127.0.0.1:6379[1]> zadd myzset1 1 hello (integer) 1 127.0.0.1:6379[1]> zadd myzset1 2 world 3 jokerdig (integer) 2 127.0.0.1:6379[1]> zcount myzset1 1 3 # 根据区间获取值的数量 (integer) 3