Sidekiq Worker 不会使缓存过期

Sidekiq Workers Not Expiring Cache

我们的架构有专用服务器,只有 运行 个 Sidekiq 工作进程,即没有应用程序服务器。我们正在进行一些模型缓存 - 所有对模型内部 Rails.cache 的调用都指定了一个 expires_in 值,但过期的缓存不会被清除。

有时磁盘 inode 会填满,因为 tmp/cache 从未被清理过。

修复很简单 - 运行 tmp:cache:clear 的 cron 作业,但也许社区可以就此场景 w/r 提供一些指导和理解如何 Rails清除过期的模型缓存键,尤其是当 Sidekiq worker 是服务器上唯一 运行ning 时。

过期由缓存存储处理,如果它支持这样的话。 Rails' default cache store(文件存储)很简单,知道的不多。你在做什么,手动清除缓存是文档(下面)所建议的。

你可以试试Redis or Memcached,他们知道如何过期。

摘自ActiveSupport::Cache::FileStore

2.4 ActiveSupport::Cache::FileStore

This cache store uses the file system to store entries. The path to the directory where the store files will be stored must be specified when initializing the cache.

As the cache will grow until the disk is full, it is recommended to periodically clear out old entries.

This is the default cache store implementation (at "#{root}/tmp/cache/") if no explicit config.cache_store is supplied.

还有一个来自 ActiveSupport::Cache::Store

:expires_in - This option sets an expiration time in seconds for the cache entry, if the cache store supports it, when it will be automatically removed from the cache.