求选择哪个缓存设计好一些?
最近在做一个类似贴吧一样的东西,在数据上想用redis缓存,但是现在有两个想法,不知道哪个更实际一些,求大家指点。
所有内容都会存mysql这个没的说
帖子列表永驻redis这个也没的说
问题是帖子内容怎么办。
一种想法是存一定数量的帖子内容到缓存里,比如1000条。用户访问时根据所在页面和条数,前1000条的请求都从redis中读取,超过1000条之后的旧数据就从mysql中读取。有新帖子且缓存达到1000条时就LPUSH新的RPOP旧的。
另一种想法是所有新增的帖子数据都存在redis中,并且都设置一个过期时间,比如24小时。用户有访问过就将这条数据的过期时间增加到24小时,如果没有访问过就从redis中删除。用户访问的时候先查redis,查不到再去mysql中查,顺便再写入redis。
这两种办法哪个好一些?
回复内容:
最近在做一个类似贴吧一样的东西,在数据上想用redis缓存,但是现在有两个想法,不知道哪个更实际一些,求大家指点。
所有内容都会存mysql这个没的说
帖子列表永驻redis这个也没的说
问题是帖子内容怎么办。
一种想法是存一定数量的帖子内容到缓存里,比如1000条。用户访问时根据所在页面和条数,前1000条的请求都从redis中读取,超过1000条之后的旧数据就从mysql中读取。有新帖子且缓存达到1000条时就LPUSH新的RPOP旧的。
另一种想法是所有新增的帖子数据都存在redis中,并且都设置一个过期时间,比如24小时。用户有访问过就将这条数据的过期时间增加到24小时,如果没有访问过就从redis中删除。用户访问的时候先查redis,查不到再去mysql中查,顺便再写入redis。
这两种办法哪个好一些?
可以说明用户规模。
我认为第二种好,因为通用性强。
第一种如果业务变大,访问增多,就要改代码
第二种当然更实用一些,代码也容易实现。第一个逻辑复杂一些不太好弄。。。。。
不过第二个,你还可以改进一下:
在缓存失效的时候,需要先查询redis,找不到数据,再查询MySQL,最后再写入redis。这里更新缓存的时候会多出两次redis查询。
比不使用缓存还要慢。。。
缓存失效的时候,如果有并发请求,则会重复更新redis缓存。
你可以参考我用Python实现的一个类解决了第一点https://github.com/lloydzhou/StaleRedisCache
给缓存多设置一段缓冲时间
若在缓冲期内,直接使用过期数据,并使用异步任务更新缓存。
你还可以借用redis实现一个内存锁,解决上面第二点的并发问题。
(我项目中没有做,是因为外面有nginx页面缓存,在里面应用层很难遇到并发问题)
我说说自己的设计,当然前提是直接查询DB是不可行的情况下,不然也没必要折腾。
首先缓存是必然,优先考虑用户在浏览贴吧时常看的基本就是前10页(举例),那么完全没必要缓存全量数据,最好是仅缓存前10页即可,这个设计无论访问量多大都不会有太大问题,只要不出现大量的用户访问10页之后的数据。对于这个缓存其实我们只需要缓存帖子列表,而非帖子具体内容,所以不会占用太大内存。
对于帖子内容可以采用热点方式解决,即LRU,因为被查看的帖子基本都是固定的,即热点帖子,缓存只需要缓存这些帖子内容即可。
综上,其实就是题主的两种方案的结合,其实这两种方案并不是针对同一个问题的解决方案。
第一种吧,如果用户更新了帖子,同样要更新对应的数据缓存,一般帖子,旧帖子翻的人很少的。
第一种。我在乐视做评论时,就用的第一种
缓存帖子内容,用mysql或redis还不如使用mongodb

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











In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.
