搜尋
首頁資料庫Redis一文搞定Redis五大資料類型及應用場景

本文學習知識點 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="/static/imghwm/default1.png" data-src="https://img-blog.csdnimg.cn/20200525112247102.png?x-oss-process=image/resize,p_40" class="lazy" 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中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
REDIS:探索其功能和功能REDIS:探索其功能和功能Apr 19, 2025 am 12:04 AM

Redis脫穎而出是因為其高速、多功能性和豐富的數據結構。 1)Redis支持字符串、列表、集合、散列和有序集合等數據結構。 2)它通過內存存儲數據,支持RDB和AOF持久化。 3)從Redis6.0開始引入多線程處理I/O操作,提升了高並發場景下的性能。

Redis是SQL還是NOSQL數據庫?答案解釋了Redis是SQL還是NOSQL數據庫?答案解釋了Apr 18, 2025 am 12:11 AM

RedisisclassifiedasaNoSQLdatabasebecauseitusesakey-valuedatamodelinsteadofthetraditionalrelationaldatabasemodel.Itoffersspeedandflexibility,makingitidealforreal-timeapplicationsandcaching,butitmaynotbesuitableforscenariosrequiringstrictdataintegrityo

REDIS:提高應用程序性能和可擴展性REDIS:提高應用程序性能和可擴展性Apr 17, 2025 am 12:16 AM

Redis通過緩存數據、實現分佈式鎖和數據持久化來提升應用性能和可擴展性。 1)緩存數據:使用Redis緩存頻繁訪問的數據,提高數據訪問速度。 2)分佈式鎖:利用Redis實現分佈式鎖,確保在分佈式環境中操作的安全性。 3)數據持久化:通過RDB和AOF機制保證數據安全性,防止數據丟失。

REDIS:探索其數據模型和結構REDIS:探索其數據模型和結構Apr 16, 2025 am 12:09 AM

Redis的數據模型和結構包括五種主要類型:1.字符串(String):用於存儲文本或二進制數據,支持原子操作。 2.列表(List):有序元素集合,適合隊列和堆棧。 3.集合(Set):無序唯一元素集合,支持集合運算。 4.有序集合(SortedSet):帶分數的唯一元素集合,適用於排行榜。 5.哈希表(Hash):鍵值對集合,適合存儲對象。

REDIS:對其數據庫方法進行分類REDIS:對其數據庫方法進行分類Apr 15, 2025 am 12:06 AM

Redis的數據庫方法包括內存數據庫和鍵值存儲。 1)Redis將數據存儲在內存中,讀寫速度快。 2)它使用鍵值對存儲數據,支持複雜數據結構,如列表、集合、哈希表和有序集合,適用於緩存和NoSQL數據庫。

為什麼要使用redis?利益和優勢為什麼要使用redis?利益和優勢Apr 14, 2025 am 12:07 AM

Redis是一個強大的數據庫解決方案,因為它提供了極速性能、豐富的數據結構、高可用性和擴展性、持久化能力以及廣泛的生態系統支持。 1)極速性能:Redis的數據存儲在內存中,讀寫速度極快,適合高並發和低延遲應用。 2)豐富的數據結構:支持多種數據類型,如列表、集合等,適用於多種場景。 3)高可用性和擴展性:支持主從復制和集群模式,實現高可用性和水平擴展。 4)持久化和數據安全:通過RDB和AOF兩種方式實現數據持久化,確保數據的完整性和可靠性。 5)廣泛的生態系統和社區支持:擁有龐大的生態系統和活躍社區,

了解NOSQL:Redis的關鍵特徵了解NOSQL:Redis的關鍵特徵Apr 13, 2025 am 12:17 AM

Redis的關鍵特性包括速度、靈活性和豐富的數據結構支持。 1)速度:Redis作為內存數據庫,讀寫操作幾乎瞬時,適用於緩存和會話管理。 2)靈活性:支持多種數據結構,如字符串、列表、集合等,適用於復雜數據處理。 3)數據結構支持:提供字符串、列表、集合、哈希表等,適合不同業務需求。

REDIS:確定其主要功能REDIS:確定其主要功能Apr 12, 2025 am 12:01 AM

Redis的核心功能是高性能的內存數據存儲和處理系統。 1)高速數據訪問:Redis將數據存儲在內存中,提供微秒級別的讀寫速度。 2)豐富的數據結構:支持字符串、列表、集合等,適應多種應用場景。 3)持久化:通過RDB和AOF方式將數據持久化到磁盤。 4)發布訂閱:可用於消息隊列或實時通信系統。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)