Redis的数据淘汰策略

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_42220174/article/details/102097955

问:MySQL中有2000w数据,Redis中只存20w的数据,如何保证Redis中的数据都是热点数据?

答:相关知识:Redis内存数据集大小上升到一定大小(设置项为maxmemory)的时候,就会执行数据淘汰策略(回收策略)。Redis提供了6种数据淘汰策略。

Redis配置文件中的关于设置maxmemory(Redis的最大使用内存)的代码maxmemory <bytes>,默认是0并且以字节为单位的。如果有新的数据添加,超过最大内存,则会使Redis崩溃。Redis内存数据集大小上升到一定大小(设置项为maxmemory)的时候,就会执行数据淘汰策略(回收策略)Redis支持6种数据淘汰策略,默认是maxmemory-policy noeviction:禁止驱逐。以下是6种数据淘汰算法的介绍:

  • volatile-lru:使用LRU算法删除具有过期时间的密钥
  • allkeys-lru:根据LRU算法删除任何密钥
  • volatile-random:删除带有过期集的随机密钥
  • allkeys-random:删除一个随机密钥
  • volatile-ttl:删除具有最近过期时间的密钥
  • noeviction:完全不过期,只是在写操作上返回一个错误

源文件相关配置代码(不同的版本可能有差异,重要的说明项已在原文代码中提供翻译):

# Note on units: when memory size is needed, it is possible to specify
# it in the usual form of 1k 5GB 4M and so forth:
#
# 1k => 1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes
# 1mb => 1024*1024 bytes
# 1g => 1000000000 bytes
# 1gb => 1024*1024*1024 bytes
# 单位是不区分大小写的,所以1GB 1Gb 1gB都是一样的
# units are case insensitive so 1GB 1Gb 1gB are all the same.

# WARNING: not setting maxmemory will cause Redis to terminate with an
# out-of-memory exception if the heap limit is reached. 
# 警告:未设置maxmemory将导致Redis在达到堆限制时发生内存不足异常

# maxmemory <bytes>
# 设置最大内存,默认单位是byte,即字节

# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached. You can select among five behaviors:
# 最大内存策略:当达到最大内存时,Redis如何选择删除什么。您可以选择五种行为:
#
# volatile-lru -> remove the key with an expire set using an LRU algorithm :使用LRU算法删除具有过期集的密钥
# allkeys-lru -> remove any key according to the LRU algorithm :根据LRU算法删除任何密钥
# volatile-random -> remove a random key with an expire set :删除带有过期集的随机密钥
# allkeys-random -> remove a random key, any key :删除一个随机密钥,任何密钥
# volatile-ttl -> remove the key with the nearest expire time (minor TTL) :删除具有最近过期时间的密钥
# noeviction -> don't expire at all, just return an error on write operations :完全不过期,只是在写操作上返回一个错误
#
# Note: with any of the above policies, Redis will return an error on write
#       operations, when there are no suitable keys for eviction.
# 注意:对于以上任何一种策略,当没有合适的清除键时,Redis将在写操作上返回一个错误。    
#       At the date of writing these commands are: set setnx setex append
#       incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd
#       sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby
#       zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
#       getset mset msetnx exec sort
#
# The default is:
#  默认的是:最大内存策略:不清除
# maxmemory-policy noeviction

# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached. You can select among five behaviors:
# 最大内存策略:当达到最大内存时,Redis将如何选择删除什么。您可以选择五种行为

# LRU and minimal TTL algorithms are not precise algorithms but approximated
# algorithms (in order to save memory), so you can tune it for speed or
# accuracy. For default Redis will check five keys and pick the one that was
# used less recently, you can change the sample size using the following
# configuration directive.
#
# The default of 5 produces good enough results. 10 Approximates very closely
# true LRU but costs a bit more CPU. 3 is very fast but not very accurate.
#
# maxmemory-samples 5

猜你喜欢

转载自blog.csdn.net/qq_42220174/article/details/102097955