2007-08-27 railsのセッション管理でmemcachedを利用
■railsのセッション管理でmemcachedを利用
1. memcachedのインストールと環境設定
% yum -y install memcached % /etc/init.d/memcached start % chkconfig memcached on #memcachedのデフォルトポート(11211)を開ける % vi /etc/init.d/iptables % /etc/init.d/iptables restart
% gem install memcache-client
3. railsのmemcachedの設定。environment.rb
# Be sure to restart your web server when you modify this file.
...
ENV['RAILS_ENV'] ||= 'production'
...
# memcacheの準備
require 'memcache'
memcache_options = {
:compression => false, :debug => false,
:namespace => "foo-#{ENV['RAILS_ENV']}",
:readonly => false, :urlencode => false
}
memcache_servers = [ '192.168.0.2:11211' ]
...
Rails::Initializer.run do |config|
...
config.action_controller.session_store = :mem_cache_store
...
# fragment_cacheのときに必要?
config.action_controller.fragment_cache_store = :mem_cache_store, memcache_servers, memcache_options
...
end
...
CACHE = MemCache.new(memcache_options)
CACHE.servers = memcache_servers
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.merge!({ 'cache' => CACHE })
4. 本当にmemcachedにセッション情報が格納されるか?
無事格納されていました。
% /etc/init.d/memcached stop % memcached -vv -u nobody <24 get foo-production:session:eedbdaf0f0981cc82eb774fcd3722294 >24 sending key foo-production:session:eedbdaf0f0981cc82eb774fcd3722294 >24 END <24 set foo-production:session:eedbdaf0f0981cc82eb774fcd3722294 1 0 107 >24 STORED