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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.
