Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Paging with redis cache

2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail about the implementation of redis cache pagination, the editor thinks it is very practical, so share it with you as a reference, I hope you can get something after reading this article.

In the actual business, we will cache some hot data in redis. If the amount of data is large, we need to page these hot data in two ways:

First: after taking out all the data from redis, do memory paging (not recommended). You can do this when the hot data is small, and the performance difference is not very big, but when the amount of data is large, it will take up a lot of memory or burst during paging.

Second: cache paging based on redis data structure, which is divided into two types.

①: the list data structure based on redis can be paged directly through the data structure of list, and the range method can be used for paging. When the amount of data is large, the performance is also considerable, but when there is high concurrent access to the interface, the list may be extended indefinitely, and there will be a lot of duplication of data in it, which will affect normal business (not highly recommended).

②: based on the ZSet data structure of redis, we can also do paging through the ordered collection of Zset, which is also using the range method, but the trouble here is that when initializing the data, Zset must store data of type TypedTuple, which is a key-value pair of value and score. You can check Baidu specifically. The generation of this score is troublesome when I test using the location of the current data in this list. Then Zset is sorted according to this score value, and the default is from small to large. The advantage of using this is that even in the case of high concurrency, there is no duplicate data in Zset, which affects normal business, and the paging efficiency is similar to that of list structure.

③: use hash and Zset to implement together; this is to ask a friend and know that the orderly id field stored in Zset, get the id after paging, and then use id to fetch it from the hash. It feels that the efficiency should be the same, but there are more layers in the middle to take from the hash structure, and you need to maintain another hash; (why I don't know why)

Post a picture of the results of my tests on list and ZSet

So much for the paging of redis cache implementation. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report