作者: gh-xiaohe
gh-xiaohe的博客
觉得博主文章写的不错的话,希望大家三连(✌关注,✌点赞,✌评论),多多支持一下!!!
127.0.0.1:6379> flushall
OK
127.0.0.1:6379> set name gh
OK
127.0.0.1:6379> keys *
1) "name"
127.0.0.1:6379> set age 1
OK
127.0.0.1:6379> keys *
1) "age"
2) "name"
127.0.0.1:6379> exists name # 判断当前的key 是否存在 返回 1 存在
(integer) 1
127.0.0.1:6379> exists name1
(integer) 0
127.0.0.1:6379> move name 1 # 移除 当前的key 设置为1 从当前数据库 移除
(integer) 1 # 过期:缓存、热点数据、cookie、session
127.0.0.1:6379> KEYS *
1) "age"
127.0.0.1:6379> set name gh
OK
127.0.0.1:6379> KEYS *
1) "name"
2) "age"
127.0.0.1:6379> expire name 10 # 设置过期的时间 10 秒
(integer) 1
127.0.0.1:6379> ttl name # 查看当前key 的剩余时间
(integer) 7
127.0.0.1:6379> ttl name
(integer) 5
127.0.0.1:6379> ttl name # -2 代表过期
(integer) -2
127.0.0.1:6379> ttl age # -1 永不过期
(integer) -1
127.0.0.1:6379> get name # 时间一个过, 获取不到
(nil)
127.0.0.1:6379> set name gh
OK
127.0.0.1:6379> KEYS *
1) "name"
2) "age"
127.0.0.1:6379> type name # 查看当前 key 的类型
string
追加字符串 append 不存在 key 就相当于 set key
127.0.0.1:6379> set key1 v1
OK
127.0.0.1:6379> get key1
"v1"
127.0.0.1:6379> KEYS *
1) "key1"
127.0.0.1:6379> exists key1
(integer) 1
127.0.0.1:6379> APPEND key1 "hello" # 追加字符串,不存在 就相当于 set key
(integer) 7
127.0.0.1:6379> get key1
"v1hello"
127.0.0.1:6379> strlen key1
(integer) 7
127.0.0.1:6379> append key1 "gh"
(integer) 9
127.0.0.1:6379> get key1
"v1hellogh"
127.0.0.1:6379> append name "zhangsan"
(integer) 8
127.0.0.1:6379> KEYS *
1) "key1"
2) "name"
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> get views
"1"
127.0.0.1:6379> INCRBY views 10 # 浏览量 + 10
(integer) 11
127.0.0.1:6379> get views
"11"
127.0.0.1:6379> DECRBY views 10 # 浏览量 - 10
(integer) 1
127.0.0.1:6379> get views
"1"
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> set key1 "hello,word"
OK
127.0.0.1:6379> get key1
"hello,word"
127.0.0.1:6379> getrange key1 0 4 # 获取字符串的范围
"hello"
127.0.0.1:6379> getrange key1 0 -1 # 获取全部的字符串
"hello,word"
127.0.0.1:6379> set key2 1234567
OK
127.0.0.1:6379> get key2
"1234567"
127.0.0.1:6379> setrange key2 1 xx # 替换 索引为1 后的两个字符
(integer) 7
127.0.0.1:6379> get key2
"1xx4567"
127.0.0.1:6379> setex key3 30 "hello" # 设置过期时间 30 秒
OK
127.0.0.1:6379> ttl key3 # 显示 还剩多长时间过期
(integer) 23
127.0.0.1:6379> get key3
"hello"
127.0.0.1:6379> KEYS * # key3 已经过期
1) "key1"
2) "myley"
3) "key2"
不存在 在设置
返回 1 设置成功
返回 0 设置失败
127.0.0.1:6379> setnx mykey "redis" # 不存在 mykey 在创建 mykey 1 成功
(integer) 1
127.0.0.1:6379> KEYS *
1) "key1"
2) "myley"
3) "key2"
127.0.0.1:6379> setnx myley "1234567" # 存在 mykey 创建不成功 返回 0
(integer) 0
127.0.0.1:6379> get myley
"redis"
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 # 批量设置值
OK
127.0.0.1:6379> mget k1 k2 k3 # 批量获取值
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379> msetnx k1 v1 k4 v3 # 批量设置 存在设置
(integer) 0 发现 是原子性操作 要么一起成功 一起失败
127.0.0.1:6379> get k4
(nil)
set user2 {name:lisi,age:3} # 设置一个user:2 对象 值为json 字符串来保存对象
127.0.0.1:6379> set user2 {name:lisi,age:3}
OK
127.0.0.1:6379> get user2
"{name:lisi,age:3}"
这里的key是一个巧妙的设计: user:{id}:{filed},如此设计在redis中是完全OK
127.0.0.1:6379> mset user:1:name zhangsan user:1:age 2
OK
127.0.0.1:6379> mget user:1:name user:1:age
1) "zhangsan"
2) "2"
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 mongodb # 如果存在值,获取原来的值,并设置新的值
"redis"
127.0.0.1:6379> get db
"mongodb"
value除了是我们的字符串还可以是我们的数字!
计数器
统计多单位的数量
对象缓存存储
粉丝数
可以存储重复的值
在redis里面,我们可以把list玩成,栈、队列、阻塞队列!
所有的list命令都是以 l 开头的
127.0.0.1:6379> lpush list one # 将一个或多个值,插入到列表的头部 (左)
(integer) 1
127.0.0.1:6379> lpush list two
(integer) 2
127.0.0.1:6379> lpush list three
(integer) 3
127.0.0.1:6379> lrange list 0 -1 # 获取list中的值 注意:索引是反过来的
1) "three" 0
2) "two" 1
3) "one" 2
127.0.0.1:6379> lrange list 0 1 # 通过区间获取具体的值
1) "three"
2) "two"
127.0.0.1:6379> RPUSH list right # # 将一个或多个值,插入到列表的 尾部 (右)
(integer) 4
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
4) "right"
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
4) "right"
127.0.0.1:6379> lpop list # 移除list的第一个元素
"three"
127.0.0.1:6379> rpop list # 移除list的最后一个元素
"right"
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
127.0.0.1:6379> lpush list 1
(integer) 1
127.0.0.1:6379> lpush list 2
(integer) 2
127.0.0.1:6379> lpush list 3
(integer) 3
127.0.0.1:6379> lpush list 4
(integer) 4
127.0.0.1:6379> lpush list 5
(integer) 5
127.0.0.1:6379> lpush list 6
(integer) 6
127.0.0.1:6379> lpush list 7
(integer) 7
127.0.0.1:6379> lpush list 8
(integer) 8
127.0.0.1:6379> lpush list 9
(integer) 9
127.0.0.1:6379> lrange list 0 -1
1) "9"
2) "8"
3) "7"
4) "6"
5) "5"
6) "4"
7) "3"
8) "2"
9) "1"
127.0.0.1:6379> lpop list 2 # 移除list的 头 2 个元素
1) "9"
2) "8"
127.0.0.1:6379> lrange list 0 -1
1) "7"
2) "6"
3) "5"
4) "4"
5) "3"
6) "2"
7) "1"
127.0.0.1:6379> rpop list 2 # 移除list的尾部 2 个元素
1) "1"
2) "2"
127.0.0.1:6379> lrange list 0 -1
1) "7"
2) "6"
3) "5"
4) "4"
5) "3"
127.0.0.1:6379> lrange list 0 -1
1) "6"
2) "5"
3) "4"
4) "3"
5) "2"
6) "1"
127.0.0.1:6379> lindex list 0
"6"
127.0.0.1:6379> lindex list 5
"1"
127.0.0.1:6379> lindex list 2
"4"
127.0.0.1:6379> lrange list 0 -1
1) "6"
2) "5"
3) "4"
4) "3"
5) "2"
6) "1"
127.0.0.1:6379> llen list
(integer) 6
127.0.0.1:6379> lrange list 0 -1 # 可以存在相同的值
1) "1"
2) "6"
3) "5"
4) "4"
5) "3"
6) "2"
7) "1"
127.0.0.1:6379> lrem list 1 2 # 移除一个 2 移除list集合中制定个数的value,精确匹配
(integer) 1
127.0.0.1:6379> lrange list 0 -1
1) "1"
2) "6"
3) "5"
4) "4"
5) "3"
6) "1"
127.0.0.1:6379> lrem list 2 1 # 移除两个 1
(integer) 2
127.0.0.1:6379> lrange list 0 -1
1) "6"
2) "5"
3) "4"
4) "3"
127.0.0.1:6379> lrange mylist 0 -1
1) "hello4"
2) "hello3"
3) "hello2"
4) "hello1"
127.0.0.1:6379> ltrim mylist 1 2
OK
127.0.0.1:6379> lrange mylist 0 -1
1) "hello3"
2) "hello2"
127.0.0.1:6379> lrange list 0 -1
1) "hello1"
2) "hello2"
3) "hello3"
127.0.0.1:6379> rpoplpush list mylist # 将list中的字符串,移除要新的列表中
"hello3"
127.0.0.1:6379> lrange list 0 -1 # 查看原来的列表
1) "hello1"
2) "hello2"
127.0.0.1:6379> lrange mylist 0 -1 # 查看目标列表中,确实存在该值
1) "hello3"
127.0.0.1:6379> exists list # 判断这个列表是否存在
(integer) 0
127.0.0.1:6379> lset list 0 item # 如果不存在列表我们去更新就会报错
(error) ERR no such key
127.0.0.1:6379> lpush list value1
(integer) 1
127.0.0.1:6379> lrange list 0 0
1) "value1"
127.0.0.1:6379> lset list 0 item # 如果存在,更新当前下表的值
OK
127.0.0.1:6379> lrange list 0 -1
1) "item"
127.0.0.1:6379> lset list 1 0 ther # 如果不存在,则会报错
(error) ERR wrong number of arguments for 'lset' command
linsert before/after “字符串” 内容
127.0.0.1:6379> lrange list 0 -1
1) "hello"
2) "word"
127.0.0.1:6379> linsert list before word one # 在 word 的前面赠加一个 one
(integer) 3
127.0.0.1:6379> lrange list 0 -1
1) "hello"
2) "one"
3) "word"
127.0.0.1:6379> linsert list after word two # 在 word 的后面赠加一个 two
(integer) 4
127.0.0.1:6379> lrange list 0 -1
1) "hello"
2) "one"
3) "word"
4) "two"
小结:
消息队列 !消息队列(Lpush Rpop),栈 (Lpush Lpop)
set无序不重复 集合
set的命令都是以s开头
smember 网络释义:查询特定值是否存在
127.0.0.1:6379> sadd set hello1 # set 结合中添加 值
(integer) 1
127.0.0.1:6379> sadd set hello2
(integer) 1
127.0.0.1:6379> sadd set hello3
(integer) 1
127.0.0.1:6379> smembers set # 查看指定set的 所有值
1) "hello1"
2) "hello3"
3) "hello2"
127.0.0.1:6379> sismember set hello1 # 判断某一个值是不是在set集合中
(integer) 1 存在 返回 1
127.0.0.1:6379> sismember set word
(integer) 0 不存在 返回 0
127.0.0.1:6379> scard set # 获取 set 集合中的内容元素的个数
(integer) 3
127.0.0.1:6379> sadd set 12306
(integer) 1
127.0.0.1:6379> esembers set
1) "hello1"
2) "hello3"
3) "hello2"
4) "12306"
127.0.0.1:6379> scard set
(integer) 4
127.0.0.1:6379> smembers set
1) "hello1"
2) "hello3"
3) "hello2"
4) "12306"
127.0.0.1:6379> srem set hello3 # 移除set 中指定的元素
(integer) 1
127.0.0.1:6379> scard set
(integer) 3
127.0.0.1:6379> smembers set
1) "hello1"
2) "hello2"
3) "12306"
127.0.0.1:6379> smembers myset
1) "k4"
2) "k3"
3) "k1"
4) "k2"
127.0.0.1:6379> SRANDMEMBER myset # 随机抽取一个元素
"k4"
127.0.0.1:6379> SRANDMEMBER myset
"k2"
127.0.0.1:6379> SRANDMEMBER myset
"k4"
127.0.0.1:6379> SRANDMEMBER myset
"k3"
127.0.0.1:6379> SRANDMEMBER myset 2 # 随机抽取两个元素
1) "k1"
2) "k2"
127.0.0.1:6379> SRANDMEMBER myset 2
1) "k3"
2) "k4"
127.0.0.1:6379> smembers myset
1) "k4"
2) "k3"
3) "k1"
4) "k2"
127.0.0.1:6379> spop myset # 随机删除set 集合中的元素
"k2"
127.0.0.1:6379> spop myset
"k4"
127.0.0.1:6379> smembers myset
1) "k3"
2) "k1"
127.0.0.1:6379> smembers set
1) "hello1"
2) "hello3"
3) "hello2"
127.0.0.1:6379> smembers myset
1) "hello4"
127.0.0.1:6379> smove set myset hello2 # 将指定的值,移动到另外一个set集合
(integer) 1
127.0.0.1:6379> smembers set
1) "hello1"
2) "hello3"
127.0.0.1:6379> smembers myset
1) "hello4"
2) "hello2"
127.0.0.1:6379> smembers k1
1) "v3"
2) "v1"
3) "v2"
127.0.0.1:6379> smembers k2
1) "v2"
2) "v4"
127.0.0.1:6379> SDIFF k1 k2 # 差集
1) "v1"
2) "v3"
127.0.0.1:6379> SINTER k1 k2 # 交集
1) "v2"
127.0.0.1:6379> SUNION k1 k2 # 并集
1) "v1"
2) "v3"
3) "v2"
4) "v4"
微博,将a用户所有关注的人放在一个集合中,将他的粉丝也放在一个集合中!
共同关注,共同爱好
Map集合 key -
本质:和string 没有太大的区别,还是简单的key - value 不过是value 变成了一个 map
Hash的命令都是以 *l* 开头
127.0.0.1:6379> hset myhash k1 v1 # set 一个具体的 key-value
(integer) 1
127.0.0.1:6379> hget myhash k1 # 获取一个字段值
"v1"
127.0.0.1:6379> hmset myhash k1 v2 k2 v3 # set 多个 key-value
OK
127.0.0.1:6379> hmget myhash k1 k2 # 获取多个 字段值
1) "v2"
2) "v3"
127.0.0.1:6379> hgetall myhash # 获取全部的数据
1) "k1"
2) "v2"
3) "k2"
4) "v3"
127.0.0.1:6379> hdel myhash k1
(integer) 1
127.0.0.1:6379> hgetall myhash
1) "k2"
2) "v3"
127.0.0.1:6379> hgetall myhash
1) "k2"
2) "v3"
3) "k1"
4) "v1"
127.0.0.1:6379> hlen myhash
(integer) 2
127.0.0.1:6379> HEXISTS myhash k1
(integer) 1
127.0.0.1:6379> HEXISTS myhash k3
(integer) 0
127.0.0.1:6379> hgetall myhash
1) "k2"
2) "v3"
3) "k1"
4) "v1"
127.0.0.1:6379> hkeys myhash # 只获取所有的flield
1) "k2"
2) "k1"
127.0.0.1:6379> hvals myhash # 只获获取所有的value
1) "v3"
2) "v1"
127.0.0.1:6379> hgetall myhash
1) "k1"
2) "1"
3) "k2"
4) "3"
127.0.0.1:6379> HINCRBY myhash k1 3 # 指定增量
(integer) 4
127.0.0.1:6379> HGETALL myhash
1) "k1"
2) "4"
3) "k2"
4) "3"
127.0.0.1:6379> hsetnx myhash k3 hello # 如果不存在则可以设置
(integer) 1
127.0.0.1:6379> hsetnx myhash k3 hello # 如果存在不能设置
(integer) 0
127.0.0.1:6379> hmset user:2 name zhangsan age 12 student idea
OK
127.0.0.1:6379> hmget user:2 name age student
1) "zhangsan"
2) "12"
3) "idea"
127.0.0.1:6379> hgetall user:2
1) "name"
2) "zhangsan"
3) "age"
4) "12"
5) "student"
6) "idea"
小结:
hash变更数据 user name age ,尤其是用户信息。经常变动的信息!hash更适合对象的存储,String更适合字符串的存储
127.0.0.1:6379> zadd myset 1 one # 添加一个值
(integer) 1
127.0.0.1:6379> zadd myset 2 two 3 three # 添加多个值
(integer) 2
127.0.0.1:6379> zrange myset 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379> zadd salary 2500 xiaoming #添加三个用户
(integer) 1
127.0.0.1:6379> zadd salary 500 zhangfei
(integer) 1
127.0.0.1:6379> zadd salary 3000 lisi
(integer) 1
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf # 显示全部的用户 从小到大
1) "zhangfei"
2) "xiaoming"
3) "lisi"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf withscores # 显示全部的用户 从小到大 并带薪水
1) "zhangfei"
2) "500"
3) "xiaoming"
4) "2500"
5) "lisi"
6) "3000"
127.0.0.1:6379> ZREVRANGE salary 0 -1 # 显示全部的用户 从大到小
1) "lisi"
2) "xiaoming"
3) "zhangfei"
127.0.0.1:6379> ZREVRANGE salary 0 -1 withscores # 显示全部的用户 从大到小 并带薪水
1) "lisi"
2) "3000"
3) "xiaoming"
4) "2500"
5) "zhangfei"
6) "500"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf 2500 withscores 显示工资小于 2500 员工的升序排序
1) "zhangfei"
2) "500"
3) "xiaoming"
4) "2500"
127.0.0.1:6379> zrange salary 0 -1
1) "zhangfei"
2) "xiaoming"
3) "lisi"
127.0.0.1:6379> zcard salary # 获取zset集合中的个数
(integer) 3
127.0.0.1:6379> zrem salary xiaoming # 移除zset集合中的元素
(integer) 1
127.0.0.1:6379> zcard salary
(integer) 2
127.0.0.1:6379> zrange myset 0 -1
1) "hello"
2) "word"
3) "haerbin"
127.0.0.1:6379> zcount myset 1 3 # 获取指定区间的成员数量!
(integer) 3
127.0.0.1:6379> zcount myset 1 2
(integer) 2
127.0.0.1:6379> zcount myset 2 3
(integer) 2
案例思路: set 排序,存储班级成绩表,工资表排序 、排行榜应用