Cache-Money:仅用于生产?

时间:2009-01-17 18:16:18

标签: ruby-on-rails memcached

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

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

3 个答案:

答案 0 :(得分:5)

感谢Obie Fernandez提供了一个很棒的离线提示:将cache-money的#index方法无效。这为模型中的#index语句提供了一个位置,并停止了上述错误。

这是我的完整cache_money.rb lib:

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

答案 1 :(得分:1)

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

我这样做了:

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

答案 2 :(得分:0)

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

unless 'development' == RAILS_ENV
  require 'cache_money'
  ....
相关问题