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

雷迪斯2

WBOY
WBOY原创
2024-08-14 17:28:31348浏览

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