首頁  >  文章  >  web前端  >  雷迪斯2

雷迪斯2

WBOY
WBOY原創
2024-08-14 17:28:31344瀏覽

Redis 2

Redis - 清單、集合和 HashMap

這是上一篇部落格的延續

除了鍵值對之外,還有其他 3 種資料型別可用

清單

此資料結構類似 Python 中的列表或 Javascript 或 C# 中的陣列。它們可用於保存最近使用過的物品。常見的操作有;

Operations Explanations Examples Further Explanation
LPUSH Adds an Item to the beginning of the list LPUSH friends "Sophia" similar to Array.unshift in javascript
LRANGE Get all the items in a list LRANGE friends 0 -1 similiarly a list in python 0 is the first item and -1 is the last item
RPUSH Adds an item to the end of the list RPUSH friends "Poe" similar to Array.push in javascript
LPOP Removes an item from the start of the list LPOP friends "Poe" Will return 1 if Poe exists in the list and 0 otherwise
RPOP Removes an item from the end of the list RPOP friends "Sophia" Will return 1 if Sophia exists in the list and 0 otherwise

僅包含唯一項目的資料結構。類似 Python 中的集合、Javascript 中的集合和 C# 中的 HashSet。常見操作包括;

Operations Explanations Examples Further Explanation
SADD Adds a value to the set SADD colors "pink"
SMEMBERS returns the members of the set SMEMBERS colors will return all the items in the set colors
SREM Removes members of the set SREM colors "pink" Will return 1 if pink exists in the list and 0 otherwise

哈希圖

雜湊圖是一組鍵值對。然而,哈希圖不能嵌套。讓我們來看一個有姓名、電子郵件和電話號碼的人的案例場景

HSET person name "Joe" # Adds the key-value pair {name : joe} to the hashmap
HSET person email "Joe@joe.com" # Adds the key-value pair {email : Joe@joe.com} to the hashmap
HSET person phone_number "+2345656655413" # Adds the key-value pair {number : ....} to the hashmap

HGET 指令可用來取得雜湊映射中特定鍵的值

HGET person name # returns "Joe"

HGETALL指令hashmap中的所有鍵值對

HGETALL person 

1) "name"
2) "Joe"
3) "email"
4) "Joe@joe.com"
5) "phone_number"
6) "+2345656655413"

HDEL 指令透過鍵刪除鍵值對

HDEL person name # removes {name : joe}

HEXISTS 指令檢查雜湊集中是否存在某個鍵

HEXISTS person name # returns 0 because we've deleted it before

這些是開發人員需要了解的大部分基本命令。


常見面試問題

  1. Redis 的完整意義是什麼? :Redis 代表遠端字典伺服器

  2. Redis 與 MySQL 等傳統資料庫有何不同? :Redis 在主記憶體中運行,具有快速存取值的基本操作,不像 SQL 駐留在磁碟上並具有廣泛的增刪改查操作

  3. Redis 在資料大小和類型方面有哪些限制? :Redis 旨在保存適合機器記憶體的值。它不適合具有連接的複雜關係模型或建立大型 blob

  4. 如何使用 Redis 在分散式環境中處理快取? :透過將 Redis 實例設定為資料庫前面的快取層來處理快取。使用一致性雜湊來分配快取節點上的金鑰,確保負載分佈均勻並減少快取未命中

  5. 什麼時候應該在 Redis 中使用列表資料類型? :清單非常適合持久原子佇列、作業佇列、日誌、緩衝區和許多其他用例

以上是雷迪斯2的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn