简体   繁体   中英

How to perform Redis client side caching of a Sorted Set, by rank

I have an application which pulls the 'top ten' from a Redis Sorted Set. That is, the members of the set with the highest ranking, eg ZRANGE top40 0 9 REV . I would like to cache this result on the client and be informed by the Server when the members of this subset change.

From my research so far, I have understood that Redis server-assisted client-side caching (RSACSC) is implemented such that the server keeps a record of the keys which are requested by each client and will inform that client (or another) when the keys change . This would imply that the server is not tracking their rank within a Sorted Set.

What I want to achieve is this:

  1. I have a Sorted Set which contains the Top 40 songs, ranked by number of streams
  2. I have a (Python) client which fetches and caches the highest ranking 10 from the Top 40
  3. If a new song comes in at number 5, the server will send an expiration message for either
    • cached members 5-10, or
    • the entire cached subset of 10 members

I would like to understand if RSACSC can accommodate this as-is. If not, can it be achieved using other Redis trickery, for example somehow storing the Top 10 server-side as a new key and pulling this key into the Python app.

I would hugely appreciate an example using a Python client, although the main purpose of the question is to better understand RSACSC and its limitation regarding Sorted Sets, plus any workarounds.


Having done some more research, and using a real-world dataset (and following the positive confirmation from @guyroyse), it seems that any change to the Sorted Set results in a client invalidate notification, even if the change happened outside of the subset of members that the client requested. Redis does indeed appear to treat the entire Set's key as a single entity, rather than track the client's request for and range of members.

In this way, (3) is not possible. Therefore this becomes a question of workarounds, eg some server-side ability to automatically store the subset as a separate key.

Having done some more research, and using a real-world dataset, I can confirm that any change to the Sorted Set results in a client invalidate notification, even if the change happened outside of the subset of members that the client requested. Redis does indeed appear to treat the entire Set's key as a single entity, rather than track the client's request for and range of members.

In this way, (3) is not possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM