首頁 > 資料庫 > Redis > 主體

一文搞定Redis五大資料類型及應用場景

咔咔
發布: 2020-08-28 17:26:18
原創
2051 人瀏覽過

本文學習知識點 redis五大資料類型資料型態:string、hash、list、set、sorted_set 五大類型各自的應用場景

#前言

############################################################# ❝######喀喀整理了一個路線圖,打造一份面試寶典,準備按照這樣的路線圖進行編寫文章,後期發現沒有補充到的知識點在進行添加。也期待各位夥伴一起來幫忙補充一下。評論區見! ###
一文搞定Redis五大資料類型及應用場景
在這裡插入圖片描述

1. string類型

1-1 string類型資料的基本操作

##新增/ 修改資料:

set key value

取得資料:

get key

刪除資料:

del key

新增/ 修改多個資料:

mset key value key1 value1

取得多個資料:

mget key key1#

追加資訊到原始資料後邊(不存在時則新增):append key value

1-2 string型別增減運算

設定數值增加指定範圍的值:incr key 預設每次加1 | incrby key 值 每次新增value設定資料減少指定範圍: decr key | decrby key value 跟新增是一回事

#「應用程式場景」

控制資料庫表主鍵id,為資料庫表提供主鍵產生策略,確保資料表主鍵的一致性。

1-3 string類型時效運算

設定過期時間:setex key seconds value

「應用場景」

實作限制時間投票功能:例如一個微信一個小時可以投一次 實現熱點資訊:例如電商產業熱門商品、新聞網站熱門新聞

1-4 string類型的應用場景

#微博大V主頁高頻的訪問,對於粉絲數、關注數、微博數都需要時時更新。這個就屬於高頻訊息了,我們就可以使用redis的string類型來解決一文搞定Redis五大資料類型及應用場景在redis中為大V設定使用者訊息,以使用者主鍵和屬性為鍵值,以下為實現案例。 一文搞定Redis五大資料類型及應用場景在這裡需要簡單的說一下key的命名規則:以表名 主鍵 主鍵值 欄位 :欄位值。以這樣的規則來命名就可以很好的來管理我們的鍵值。

我們還可以使用另一種方​​式來實現,就是鍵後邊直接跟一個結構,例如一文搞定Redis五大資料類型及應用場景以上的倆種方式都是可以實現的,只是第一種可以很方便的對任意一個值進行管理,第二種是改一個都得改一次,看業務場景,定時刷新就行。

2. hash型別

#2-1 hash型別資料的基本操作

新增/ 修改資料:hset key field value

#

取得資料:hget key field    |  hgetall key

刪除資料:hdel key field field1

新增/ 修改多個資料:hmset key field value field1 value1

#取得多個資料:hmget key field field1

# 取得表中欄位數量:hlen key

取得表格中是否存在某個欄位:hexists key field

2- 2 hash類型資料的擴充操作

取得hash表中所有的欄位值:hkeys key

取得hash表中所有的欄位值:hvals key

設定指定欄位的數值增加指定範圍的值:hincrby key field increment   |  hincrbyfloat key field increment

#2-3 hash業務場景之購物車

此圖來源於網路非自製,只是模擬購物車場景一文搞定Redis五大資料類型及應用場景在上圖中,我們可以看到購物車裡的信息,下來咱們使用redis來對這個購物車的實現。

這裡實作了一個新增購物車和取得購物車,keys的命名為 表名主鍵主鍵值一文搞定Redis五大資料類型及應用場景在上圖中,我們會有一個問題就是商品資訊儲存會大量重複,所有我們也需要將商品單獨給一直hash。如下圖,只儲存商品id一文搞定Redis五大資料類型及應用場景這裡提供了倆種設定方式,一種是設定多個字段,一種是直接儲存為json。資訊不常變動的話可以使用json一文搞定Redis五大資料類型及應用場景給大家在提供一個方法hsetnx key field value,如果有則不進行添加,沒有則添加。這個功能就使用在不同的使用者新增相同的商品時不會執行覆寫和無用操作一文搞定Redis五大資料類型及應用場景

