2009-01-17 64 views
1

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

目前尚不清楚如何做到这一点,而且这是一种在开发模式下处理缓存的总体痛苦。

回答

5

感谢Obie Fernandez为一个伟大的离线提示:捻熄缓存钱的#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 
0

在你的初始化,初始化跳过如果你在开发模式下运行:

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

我希望它是那么容易。当缓存资金未加载时,我的索引模型无法加载: /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.0/lib/active_record/base.rb:1963:in `method_missing_without_paginate':未定义的方法`index'为#(NoMethodError) – 2009-02-04 14:07:26

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