走進redis,探討應用場景
阿新 • • 發佈:2021-12-17
前言: 學習redis之前,我們要首先了解一下什麼是關係型資料庫(mysql),什麼是非關係型資料庫(redis, MongoDB )
nosql,泛指非關係型的資料庫,區別於關係資料庫,它們不保證關係資料的ACID特性
做完以上了解,那麼開啟redis的學習!
我們跳過安裝過程,百度教程有很多
redis基礎知識
redis預設16個數據庫,預設使用第0個數據庫
127.0.0.1:6379> ping #測試連線 PONG 127.0.0.1:6379> select 1 #切換資料庫 OK 127.0.0.1:6379[1]> dbsize #檢視資料庫儲存數 (integer) 0 127.0.0.1:6379[1]> set name xiaoming #設定一個key-value值 OK 127.0.0.1:6379[1]> keys * #檢視所有key 1) "name" 127.0.0.1:6379[1]> get name #檢視指定key的值 "xiaoming" 127.0.0.1:6379[1]> flushdb #清空當前資料庫 OK 127.0.0.1:6379[1]> keys * (empty list or set) 127.0.0.1:6379[1]> flushall #清空所有資料庫 OK 127.0.0.1:6379> clear #相當於linux的清理乾淨當前頁面
127.0.0.1:6379> keys *
1) "age"
2) "name"
127.0.0.1:6379> exists name #是否存在key,存在返回1,不存在返回0
(integer) 1
127.0.0.1:6379> expire age 20 #設定key過期時間 (integer) 1 127.0.0.1:6379> ttl age (integer) 16 127.0.0.1:6379> ttl age (integer) 14 127.0.0.1:6379> ttl age (integer) 11 127.0.0.1:6379> ttl age (integer) 7 127.0.0.1:6379> ttl age #key過期 (integer) -2 127.0.0.1:6379> get age (nil)
127.0.0.1:6379> set name xiaoming
OK
127.0.0.1:6379> type name #檢視資料型別
string
redis五大基礎型別和使用
String
應用場景:後臺大多資料的字串儲存,也可以儲存數字
127.0.0.1:6379> set k1 v1 OK 127.0.0.1:6379> get k1 "v1" 127.0.0.1:6379> append k1 hello #追加字串 (integer) 7 127.0.0.1:6379> get k1 "v1hello" 127.0.0.1:6379> strlen k1 #獲取字串長度 (integer) 7 127.0.0.1:6379> append k2 hello #如果追加不存在此key,會新建一個key (integer) 5 127.0.0.1:6379> keys * 1) "k2" 2) "k1"
應用場景:文章瀏覽量,點贊數,做計數器
127.0.0.1:6379> set k1 0
OK
127.0.0.1:6379> get k1
"0"
127.0.0.1:6379> incr k1 #數值+1,只能是數值可以,有字元會報錯
(integer) 1
127.0.0.1:6379> incr k1
(integer) 2
127.0.0.1:6379> get k1
"2"
127.0.0.1:6379> decr k1 #數值-1,只能是數值可以,有字元會報錯
(integer) 1
127.0.0.1:6379> get k1
"1"
127.0.0.1:6379> incrby k1 10 #設定增長步長
(integer) 11
127.0.0.1:6379> get k1
"11"
127.0.0.1:6379> decrby k1 5 #設定減少步長
(integer) 6
127.0.0.1:6379> get k1
"6"
127.0.0.1:6379> set k1 hello,xiaoming
OK
127.0.0.1:6379> get k1
"hello,xiaoming"
127.0.0.1:6379> getrange k1 0 5 #擷取0到5字元
"hello,"
127.0.0.1:6379> getrange k1 0 -1 #獲取全部
"hello,xiaoming"
127.0.0.1:6379> getrange k1 0 -1
"hello,xiaoming"
127.0.0.1:6379> setrange k1 1 good #替換指定字串
(integer) 14
127.0.0.1:6379> get k1
"hgood,xiaoming"
127.0.0.1:6379> setex k1 20 hello #設定key過期時間
OK
127.0.0.1:6379> ttl k1
(integer) 14
127.0.0.1:6379> get k1
"hello"
127.0.0.1:6379> ttl k1
(integer) -2
127.0.0.1:6379> get k1
(nil)
127.0.0.1:6379> setnx k2 redis #如果k2不存在設定成功,存在則失敗,樂觀鎖中用
(integer) 1
127.0.0.1:6379> setnx k2 nginx #設定失敗
(integer) 0
127.0.0.1:6379> get k2
"redis"
127.0.0.1:6379> mset k1 v1 k2 v2 #同時設定多個值
OK
127.0.0.1:6379> mget k1 k2 #同時獲取多個值
1) "v1"
2) "v2"
127.0.0.1:6379> msetnx k1 v1 k3 v3 #保證原子性,一起成功一起失敗
(integer) 0
#將物件儲存在redis的string型別中
set user:1:{name:xiaoming,age:16}
#用法user:{id}:{filed}
127.0.0.1:6379> mset user:1:name xiaoming user:1:age 16
OK
127.0.0.1:6379> mget user:1:name user:1:age
1) "xiaoming"
2) "16"
127.0.0.1:6379> getset k1 v1 #先獲取在設定值,沒有返回空
(nil)
127.0.0.1:6379> getset k1 v2
"v1"
127.0.0.1:6379> get k1
"v2"
List
List可以做棧,佇列,左右都可以進,都可以出,具體如下
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 #檢視列表全部內容
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> rpush list four #將值放在尾部(右)
(integer) 4
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
4) "four"
127.0.0.1:6379> keys *
1) "list"
127.0.0.1:6379> lpop list #移除頭部第一個
"three"
127.0.0.1:6379> rpop list #移除尾部第一個
"four"
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
127.0.0.1:6379> lindex list 1 #通過索引獲取一個值
"one"
127.0.0.1:6379> lindex list 0 #通過索引獲取一個值
"two"
127.0.0.1:6379> rpush list one
(integer) 1
127.0.0.1:6379> rpush list two
(integer) 2
127.0.0.1:6379> rpush list three
(integer) 3
127.0.0.1:6379> rpush list three
(integer) 4
127.0.0.1:6379> lrange list 0 -1
1) "one"
2) "two"
3) "three"
4) "three"
127.0.0.1:6379> lrem list 2 three #移除指定個數的value
(integer) 2
127.0.0.1:6379> lrange list 0 -1
1) "one"
2) "two"
127.0.0.1:6379> ltrim list 1 1 #擷取指定的長度
OK
127.0.0.1:6379> lrange list 0 -1
1) "two"
127.0.0.1:6379> lpush list v1
(integer) 1
127.0.0.1:6379> lpush list v2
(integer) 2
127.0.0.1:6379> lpush list v3
(integer) 3
127.0.0.1:6379> rpoplpush list newlist #移除原來列表最後一個元素到新列表
"v1"
127.0.0.1:6379> lrange newlist 0 -1
1) "v1"
127.0.0.1:6379> exists list
(integer) 0
127.0.0.1:6379> lset list 0 item #list不存在,不可以更新值
(error) ERR no such key
127.0.0.1:6379> lpush list v1
(integer) 1
127.0.0.1:6379> lrange list 0 -1
1) "v1"
127.0.0.1:6379> lset list 0 item #list存在,可以更新新值
OK
127.0.0.1:6379> lrange list 0 -1
1) "item"
127.0.0.1:6379> linsert list before item world #往列表某個元素後插入一個值
(integer) 2
127.0.0.1:6379> lrange list 0 -1
1) "world"
2) "item"
127.0.0.1:6379> linsert list after item hello #往列表某個元素前插入一個值
(integer) 3
127.0.0.1:6379> lrange list 0 -1
1) "world"
2) "item"
3) "hello"
Set
set 的值是不可以重複,無序儲存
127.0.0.1:6379> sadd myset hello #往set集合新增元素
(integer) 1
127.0.0.1:6379> sadd myset hello,world
(integer) 1
127.0.0.1:6379> sadd myset welcome,world
(integer) 1
127.0.0.1:6379> smembers myset #檢視set集合全部元素
1) "hello,world"
2) "welcome,world"
3) "hello"
127.0.0.1:6379> sismember myset hello #檢視元素是否存在,1存在,0不存在
(integer) 1
127.0.0.1:6379> sismember myset world
(integer) 0
127.0.0.1:6379> scard myset #統計元素個數
(integer) 3
127.0.0.1:6379> srem myset hello #移除一個指定元素
(integer) 1
127.0.0.1:6379> scard myset
(integer) 2
127.0.0.1:6379> smembers myset
1) "hello,world"
2) "welcome,world"
127.0.0.1:6379> smembers myset
1) "good1"
2) "good"
3) "welcome,world"
4) "good2"
5) "hello,world"
6) "good3"
1
127.0.0.1:6379> srandmember myset #隨機抽取一個元素
"good1"
127.0.0.1:6379> srandmember myset
"good3"
127.0.0.1:6379> srandmember myset 2 #隨機抽取2個元素
1) "good"
2) "welcome,world"
127.0.0.1:6379> spop myset #隨機移除一個元素
"good2"
127.0.0.1:6379> spop myset
"hello,world"
127.0.0.1:6379> smembers myset
1) "welcome,world"
2) "good"
3) "good1"
4) "good3"
127.0.0.1:6379> smembers myset
1) "welcome,world"
2) "good"
3) "good1"
4) "good3"
127.0.0.1:6379> smove myset myset2 good #移動一個指定元素到新set集合
(integer) 1
127.0.0.1:6379> smembers myset2
1) "good"
127.0.0.1:6379> smembers k1
1) "c"
2) "b"
3) "e"
4) "a"
127.0.0.1:6379> smembers k2
1) "d"
2) "c"
3) "e"
4) "f"
127.0.0.1:6379> sdiff k1 h2 #差集
1) "b"
2) "c"
3) "e"
4) "a"
127.0.0.1:6379> sinter k1 k2 #交集 ,共同好友
1) "c"
2) "e"
127.0.0.1:6379> sunion k1 k2 #並集
1) "e"
2) "a"
3) "c"
4) "b"
5) "d"
6) "f"
Hash
相當於map集合,key-map 時候這個值是一個map集合!
應用場景:使用者資訊儲存,經常變動的資訊,hash更適合物件的儲存
127.0.0.1:6379> hset myhash k1 v1 #set一個key-value
(integer) 1
127.0.0.1:6379> hget myhash k1 #獲取一個key-value
"v1"
127.0.0.1:6379> hmset myhash k1 v1 k2 v2 #set多個key-value
OK
127.0.0.1:6379> hmget myhash k1 k2 #獲取多個key-value
1) "v1"
2) "v2"
127.0.0.1:6379> hgetall myhash #獲取全部
1) "k1"
2) "v1"
3) "k2"
4) "v2"
127.0.0.1:6379> hdel myhash k1 #移除一個key-value
(integer) 1
127.0.0.1:6379> hgetall myhash
1) "k2"
2) "v2"
127.0.0.1:6379> hlen myhash #獲取hash數量
(integer) 1
127.0.0.1:6379> hexists myhash k1 #判斷hash指定欄位是否存在
(integer) 1
127.0.0.1:6379> hexists myhash k1
(integer) 0
127.0.0.1:6379> hkeys myhash #只獲取key
1) "k2"
127.0.0.1:6379> hvals myhash #只獲取value
1) "v2"
127.0.0.1:6379> hset myhash field1 5
(integer) 1
127.0.0.1:6379> hincrby myhash field1 1 #增1
(integer) 6
127.0.0.1:6379> hincrby myhash field1 -1 #減1
(integer) 5
127.0.0.1:6379> hsetnx myhash field2 world #判斷元素是否存在,1存在
(integer) 1
127.0.0.1:6379> hsetnx myhash field2 worlds #0不存在
(integer) 0
127.0.0.1:6379> hset user:1 name zhangsan #儲存一個物件
(integer) 1
127.0.0.1:6379> hget user:1 name #獲取一個物件值
"zhangsan"
Zset
Zset有序儲存
應用場景:排行榜,閱讀榜
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 zhangsan
(integer) 1
127.0.0.1:6379> zadd salary 5000 lisi
(integer) 1
127.0.0.1:6379> zadd salary 1000 zhaowu
(integer) 1
127.0.0.1:6379> zrem salary zhangsan #移除一個元素
(integer) 1
127.0.0.1:6379> zrangebyscore salary -inf +inf #升序排序
1) "zhaowu"
2) "zhangsan"
3) "lisi"
127.0.0.1:6379> zrangebyscore salary -inf +inf withscores #升序排序,帶成績
1) "zhaowu"
2) "1000"
3) "zhangsan"
4) "2500"
5) "lisi"
6) "5000"
127.0.0.1:6379> zrangebyscore salary -inf 2500 withscores #升序排序,帶成績,有範圍
1) "zhaowu"
2) "1000"
3) "zhangsan"
4) "2500"
127.0.0.1:6379> zrevrangebyscore salary +inf -inf withscores #降序排序,帶成績
1) "lisi"
2) "5000"
3) "zhangsan"
4) "2500"
5) "zhaowu"
6) "1000"
127.0.0.1:6379> zrange salary 0 -1 #從小到大排序
1) "zhaowu"
2) "zhangsan"
3) "lisi"
127.0.0.1:6379> zrevrange salary 0 -1 #從大到小排序
1) "lisi"
2) "zhangsan"
3) "zhaowu"
127.0.0.1:6379> zcard salary #獲取集合個數
(integer) 3
127.0.0.1:6379> zcount myset 1 3 #獲取指定區間成員個數
(integer) 3
三種特殊型別
geospatial
應用場景: 搜尋附近人,計算兩人距離
使用方式
1.先新增地理座標和城市名(人名)
2.通過方法獲取兩地距離
3.以r為半徑掃描獲取附近的人
Hyperloglog
應用場景 :基數統計
只要12kb記憶體,有0.81%誤差
127.0.0.1:6379> pfadd mykey a b c d e f d e f g h #新增元素
(integer) 1
127.0.0.1:6379> pfadd mykey2 e f g h i j k l
(integer) 1
127.0.0.1:6379> pfcount mykey2 #統計個數
(integer) 8
127.0.0.1:6379> pfmerge mykey3 mykey mykey2 #統計兩者差值
OK
127.0.0.1:6379> pfcount mykey3
(integer) 12
Bitmap
二位數儲存 0 1
應用場景:統計用0 ,1兩種狀態的情景 365打卡,1打卡,0沒有打卡
127.0.0.1:6379> setbit sign 0 0 #設定有無打卡
(integer) 0
127.0.0.1:6379> setbit sign 1 0
(integer) 0
127.0.0.1:6379> setbit sign 2 1
(integer) 0
127.0.0.1:6379> setbit sign 3 1
(integer) 0
127.0.0.1:6379> setbit sign 4 0
(integer) 0
127.0.0.1:6379> setbit sign 5 0
(integer) 0
127.0.0.1:6379> setbit sign 6 1
(integer) 0
127.0.0.1:6379> getbit sign 4 #檢視某天有無打卡
(integer) 0
127.0.0.1:6379> getbit sign 6
(integer) 1
127.0.0.1:6379> bitcount sign #統計總共打卡記錄
(integer) 3