3. list類型

#資料儲存需求:儲存多個數據,並對資料進行儲存空間的順序進行區分 所需的數據結構:一個儲存空間保存多個數據,透過數據可以體現進入順序 list類型:保存多個數據,底層使用雙向鍊錶儲存結構實作

#3-1 list類型資料的基本操作

新增/ 修改資料:lpush key value value1   |  rpush key value value1

## 取得資料:

lrange key start end   | lindex key index  | llen key

#刪除資料:

rpop key## | lpop key##<figure data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;"><img src="https://img-blog.csdnimg.cn/20200525112247102.png" alt="一文搞定Redis五大資料類型及應用場景" style="max-width:90%"><figcaption style="margin-top: 5px; text-align: center; font-size: 13px;"><span style="background-image: url(https://img.alicdn.com/tfs/TB1Yycwyrj1gK0jSZFuXXcrHpXa-32-32.png); display: inline-block; width: 18px; height: 18px; background-size: 18px; background-repeat: no-repeat; background-position: center; margin-right: 5px; margin-bottom: -5px;"></span>在這裡插入圖片描述</figcaption></figure><h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 22px; border-bottom: 4px solid rgb(64, 184, 250);"> <span class="prefix" style="display: flex; width: 20px; height: 20px; background-size: 20px 20px; background-image: url(https://my-wechat.mdnice.com/fullstack-1.png); margin-bottom: -22px;"></span><span class="content" style="display: flex; color: #40B8FA; font-size: 20px; margin-left: 25px;">#3-2 list類型資料的擴充功能</span> <span class="suffix" style="display: flex; box-sizing: border-box; width: 200px; height: 10px; border-top-left-radius: 20px; background: RGBA(64, 184, 250, .5); color: rgb(255, 255, 255); font-size: 16px; letter-spacing: 0.544px; justify-content: flex-end; float: right; margin-top: -10px; box-sizing: border-box !important; overflow-wrap: break-word !important;"></span> </h2> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;">在規定時間內取得並移除資料:<code style="overflow-wrap: break-word; margin: 0px 2px; font-family: " operator mono consolas monaco menlo monospace word-break: break-all color: rgb background: rgba padding: border-radius: height: line-height:>blpop key1 key2 timeout | brpop key1 key2 timeout

##這個功能簡單的寫一個案例,容易理解

左邊這個終端指令執行後會等待30秒的時間返回刪除的資料

當右邊的新增指令執行後左邊會直接返回返回刪除的資料

一文搞定Redis五大資料類型及應用場景

3-3 list業務場景

在上邊我們知道了list的基礎作業 執行 lpop key 或rpop key可以從做或從右刪除,但現在有個場景是朋友圈點讚業務,然後從中間取消資料。案例如下圖

我們先往list5裡邊加入  a b c d 然後移除c 在查看就剩下a b d了

一文搞定Redis五大資料類型及應用場景

4. set類型

#新的儲存需求:儲存大量的數據,在查詢方便提供更高的效率 所需的儲存結構:能夠保存大量的數據,高效的內部儲存機制,以便於查詢 set類型:與hash儲存結構完全相同,僅儲存鍵,不儲存值(nil),且值是不允許重複的

一文搞定Redis五大資料類型及應用場景
##在這裡插入圖片描述

4-1 set類型資料的基本操作##新增/ 修改資料:

sadd key member member1

取得資料:

smembers key

刪除資料:

srem key member1

#取得集合資料總量:

scard key

<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;">判斷集合中是否包含指定資料:<code style="overflow-wrap: break-word; margin: 0px 2px; font-family: " operator mono consolas monaco menlo monospace word-break: break-all color: rgb background: rgba padding: border-radius: height: line-height:>sismember key member

一文搞定Redis五大資料類型及應用場景
#在這裡插入圖片描述

4-2 set類型資料擴充運算

#隨機取得集合中指定數量的資料:srandmember key count

隨機取得集合中某個資料並將改變資料集移除集合:spop key

4-3 set類型業務場景推薦資訊

