简体   繁体   English

我可以将现有的 Redis 用于自定义 Rails 缓存吗?

[英]Can I use my existing Redis for a custom rails cache?

The documentation for the ActiveSupport::Cache::RedisCacheStore states: ActiveSupport::Cache::RedisCacheStore文档指出:

Take care to use a dedicated Redis cache rather than pointing this at your existing Redis server.注意使用专用的 Redis 缓存,而不是将其指向现有的 Redis 服务器。 It won't cope well with mixed usage patterns and it won't expire cache entries by default.它不能很好地应对混合使用模式,并且默认情况下不会使缓存条目过期。

Is this advice still true in general, especially when talking about custom data caches, not page (fragment) caches?这个建议在一般情况下是否仍然正确,尤其是在谈论自定义数据缓存而不是页面(片段)缓存时?

Or, more specifically, If I'm building a custom cache for specific "costly" backend calls to a slow 3rd-party API and I set an explicit expires_in value on my cache (or all my cached values), does this advice apply to me at all?或者,更具体地说,如果我正在为对缓慢的第 3 方 API 的特定“昂贵”后端调用构建自定义缓存,并且我在缓存(或所有缓存值)上设置了显式expires_in值,那么此建议是否适用于我呢?

TLDR; TLDR; yes, as long as you set an eviction policy.是的,只要您设置驱逐政策。 Which one?哪一个? Read on.继续阅读。

On the same page, the docs for #new state that:在同一页面上, #new的文档:

No expiry is set on cache entries by default.默认情况下,缓存条目没有过期。 Redis is expected to be configured with an eviction policy that automatically deletes least-recently or -frequently used keys when it reaches max memory. Redis 预计将配置一个驱逐策略,该策略会在达到最大 memory 时自动删除最近最少或经常使用的密钥。 See redis.io/topics/lru-cache for cache server setup.有关缓存服务器设置,请参阅 redis.io/topics/lru-cache。

This is more about memory management and access patterns than what's being cached.这更多的是关于 memory 管理和访问模式,而不是缓存的内容。 The Redis eviction policy documentation has a detailed section for policy choice and mixed usage (whether to use a single instance or not): Redis驱逐策略文档有一个关于策略选择和混合使用(是否使用单个实例)的详细部分:

Picking the right eviction policy is important depending on the access pattern of your application, however you can reconfigure the policy at runtime while the application is running, and monitor the number of cache misses and hits using the Redis INFO output to tune your setup.根据应用程序的访问模式选择正确的驱逐策略很重要,但是您可以在应用程序运行时在运行时重新配置策略,并使用 Redis INFO output 监控缓存未命中和命中的数量来调整您的设置。

In general as a rule of thumb:一般来说,根据经验:

  • Use the allkeys-lru policy when you expect a power-law distribution in the popularity of your requests.如果您希望请求的受欢迎程度呈幂律分布,请使用allkeys-lru策略。 That is, you expect a subset of elements will be accessed far more often than the rest.也就是说,您预计元素子集的访问频率将远高于 rest。 This is a good pick if you are unsure.如果您不确定,这是一个不错的选择。

  • Use the allkeys-random if you have a cyclic access where all the keys are scanned continuously, or when you expect the distribution to be uniform.如果您有一个循环访问,其中所有键都被连续扫描,或者当您希望分布均匀时,请使用allkeys-random

  • Use the volatile-ttl if you want to be able to provide hints to Redis about what are good candidate for expiration by using different TTL values when you create your cache objects.如果您希望能够通过在创建缓存对象时使用不同的 TTL 值向 Redis 提供关于什么是好的过期候选者的提示,请使用volatile-ttl

The volatile-lru and volatile-random policies are mainly useful when you want to use a single instance for both caching and to have a set of persistent keys. volatile-lruvolatile-random策略主要在您想使用单个实例进行缓存并拥有一组持久键时非常有用。 However it is usually a better idea to run two Redis instances to solve such a problem.但是,通常最好运行两个 Redis 实例来解决此类问题。

It is also worth noting that setting an expire value to a key costs memory, so using a policy like allkeys-lru is more memory efficient since there is no need for an expire configuration for the key to be evicted under memory pressure.还值得注意的是,为密钥设置过期值会花费 memory,因此使用诸如allkeys-lru 之类的策略更有效,因为在 ZCD69B4957F08CD8218 压力下不需要expire配置来驱逐密钥。

You do not have mixed usage.您没有混合使用。 For example, you do not persist Sidekiq jobs in Redis, which have no TTL/expiry by default.例如,您不会在 Redis 中保留 Sidekiq 作业,默认情况下它们没有 TTL/到期。 So, you can treat your Redis instance as cache-only.因此,您可以将 Redis 实例视为仅缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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