Home  >  Article  >  Database  >  How redis caches paging data

How redis caches paging data

(*-*)浩
(*-*)浩Original
2019-11-28 10:34:566516browse

We often need to cache data that needs paging in our projects. There are several ideas as follows. Let’s compare the advantages and disadvantages of each

How redis caches paging data

## An idea:

Each page of data uses a key cache, and sets the expiration time respectively. It is initialized when the data is requested and is not in the cache. PageIndex and pageSize are used as part of the cache key, for example: (Recommended learning: Redis video tutorial)

key: cstm:list:${pageIndex}_${pageSize}

value: serialization (List)


Each page of data uses a key cache. When the data is updated, the data of all pages is actively updated. pageIndex and pageSize are used as part of the cache key, for example:

key: cstm:list:${pageIndex}_${pageSize}

value: serialization (List)

Use ordered sets to cache hotspot data, sort conditions as scores, and use the command [ZRANGE key start stop [WITHSCORES]] to achieve paging effects

key: cstm:zset:pageList

value: Serialization (Customer)


Analysis of advantages and disadvantages:

The first method: initialize between data addition and deletion Caching will cause data duplication or omission. For example: after the first page of data is cached, if there is new data, when the second page of data is cached, the cached data of the two pages will be duplicated

Second The first method: If the data is updated frequently, the cached data will be updated more frequently.

The third method: This method is relatively friendly. You need to maintain the cache while the data changes, and pay attention to trimming the list length

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 caches paging data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:How redis stores listNext article:How redis stores list