隨機推送熱點資訊、熱點新聞、熱賣旅遊、應用app推薦、關注推薦等

#由於最近咔咔在寫discuz,這個案例就以實現關注推薦。

案例一:依照一定的推薦機制在set裡邊存放對應的用戶,然後每次進行隨機取得2位需要推薦的用戶

一文搞定Redis五大資料類型及應用場景
##在這裡插入圖片描述
案例二:根據一定的推薦機制在set裡邊存放對應的用戶,然後根據日期每天推薦的用戶都不能重複

一文搞定Redis五大資料類型及應用場景
在這裡插入圖片描述

4-4 set類型業務場景挖掘使用者關係

#兩個集合的交、並、差集

<span style="display: block; background: url(https://my-wechat.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #272822; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #ddd; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; letter-spacing: 0px; padding-top: 15px; background: #272822; border-radius: 5px;"><span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">sinter</span> <span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">key</span> <span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">key1</span><br/><span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">sunion</span> <span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">key</span> <span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">key1</span><br/><span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">sdiff</span> <span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">key</span> <span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">key1</span><br/></code>
登入後複製

兩個集合的交、並、差集並儲存到指定集合中

<span style="display: block; background: url(https://my-wechat.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #272822; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #ddd; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; letter-spacing: 0px; padding-top: 15px; background: #272822; border-radius: 5px;"><span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">sinterstore</span> <span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">destination</span> <span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">key1</span> <span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">key2</span><br/><span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">sunionstore</span> <span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">destination</span> <span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">key1</span> <span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">key2</span><br/><span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">sdiffstore</span> <span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">destination</span> <span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">key1</span> <span class="hljs-selector-tag" style="color: #f92672; font-weight: bold; line-height: 26px;">key2</span><br/></code>
登入後複製

案例:我們需要挖掘一個資訊的共同好友。例如微信公眾號的共同關注好友數量、QQ添加新好友的推薦機制、深度挖掘用戶直接的聯繫一文搞定Redis五大資料類型及應用場景

#就根據上述案例,我們可以使用差集來實現qq的有可能認識的好友。

4-5 set類型業務場景實作網站的PV  UV  IP的記錄

PV直接使用string類型的incr統計即可

UV和IP都是獨立不重複的,使用set來操作。

在上邊我們知道set有一個特性就是不能重複,我們就可以根據這一點來輕鬆實現這個功能。然後使用scard key 來統計數量。

至於UV是獨立訪客,使用本地的cookie來實作就可以,方法一樣把cookie傳給redis做記錄即可一文搞定Redis五大資料類型及應用場景

5. sorted_set類型

在之前的四個類型中都不支援排序的,下來咱們看的sorted_set類型是既支援儲存大數據,也支援排序功能

5-1. sorted_set類型基本運算

新增資料:zadd key score member

#取得資料:zrange key start stop | zrevrange key start stop

刪除資料:zrem key member

一文搞定Redis五大資料類型及應用場景依條件取得資料:zrangebyscore key min max limit | zrevrangescore key max min

#條件刪除資料:zremrangebyrank key start stop | zremrangebyscore key min max

取得集合資料總量:zcard key | zcount key min max

<a href="https://www.php.cn/redis/" target="_blank"></a>

### #集合交、並操作:###zinterstore destination numkeys key | zunionstore destination numkeys key###(這個指令就不做演示了,可以自己查看文檔。跟set有點類似,只不過會把所有交集的和給加起來。然後這裡邊有個numkeys這個參數是一共幾個key進行計算 後邊的key就需要幾個)######取得資料對應的索引:###zrank key member | zrevrank key member## #######socre值取得與修改:###zscore key member | zincrby key increment member############相關推薦:《###redis教學###》# #####

總結

以上就是redis資料型別的簡單介紹與具體應用,後文會針對具體需求在進行實戰。

堅持學習、堅持寫博、堅持分享是咔咔從業以來一直所秉持的信念。希望在偌大互聯網中咔咔的文章能帶給你一絲絲幫助。我們下期再見。

以上是一文搞定Redis五大資料類型及應用場景的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!