【问题标题】:Disable Rails caching if Redis is down如果 Redis 关闭,则禁用 Rails 缓存
【发布时间】:2016-02-01 03:56:49
【问题描述】:

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

但是如果 Redis 宕机了怎么办? readthis 和 redis-store 都完全失败。我宁愿让网站在没有缓存的情况下变慢而不是死掉。

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

【问题讨论】:

  • 欢迎来到 StackOverflow。当人们回答您的问题并解决您的问题时,您可以接受您最喜欢的答案。要了解有关接受答案如何工作的更多信息,请参阅此帖子:meta.stackexchange.com/questions/5234/…

标签: ruby-on-rails caching redis


【解决方案1】:

截至https://github.com/sorentwo/readthis/pull/30,这可直接在 Readthis 中获得。它将在即将发布的 1.2 版本中提供。来自自述文件:

在某些情况下,希望保持从磁盘为请求提供服务 如果 Redis 崩溃,则数据库。这可以通过连接来实现 通过在顶层启用容错:

Readthis.fault_allow = true

默认值为 false,因为它可能适用于 fetch 操作,它与其他基于状态的命令不兼容,例如 递增。

【讨论】:

    【解决方案2】:

    这里有一个关于这个话题的有趣讨论:Don't crash the application if redis is down

    由于问题仍然存在并且他们还没有合并任何修复,您可以使用讨论中的少数建议之一,即像这样的猴子补丁:

    # 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 
    

    【讨论】:

    • 谢谢 K M。我在 github 上看到过这个补丁,但有点犹豫要不要实现它。我希望有人有更强大的解决方案,但我想我可以试试这个。
    猜你喜欢
    • 2015-03-18
    • 1970-01-01
    • 2019-02-10
    • 2015-05-14
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    相关资源
    最近更新 更多