如果Redis关闭,则禁用Rails缓存

我正在使用Rails 4.1并设置共享Redis ElasticCache节点进行缓存。 我尝试了https://github.com/redis-store/redis-store和https://github.com/sorentwo/readthis ,看起来很棒。

但是如果Redis失败会怎么样? readthis和redis-store都完全失败了。 我宁愿让网站缓慢无缓存而不是死机。

有没有人有想法? 我提前谢谢你。

从https://github.com/sorentwo/readthis/pull/30开始,这可直接在Readthis中找到。 它将在即将发布的1.2版本中提供。 来自README:

在某些情况下,如果Redis崩溃,则需要继续从磁盘或数据库提供请求。 这可以通过在顶层启用它来实现连接容错:

Readthis.fault_tolerant = true

默认值为false,因为虽然它可能适用于提取操作,但它与其他基于状态的命令(如增量)不兼容。

以下是关于此主题的有趣讨论: 如果redis关闭,请不要崩溃应用程序

由于问题仍然存在,并且它们尚未合并任何修复,您可以使用讨论中的一些建议,即猴子修补,如下所示:

# patch to do not crash on redis backend errors # https://github.com/redis-store/redis-rails/issues/14 module ActiveSupport module Cache class RedisStore %w[increment decrement clear read_entry write_entry delete_entry].each do |method| define_method "#{method}_with_rescue" do |*args, &block| begin self.send "#{method}_without_rescue", *args, &block rescue nil end end alias_method_chain method, :rescue end end end end