【问题标题】:Cache-Money: Only use in Production?Cache-Money:仅在生产中使用?
【发布时间】:2009-01-17 18:16:17
【问题描述】:

我使用cache-money gem 来透明地使用 Memcached。使用提供的配置文件,它可以在所有模式(开发、测试、生产)上启用。有没有办法只在生产模式下激活缓存货币?

目前还不清楚如何做到这一点,而且在开发模式下处理缓存是一件非常痛苦的事情。

【问题讨论】:

    标签: ruby-on-rails memcached


    【解决方案1】:

    感谢Obie Fernandez 提供了一个很好的离线提示:存根 cache-money 的 #index 方法什么都不做。这为模型中的#index 语句提供了一个位置,并停止了上述错误。

    这是我完整的 cache_money.rb 库:

    if RAILS_ENV != 'development'
      require 'cache_money'
    
      config = YAML.load(IO.read(File.join(RAILS_ROOT, "config", "memcached.yml")))[RAILS_ENV]
      $memcache = MemCache.new(config)
      $memcache.servers = config['servers']
    
      $local = Cash::Local.new($memcache)
      $lock = Cash::Lock.new($memcache)
      $cache = Cash::Transactional.new($local, $lock)
    
      class ActiveRecord::Base
        is_cached :repository => $cache
      end
    else
      # If we're in development mode, we don't want to
      # deal with cacheing oddities, so let's overrite
      # cache-money's #index method to do nothing...
      class ActiveRecord::Base
        def self.index(*args)
        end
      end
    end
    

    【讨论】:

      【解决方案2】:

      通过在测试中关闭缓存资金,您无法知道它是否会干扰您的代码。

      我这样做了:

      require 'cache_money'
      require 'memcache'
      
      if RAILS_ENV == 'test'
        $memcache = Cash::Mock.new
      else
        config = YAML.load(IO.read(File.join(RAILS_ROOT, "config", "memcached.yml")))[RAILS_ENV]
        $memcache = MemCache.new(config)
        $memcache.servers = config['servers']
      end
      
      $local = Cash::Local.new($memcache)
      $lock = Cash::Lock.new($memcache)
      $cache = Cash::Transactional.new($local, $lock)
      
      class ActiveRecord::Base
        is_cached :repository => $cache
      end
      

      【讨论】:

        【解决方案3】:

        如果您在开发模式下运行,请在初始化程序中跳过初始化:

        unless 'development' == RAILS_ENV
          require 'cache_money'
          ....
        

        【讨论】:

        • 我希望就这么简单。未加载缓存货币时,我的索引模型无法加载:/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.0/lib/active_record/base.rb:1963:in @987654322 @index' 用于#<0x20215b4>
        猜你喜欢
        • 1970-01-01
        • 2019-02-20
        • 1970-01-01
        • 1970-01-01
        • 2013-01-19
        • 2012-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多