Resis 基本命令
127.0.0.1:6379[1]> set anme wfks
OK
127.0.0.1:6379[1]> get name
"wfk"
127.0.0.1:6379[1]> keys * #查看所有key
1) "anme"
2) "name"
127.0.0.1:6379[1]> get name
"wfk"
127.0.0.1:6379[1]> exists name #是否存在key
(integer) 1
127.0.0.1:6379[1]> exists namess
(integer) 0
127.0.0.1:6379[1]> set name zzz
OK
127.0.0.1:6379[1]> get name
"zzz"
127.0.0.1:6379[1]> expire name 10 #设置过期时间
(integer) 1
127.0.0.1:6379[1]> ttl name #查询过期剩余时间
(integer) 3
127.0.0.1:6379[1]> ttl name
(integer) 3
127.0.0.1:6379[1]> ttl name
(integer) 1
127.0.0.1:6379[1]> ttl name
(integer) -2
127.0.0.1:6379[1]>
五大数据类型:
- String
- List
- Set
- Hash
- Zset
String(字符串)
127.0.0.1:6379[1]> appeng name hello
(error) ERR unknown command 'appeng'
127.0.0.1:6379[1]> append name "str"
(integer) 6
127.0.0.1:6379[1]> get name
"wfkstr"
127.0.0.1:6379[1]> append name ",Test" #追加字符串
(integer) 11
127.0.0.1:6379[1]> get name
"wfkstr,Test"
127.0.0.1:6379[1]>
127.0.0.1:6379[1]> set views 0
OK
127.0.0.1:6379[1]> incr views #增加浏览量(1)
(integer) 1
127.0.0.1:6379[1]> get views
"1"
127.0.0.1:6379[1]> incr views
(integer) 2
127.0.0.1:6379[1]> get views
"2"
127.0.0.1:6379[1]> type views#获取类型
string
127.0.0.1:6379[1]> decr views
(integer) 1
127.0.0.1:6379[1]> get views
"1"
127.0.0.1:6379[1]> incrby views 10 #增长指定步长
(integer) 11
127.0.0.1:6379[1]> get views
"11"
127.0.0.1:6379[1]> decrby views 5#减少指定步长
(integer) 6
127.0.0.1:6379[1]> get views
"6"
127.0.0.1:6379[1]>
#################################################
127.0.0.1:6379[1]> get name
"wfkstr,Test"
127.0.0.1:6379[1]> getrange name 0 3 #截取字符串
"wfks"
127.0.0.1:6379[1]> getrange name 0 -1#截取全部
"wfkstr,Test"
127.0.0.1:6379[1]>
127.0.0.1:6379[1]> set key2 abcdefg
OK
127.0.0.1:6379[1]> get key2
"abcdefg"
127.0.0.1:6379[1]> setrange key2 1 xx #替换指定位置的字符串
(integer) 7
127.0.0.1:6379[1]> get key2
"axxdefg"
127.0.0.1:6379[1]>
#################################################
127.0.0.1:6379[1]> setex key3 30 hello #设置key3 值为hello
OK,30秒后过期
127.0.0.1:6379[1]> ttl key3
(integer) 16
127.0.0.1:6379[1]> setnx mykey redis
(integer) 1
127.0.0.1:6379[1]> keys *
1) "mykey"
2) "age"
3) "key2"
4) "name"
5) "views"
127.0.0.1:6379[1]> ttl key3
(integer) -2
127.0.0.1:6379[1]> setnx mykeyt MongoDB #如果mykeyt 不存在那么去创建mykeyt
(integer) 1
127.0.0.1:6379[1]> setnx mykeyt MongoDs#如果mykeyt 存在那么创建mykeyt失败
(integer) 0
127.0.0.1:6379[1]> get mykeyt
"MongoDB"
127.0.0.1:6379[1]>
#################################################
127.0.0.1:6379[1]> mset k1 v1 k2 v2 k3 v3 #批量设置键值
OK
127.0.0.1:6379[1]> keys *
1) "k2"
2) "k1"
3) "k3"
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]>
#################################################
set user:1{name:wfk,age:3}
127.0.0.1:6379[1]> mset user:1:name wfk user:1:age 2
OK
127.0.0.1:6379[1]> keys *
1) "user:1:age"
2) "user:1:name"
127.0.0.1:6379[1]> mget user:1:name user:1:age
1) "wfk"
2) "2"
127.0.0.1:6379[1]>
#################################################
getset #先get再set
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 mongodb #如果存在值,获取原来的值,并存入新的值
"redis"
127.0.0.1:6379[1]> get db
"mongodb"
127.0.0.1:6379[1]>
List(基本数据类型,列表)
所有的 list 命令都是“l”开头的。在 reidis 里面,我们可以把 list 玩成,栈、队列、阻塞队列!
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 #获取list里面的值
1) "three"
2) "two"
3) "one"
127.0.0.1:6379[1]> lrange list 0 1 #获取list区间里面的值
1) "three"
2) "two"
127.0.0.1:6379[1]> rpush list rrrr #将一个值或者多个值,插入到列表尾部
(integer) 4
127.0.0.1:6379[1]> lrange list 0 -1
1) "three"
2) "two"
3) "one"
4) "rrrr"
127.0.0.1:6379[1]> lpop list #移除list的第一个元素
"three"
127.0.0.1:6379[1]> lrange list 0 -1
1) "two"
2) "one"
3) "rrrr"
127.0.0.1:6379[1]> lpop list #移除list的最后一个元素
"two"
#################################################
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]> llen list #返回列表的长度
(integer) 3
127.0.0.1:6379[1]>
#################################################
移除指定的值
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]> 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 three #移除一个相关的量
(integer) 1
127.0.0.1:6379[1]> lrem list 1 three
(integer) 1
127.0.0.1:6379[1]> lrange list 0 -1
1) "two"
2) "one"
127.0.0.1:6379[1]>
#################################################
trim 修剪
127.0.0.1:6379> rpush mylist hello
(integer) 1
127.0.0.1:6379> rpush mylist hello1
(integer) 2
127.0.0.1:6379> rpush mylist hello2
(integer) 3
127.0.0.1:6379> rpush mylist hello3
(integer) 4
127.0.0.1:6379> ltrim mylist 1 2 #将其修剪为区间值
OK
127.0.0.1:6379> lrange mylist 0 -1
1) "hello1"
2) "hello2"
127.0.0.1:6379> rpoplpush mylist myotherlist #移除列表的最后一个元素,将他移动到新的列表中
"hello2"
127.0.0.1:6379> lrange mylist 0 -1 #查看原来的列表
1) "hello1"
127.0.0.1:6379> lrange myotherlist 0 -1 #查看新的列表
1) "hello2"
127.0.0.1:6379>
#################################################
lset 将列表中的指定下标的值替换为另一个值,更新操作
127.0.0.1:6379> lpush list value1
(integer) 1
127.0.0.1:6379> lset list 0 item
OK
127.0.0.1:6379> lrange list 0 0
1) "item"
127.0.0.1:6379>
#################################################
linsert 将某个具体的value插入到你的列表中的某个元素的前面或者后面!
127.0.0.1:6379> rpush mylist hello
(integer) 1
127.0.0.1:6379> rpush mylist world
(integer) 2
127.0.0.1:6379> linsert mylist before world othre
(integer) 3
127.0.0.1:6379> lrange mylist 0 -1
1) "hello"
2) "othre"
3) "world"
127.0.0.1:6379>
小结
- 它实际是一个链表,before Node after ,left,right 都可以插入值
- 如果 key 不存在,创建一个新的链表
- 如果 key 存在,新增加内容
- 如果移除了所有值,空链表也代表不存在!
- 在两边插入或者改动值,效率最高!中间元素相对来说效率会低一点~
消息排队!消息队列 Lpush Rpop Lpush Lpop
Set(集合)
set 中的值是不能重读的
127.0.0.1:6379> sadd myset hello # set集合添加元素
(integer) 1
127.0.0.1:6379> sadd myset wfk
(integer) 1
127.0.0.1:6379> sadd myset dsds
(integer) 1
127.0.0.1:6379> smembers myset # 查看指定set的所有值
1) "hello"
2) "dsds"
3) "wfk"
127.0.0.1:6379> sismember myset hello #判断某一个值是不是在set集合中
(integer) 1
127.0.0.1:6379> sismember myset world
(integer) 0
127.0.0.1:6379> scard myset # 获取set集合元素个数
(integer) 3
127.0.0.1:6379>
#################################################
rem
127.0.0.1:6379> srem myset hello # 移除set集合中指定的元素
(integer) 1
127.0.0.1:6379> scard myset
(integer) 2
127.0.0.1:6379> smembers myset
1) "dsds"
2) "wfk"
127.0.0.1:6379>
#################################################
set 无序不重复集合,随机抽取
127.0.0.1:6379> srandmember myset #随机抽选出一个元素
"dasa"
127.0.0.1:6379> srandmember myset
"dasasadsa"
127.0.0.1:6379> srandmember myset
"wfk"
127.0.0.1:6379> srandmember myset
"dasa"
127.0.0.1:6379> srandmember myset
"dasasadsadasdsaasdasd"
127.0.0.1:6379>
#################################################
127.0.0.1:6379> smembers myset
1) "dasasadsadasdsaasdasd"
2) "dasasadsadasdsa"
3) "dasa"
4) "wfk"
5) "dasasadsa"
6) "dasasadsadasdsaasdasddasdas"
7) "dsds"
127.0.0.1:6379> spop myset #随机移除元素
"dasasadsadasdsa"
127.0.0.1:6379> smembers myset
1) "dasasadsadasdsaasdasd"
2) "dasa"
3) "wfk"
4) "dasasadsa"
5) "dasasadsadasdsaasdasddasdas"
6) "dsds"
127.0.0.1:6379> spop myset
"dasa"
127.0.0.1:6379> smembers myset
1) "dasasadsadasdsaasdasd"
2) "wfk"
3) "dasasadsa"
4) "dasasadsadasdsaasdasddasdas"
5) "dsds"
127.0.0.1:6379>
#################################################
将一个指定元素移动到另外一个set集合
127.0.0.1:6379> smembers myset
1) "dasasadsa"
2) "dasasadsadasdsaasdasddasdas"
3) "dsds"
127.0.0.1:6379> smove myset myset2 dsds #将一个指定的值,移动到另外一个set集合
(integer) 1
127.0.0.1:6379> smembers myset
1) "dasasadsa"
2) "dasasadsadasdsaasdasddasdas"
127.0.0.1:6379> smembers myset2
1) "dsds"
127.0.0.1:6379>
#################################################
微博,B站,共同关注!(并集)
数字集合类:
—— 差集
—— 交集
—— 并集
127.0.0.1:6379> sadd key1 a
(integer) 1
127.0.0.1:6379> sadd key1 b
(integer) 1
127.0.0.1:6379> sadd key1 c
(integer) 1
127.0.0.1:6379> sadd key2 c
(integer) 1
127.0.0.1:6379> sadd key2 d
(integer) 1
127.0.0.1:6379> sadd key2 e
(integer) 1
127.0.0.1:6379> sdiff key1 key2 #差集
1) "a"
2) "b"
127.0.0.1:6379> sinter key1 key2 #交集
1) "c"
127.0.0.1:6379> sunion key1 key2 #并集
1) "a"
2) "b"
3) "c"
4) "e"
5) "d"
127.0.0.1:6379>
微博,A 用户将所有关注的人放在一个 set 集合中!将他的粉丝也放到一个集合中!
共同关注,共同爱好,二度好友(六度分隔理论)
Hash(哈希)
Map 集合,key-map!这时候这个值是一个 map 集合,本质和 String 类型没有什么区别
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> hset myhash key1 value1 #储存键值对
(integer) 1
127.0.0.1:6379> hset myhash key2 value2
(integer) 1
127.0.0.1:6379> hget myhash key1
"value1"
127.0.0.1:6379> hmset myhash key1 TTT key2 VVV #批量储存
OK
127.0.0.1:6379> hmget myhash key1 key2 #批量取出
1) "TTT"
2) "VVV"
127.0.0.1:6379> hgetall myhash #获取所有的键值对
1) "key1"
2) "TTT"
3) "key2"
4) "VVV"
127.0.0.1:6379>
127.0.0.1:6379> hgetall myhash
1) "key1"
2) "TTT"
3) "key2"
4) "VVV"
127.0.0.1:6379> hdel myhash key1 #删除指定的key
(integer) 1
127.0.0.1:6379> hgetall myhash
1) "key2"
2) "VVV"
127.0.0.1:6379>
#################################################
hlen
127.0.0.1:6379> hgetall myhash
1) "key2"
2) "VVV"
127.0.0.1:6379> hlen myhash #获取hash表的字段数量
(integer) 1
127.0.0.1:6379>
#################################################
127.0.0.1:6379> hexists myhash key2 #判断hash中指定的字段是否存在!
(integer) 1
127.0.0.1:6379>
#################################################
# 只获取所有的 field
# 只获取所有的value
127.0.0.1:6379> hkeys myhash # 只获取所有的 field
1) "key2"
127.0.0.1:6379> hvals myhash # 只获取所有的value
1) "VVV"
127.0.0.1:6379>
#################################################
127.0.0.1:6379> hset myhash key3 5
(integer) 1
127.0.0.1:6379> hincrby myhash key3 1 #指定增量
(integer) 6
127.0.0.1:6379> hincrby myhash key3 -1
(integer) 5
127.0.0.1:6379> hsetnx myhash key4 hello
(integer) 1
127.0.0.1:6379>
#################################################
hash 变更的数据 user name age ,尤其是用户信息之类的,经常变动的信息!hash 更适于对象储存,String 更加适合字符串储存!
Zset(有序集合)
在 set 的基础上,增加了一个值,set k1 v1 score1 v1
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>
#################################################
127.0.0.1:6379> zadd salary 2500 xiaohong
(integer) 1
127.0.0.1:6379> zadd salary 5000 zhangshan
(integer) 1
127.0.0.1:6379> zadd salary 500 wfk
(integer) 1
127.0.0.1:6379> zrangebyscore salary -inf +inf #排序
1) "wfk"
2) "xiaohong"
3) "zhangshan"
127.0.0.1:6379>
127.0.0.1:6379> zrangebyscore salary -inf +inf withscores #返回数据带有权值
1) "wfk"
2) "500"
3) "xiaohong"
4) "2500"
5) "zhangshan"
6) "5000"
127.0.0.1:6379>
127.0.0.1:6379> zrangebyscore salary -inf 2500 withscores #获取指定范围的数据
1) "wfk"
2) "500"
3) "xiaohong"
4) "2500"
127.0.0.1:6379>
#################################################
移除rem中的元素
127.0.0.1:6379> zrange salary 0 -1
1) "wfk"
2) "xiaohong"
3) "zhangshan"
127.0.0.1:6379> zrem salary xiaohong #移除相应的元素
(integer) 1
127.0.0.1:6379> zrange salary 0 -1
1) "wfk"
2) "zhangshan"
127.0.0.1:6379>
#################################################
127.0.0.1:6379> zcard salary #查询数量
(integer) 2
127.0.0.1:6379>
#################################################
127.0.0.1:6379[1]> zrevrange salary 0 -1 #从大到小排序
1) "zhangshan"
2) "xiaohong"
3) "wfk"
127.0.0.1:6379[1]>
127.0.0.1:6379[1]> zcount myset 1 2 #获取指定区间数据的数量
(integer) 1
127.0.0.1:6379[1]>
案例思路:set 排序 储存班级成绩表,工资表排序
普通消息,重要消息,带权重进行判断
排行榜应用的实现
三种特殊数据类型
- geospatial(地理位置)
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于