Home > Database > Redis > How redis solves paging queries

How redis solves paging queries

(*-*)浩
Release: 2019-11-27 10:11:37
Original
6602 people have browsed it

We all know that caching query results can greatly improve the service capabilities of the system and reduce the pressure on the underlying service or database. For caching with paging conditions, we can also cache multiple keys according to different paging conditions.

How redis solves paging queries

Paging query caching solution based on SortedSet

The first solution that comes to mind is to use @see ListOperations Instead of using multiple keys according to paging conditions, use one key and cache all data into redis without paging, and then use range (key, start, limit) according to paging conditions to obtain the paging results. (Recommended study: Redis video tutorial)

This will cause a problem. When the cache fails, concurrent write cache will cause duplicate data, So I thought of passing Use set to handle duplicate data during concurrency, @see ZSetOperations

The code logic is as follows:

range(key,start,limit)按照分页条件获取缓存,命中则直接返回
缓存未命中,查询(没有分页条件)数据库或是调用(没有分页)底层接口
add(key,valueScoreMap<value,score>)写入缓存,expire设置缓存时间
当需要清理缓存时,直接删除key,如果是因为数据新增和删除,可以add(key,value,score)或remove(key,value)
Copy after login

redis will be divided according to score Arrange the data in the map in ascending order of value. Generally, the score score is the value of filedA in the order by filedA of the SQL statement. This can ensure data consistency

But this method also has certain problems:

The value cached by this key is indeed hot data, but only a small number of data may be frequently used and the rest may not be used at all. For example, if there are 100 pages of data, only the first 10 may actually be used. Page, this will also lead to a waste of cache space. If redis virtual memory is used, it will also have a certain impact.

SQL query has changed from the original paging query to a non-paging query. After the cache fails, the system processing The ability will be lower than before, especially for large tables.

For more Redis-related technical articles, please visit the Redis Getting Started Tutorial column to learn!

The above is the detailed content of How redis solves paging queries. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template