redis具体场景该怎么样用?
接触redis一段时间了,5种类型都知道,但具体什么时候用哪种类型好像很混乱~求高手指点~
回复内容:
接触redis一段时间了,5种类型都知道,但具体什么时候用哪种类型好像很混乱~求高手指点~
结合我对redis的理解,回答一下这个问题
redis是什么
redis就是一个存储key-value键值对的仓库,如何使用redis在于如何理解你需要设计的系统的E-R的模型,然后合理的规划redis的数据库结构
场景
我举一个简单的消息系统的例子,业务需求:服务器端发送消息给用户
E-R模型:
1. 用户(uid,nickname,phone,mood)
2. 消息(mid,title,content,ts)
用户和消息之间是n:n的关系,一个消息可以发给多个用户,每个用户可以拥有多个消息
redis数据库设计
redis数据库设计的关键在于key的设计,我一般采用固定前缀+唯一后缀的方式,例如:
1. 消息实体是两层结构,考虑用redis的hash数据结构进行存储(ps:用基本的string也是没问题的),key为msg_[$mid],mid类似于mysql表的主key,在当前redis数据库要保证全局唯一,可使用redis的incr原子操作实现,value可以是array('title' => '标题', 'content' => '内容', 'ts' => '发布时间戳').这样,每次服务器端产生消息,构造这样一个key-value的键值对即可表示消息本身的内容
2. 用户实体,因为用户的信息需要更稳定的持久化存储,所以建议直接存储在mysql里,不需要进行迁移到redis中
3. 用户-消息关系,可以考虑redis的sets数据结构。key为unread_mids_[$uid]和read_mids_[$uid],value为mid的集合。每次消息发送给指定的用户时,只需要将消息放到对应的集合里即可
这样就能表示出消息系统对应的E-R模型了
总结
redis数据库如何使用,关键自于你如何设计和理解你要做系统的E-R模型,搞清楚实体之间的关系,在redis里设计相应的key-value键值对即可。至于那几种数据结构,对你设计redis数据库来说作用不大,只是起到更方便的效果,用memcache一样可以做到这些。
针对redis的提供的几种数据结构,我建议别浮在表面只是会调个api,一点技术含量都没有,建议去看一下源码实现,推荐链接:http://www.redisbook.com/en/latest/

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.
