cache_fuとcache_fu_find_hookを使ってみる

環境

OS Window XP
Ruby 1.8.7p302(MinGW)
Rails 2.3.10

cache_fuとcache_fu_find_hookのインストール

cache_fuのインストール
ruby script/plugin install git://github.com/defunkt/cache_fu.git

エラーになるので・・・

Initialized empty Git repository in C:/rails2/app2/vendor/plugins/cache_fu/.git/

remote: Counting objects: 39, done.
remote: Compressing objects: 100% (37/37), done.
remote: Total 39 (delta 3), reused 23 (delta 0)
Unpacking objects: 100% (39/39), done.
From git://github.com/defunkt/cache_fu
 * branch            HEAD       -> FETCH_HEAD

** Checking for memcached in path...
script/plugin: No such file or directory - which memcached
Plugin not found: ["git://github.com/defunkt/cache_fu.git"]

vendor/plugins/cache_fu/install.rb の以下をコメントアウト

puts "** Checking for memcached in path..."
if `which memcached`.strip.empty?
  $errors += 1
  puts "!! Couldn't find memcached in your path.  Are you sure you installed it? !!"
  puts "!! Check the README for help.  You can't use acts_as_cached without it.  !!"
end

もう一度インストール

ruby vendor/plugins/cache_fu/install.rb
** Checking for memcache-client gem...
** Trying to copy memcached.yml.default to ./config/memcached.yml...
** Trying to copy memcached_ctl.default to ./script/memcached_ctl...

** acts_as_cached installed with no errors. Please edit the memcached.yml file t
o your liking.
** Now would be a good time to check out the README.  Enjoy your day.
cache_fu_find_hookのインストール
ruby script/plugin install git://github.com/morimori/cache_fu_find_hook.git

config/environment.rbを修正。

Rails::Initializer.run do |config|
  ...
  config.plugins = [:cache_fu, :cache_fu_find_hook, :all]
  ...
end

使ってみる

Modelにこれだけを定義

models/user.rb

class User < ActiveRecord::Base
  acts_as_cached
  after_save :reset_cache
  after_destroy :expire_cache
end
有効期限やfinderを指定することも可能
acts_as_cached :ttl => 1.minutes,
               :find_by => "col1"

※ finder を変更した場合は、:reset_cache, :expire_cache等は使えない。

Observerに定義できるか確認してみた

models/sqlite_observer.rb

class SqliteObserver < ActiveRecord::Observer
  observe :user
  
  acts_as_cached
  after_save :reset_cache
  after_destroy :expire_cache
end

エラーになる・・・

C:/rails2/app2/vendor/plugins/cache_fu_find_hook/lib/cache_fu_find_hook.rb:20:in `acts_as_cached': undefined method `find' for class `Class' (NameError)
	from C:/rails2/app2/app/models/sqlite_observer.rb:4
	from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.10/lib/active_support/dependencies.rb:406:in `load_without_new_constant_marking'
・・・

Observerに設定するのは無理っぽい

cache_fuのsessionについて

config/memcached.yml の sessions: を true にすると、セッション作成時にエラーになってしまう。

"1424"
=> Booting Mongrel
=> Rails 2.3.10 application starting on http://0.0.0.0:3002
=> Call with -d to detach
=> Ctrl-C to shutdown server
Wed Jan 12 01:10:08 +0900 2011: Read error: #<RuntimeError: #<ActionController::Session::MemCacheStore:0x2895468> unable to find server during initialization.>
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.10/lib/action_controller/session/mem_cache_store.rb:21:in `initialize'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.10/lib/action_controller/middleware_stack.rb:72:in `new'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.10/lib/action_controller/middleware_stack.rb:72:in `build'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.10/lib/action_controller/middleware_stack.rb:116:in `build'
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.10/lib/active_support/inflector.rb:364:in `inject'
・・・

環境の所為なのか、どうなのか分からないが、cache_fuのsessionは使えない模様。


ただ、config/memcached.yml の sessions: を false にして、別にセッションの保管先をmemcachedに指定することで、セッションをmemcachedに保管することは可能。

以下、確認した時の session_store.rb と cache_fu の設定


config/initializers/session_store.rb

config = YAML.load(ERB.new(IO.read(File.dirname(__FILE__) + "/../memcached.yml")).result)

ActionController::Base.session = {
  :memcache_server => config["defaults"]["servers"],
  :expire_after => config["defaults"]["session_ttl"],
  :namespace => config["defaults"]["namespace"] + "-#{ENV['RAILS_ENV']}",
  :key => "_app2_session",
  :secret => "789db92e8a6162f2185368ab0a1e5d8f2761bb25ebfb88e0b3e7ae4fe803d42981d32d96ca544aeb178e840f00f477c83bd370ff7d11f3de3f232052b59c83ce"
}

ActionController::Base.session_store = :mem_cache_store

config/memcached.yml

defaults:
  ttl: 1800
  readonly: false
  urlencode: false
  c_threshold: 10000
  compression: true
  debug: false
  namespace: app2
  sessions: false
  session_servers: false
  fragments: true
  memory: 640
  servers:
    - 192.168.24.111:11211
    - 192.168.24.122:11211
  benchmarking: false
  raise_errors: false
  fast_hash: false
  fastest_hash: false
  session_ttl: 86400