public
Created

Rails cache summary.

  • Download Gist
rails_cache.md
Markdown

Rails 2

  • Specify RAILS_CACHE directly in environment.rb, like: RAILS_CACHE = ActiveSupport::Cache.lookup_store :memory_store.
  • Use config.cache_store=, like: config.cache_store = :memory_store. In fact, it calls ActiveSupport::Cache.lookup_store.
  • Another example of cache_store usage: config.cache_store = [:mem_cache_store, 'memhost:11211', {:namespace => 'gimi', :autofix_keys => true}]
  • By default, ActionController::Base.cache_store will be set to RAILS_CACHE, but it can be changed by using ActionController::Base.cache_store=.

  • About session store: ActionController::Base.session= will set ActionController::Base.session_options. ActionController::Base.session_store= decides which store will be used.

Session

Configuration

-- environment files --

  • config.action_controller.session_store = [class, symbol]
  • config.action_controller.session_options #=> it's a hash

  • new in Rails 2

    • config.action_controller.session = [hash] # Rails 2

session_options: * :database_manager - Rails 1: PStore; Rails 2:Cookie * :session_path - default '/' * :cookie_only - default true,表示仅从cookie中获取session,而不能通过参数来得到。 * :prefix - default 'ruby_sess.'

* Rails 1 only
  * :session_id - 默认根据session_key来取(或生成新的,如果没有的话)
  * :new_session - 不设置,自动生成新的;true,强制生成新的;false,总不生成新的,这时,如果:session_id不设置,则会抛出异常。
  * :session_expires - session过期时间
  * :session_domain
  * :session_secure - true表示只能用https

* Rails 2 only
  * :httponly, :session_http_only - 默认:true,不能用js来获取cookie中的session信息
  * :domain
  * :secure
  * :key(:session_key)
  * :path(:session_path)

-- in controllers -- This method is deprecated since Rails 2, for sessions are now only generated when they are accessed.

  • session *args
  • session :off
  • session :only => 'foo', :if => -> {}

-- use memcached --

Rails 1

config.action_controller.session_store = :mem_cache_store ActionController::Base.session_options.update( :database_manager => :mem_cache_store, :cache => [an instance of Memcache client], :expires => 0, # 这个设了,session_expires应该就可以不用设了。 some other configs ...(:session_domain, etc.) )

Rails 2

config.action_controller.session_store = :mem_cache_store config.action_controller.session = { :cache => [an instance of Memcache client], :expire_after => 0, # 也可以通过:expires来设置 some other configs ... }

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.