Based on @ybak's answer, I will introduce a relatively simple and practical solution.
Limit the memory occupied by Redis. Redis will load hot data into memory according to its own data elimination strategy. So, calculate the approximate memory occupied by 20W data, and then set the Redis memory limit.
Such as user data. The database has 20 million entries. Active users: redis sortSet puts users who have logged in within two days (for convenience, active users within one day), log in ZADD once, and overwrite their scores (login time) if the set already exists. Key: login:users, value: score timestamp, value userid. Set up a periodic task, such as deleting the data in the sort set before 3 o'clock on the previous day at 03:00:00 every day (to ensure that the set does not grow disorderly and retain active users in the past day).
When fetching, get the current timestamp (int 10 digits), then subtract 1 day to get the active users in the past 24 hours according to the score range.
Looking at your question, you should just use Redis as a cache. Provide a simple way to implement cache invalidation: LRU (recently less used elimination) That is, every time the redis cache hits, the hit cache will be increased by a certain amount ttl (expiration time) (set according to specific circumstances, such as 10 minutes). After a period of time, the ttl of hot data will be larger and will not automatically expire, while cold data will basically expire immediately after it exceeds the set ttl
When the size of the redis memory data set increases to a certain size, the data elimination strategy will be implemented.
redis provides 6 data elimination strategies:
volatile-lru: Select the least recently used data from the data set with expiration time set to eliminate volatile-ttl volatile-random allkeys-lru allkeys-random no-enviction
Based on @ybak's answer, I will introduce a relatively simple and practical solution.
Limit the memory occupied by Redis. Redis will load hot data into memory according to its own data elimination strategy.
So, calculate the approximate memory occupied by 20W data, and then set the Redis memory limit.
What data is the question?
Such as user data. The database has 20 million entries.
Active users:
redis sortSet puts users who have logged in within two days (for convenience, active users within one day), log in ZADD once, and overwrite their scores (login time) if the set already exists. Key: login:users, value: score timestamp, value userid. Set up a periodic task, such as deleting the data in the sort set before 3 o'clock on the previous day at 03:00:00 every day (to ensure that the set does not grow disorderly and retain active users in the past day).
When fetching, get the current timestamp (int 10 digits), then subtract 1 day to get the active users in the past 24 hours according to the score range.
Looking at your question, you should just use Redis as a cache.
Provide a simple way to implement cache invalidation: LRU (recently less used elimination)
That is, every time the redis cache hits, the hit cache will be increased by a certain amount ttl (expiration time) (set according to specific circumstances, such as 10 minutes).
After a period of time, the ttl of hot data will be larger and will not automatically expire, while cold data will basically expire immediately after it exceeds the set ttl
When the size of the redis memory data set increases to a certain size, the data elimination strategy will be implemented.
redis provides 6 data elimination strategies:
volatile-lru: Select the least recently used data from the data set with expiration time set to eliminate
volatile-ttl
volatile-random
allkeys-lru
allkeys-random
no-enviction