Ruby on Rails memcached Session Storage 实践

豆豆网   技术应用频道   2008年01月18日  【字号: 收藏本文

本文详细介绍Ruby on Rails memcached Session Storage 实践

  一、安装 memcached

  到这里下载安装并启动(Debian 上我使用的是memcached-1.1.13.tar.gz):

  ./memcached -d -u root -m 10 -l 192.168.0.249 -p 11211

  二、安装 memcache-client 和 cached_model , 执行下面的命令或到这里下载安装:

  gem install cached_model –include-dependencies

  三、配置 Rails App 使用 memcached Session Storage

  1. 在 environments.rb 文件后加入以下代码:

  require ‘memcache’
  require ‘memcache_util’
  # memcache defaults, environments may override these settings
  unless defined? MEMCACHE_OPTIONS then
  MEMCACHE_OPTIONS = {
  :debug => false,
  :namespace => ‘my_memcache’,
  :readonly => false
  }
  end
  # memcache configuration
  unless defined? MEMCACHE_CONFIG then
  File.open “#{RAILS_ROOT}/config/memcache.yml” do |memcache|
  MEMCACHE_CONFIG = YAML::load memcache
  end
  end
  # Connect to memcache
  unless defined? CACHE then
  CACHE = MemCache.new MEMCACHE_OPTIONS
  CACHE.servers = MEMCACHE_CONFIG[RAILS_ENV]
  end
  # Configure the session manager to use memcache data store
  ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.update(
  :database_manager => CGI::Session::MemCacheStore,
  :cache => CACHE, :expires => 3600 * 12)
  2.memcache.yml 文件内容:
  production:
  - 192.168.0.249:11211
  development:
  - 192.168.0.249:11211
  benchmarking:
  - 192.168.0.249:11211

  四、使用lighttpd + mod_proxy + Mongrel 实现 Scale , 如果安装Mongrel请看我的前一篇Blog:使用Mongrel替代scgi .

  1. 实现目标: http://mongrel.rubyforge.org/docs/lighttpd.html

Ruby on Rails memcached Session Storage 实践

  2.Lighttpd 配置

  server.modules = ( "mod_rewrite", "mod_redirect",
  “mod_access”, “mod_accesslog”, “mod_compress”,
  “mod_proxy”)
  $HTTP[”url”] =~ “^/myapp1/” {
  proxy.balance = “fair”
  proxy.server = (”" => (
  (”host” => “127.0.0.1″,”port” => 4000),
  (”host” => “192.168.0.60″,”port” => 3000)
  ))
  }
  $HTTP[”url”] =~ “^/myapp2/” {
  proxy.palance = “fair”
  proxy.server = (”" => (
  (”host” => “127.0.0.1″,”port” => 4001),
  (”host” => “192.168.0.60″,”port” => 3001)
  ))
  }

责编:豆豆技术应用

正在加载评论...