mirror of
https://github.com/github/rails.git
synced 2026-01-26 14:58:11 -05:00
Added easy assignment of fragment cache store through use of symbols for included stores (old way still works too)
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2230 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -1,5 +1,14 @@
|
||||
*SVN*
|
||||
|
||||
* Added easy assignment of fragment cache store through use of symbols for included stores (old way still works too)
|
||||
|
||||
Before:
|
||||
ActionController::Base.fragment_cache_store =
|
||||
ActionController::Base::Caching::Fragments::FileStore.new("/path/to/cache/directory")
|
||||
|
||||
After:
|
||||
ActionController::Base.fragment_cache_store = :file_store, "/path/to/cache/directory"
|
||||
|
||||
* Added ActionController::Base.session_store=, session_store, and session_options to make it easier to tweak the session options (instead of going straight to ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS)
|
||||
|
||||
* Added TextHelper#cycle to cycle over an array of values on each hit (useful for alternating row colors etc) #2154 [dave-ml@dribin.org]
|
||||
|
||||
@@ -38,7 +38,6 @@ require 'action_controller/benchmarking'
|
||||
require 'action_controller/filters'
|
||||
require 'action_controller/layout'
|
||||
require 'action_controller/flash'
|
||||
require 'action_controller/session'
|
||||
require 'action_controller/dependencies'
|
||||
require 'action_controller/pagination'
|
||||
require 'action_controller/scaffolding'
|
||||
@@ -67,7 +66,6 @@ ActionController::Base.class_eval do
|
||||
include ActionController::Scaffolding
|
||||
include ActionController::Helpers
|
||||
include ActionController::Cookies
|
||||
include ActionController::Session
|
||||
include ActionController::Caching
|
||||
include ActionController::Components
|
||||
include ActionController::Verification
|
||||
|
||||
@@ -240,23 +240,28 @@ module ActionController #:nodoc:
|
||||
#
|
||||
# Configuration examples (MemoryStore is the default):
|
||||
#
|
||||
# ActionController::Base.fragment_cache_store =
|
||||
# ActionController::Caching::Fragments::MemoryStore.new
|
||||
#
|
||||
# ActionController::Base.fragment_cache_store =
|
||||
# ActionController::Caching::Fragments::FileStore.new("/path/to/cache/directory")
|
||||
#
|
||||
# ActionController::Base.fragment_cache_store =
|
||||
# ActionController::Caching::Fragments::DRbStore.new("druby://localhost:9192")
|
||||
#
|
||||
# ActionController::Base.fragment_cache_store =
|
||||
# ActionController::Caching::Fragments::MemCacheStore.new("localhost")
|
||||
# ActionController::Base.fragment_cache_store = :memory_store
|
||||
# ActionController::Base.fragment_cache_store = :file_store, "/path/to/cache/directory"
|
||||
# ActionController::Base.fragment_cache_store = :drb_store, "druby://localhost:9192"
|
||||
# ActionController::Base.fragment_cache_store = :mem_cache_store, "localhost"
|
||||
# ActionController::Base.fragment_cache_store = MyOwnStore.new("parameter")
|
||||
module Fragments
|
||||
def self.append_features(base) #:nodoc:
|
||||
super
|
||||
base.class_eval do
|
||||
@@fragment_cache_store = MemoryStore.new
|
||||
cattr_accessor :fragment_cache_store
|
||||
cattr_reader :fragment_cache_store
|
||||
|
||||
def self.fragment_cache_store=(store_option)
|
||||
store, *parameters = *([ store_option ].flatten)
|
||||
@@fragment_cache_store = if store.is_a?(Symbol)
|
||||
store_class_name = (store == :drb_store ? "DRbStore" : store.to_s.camelize)
|
||||
store_class = ActionController::Caching::Fragments.const_get(store_class_name)
|
||||
parameters.empty? ? store.new : store_class.new(*parameters)
|
||||
else
|
||||
store
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -349,18 +354,26 @@ module ActionController #:nodoc:
|
||||
end
|
||||
|
||||
class DRbStore < MemoryStore #:nodoc:
|
||||
attr_reader :address
|
||||
|
||||
def initialize(address = 'druby://localhost:9192')
|
||||
@address = address
|
||||
@data, @mutex = DRbObject.new(nil, address), Mutex.new
|
||||
end
|
||||
end
|
||||
|
||||
class MemCacheStore < MemoryStore #:nodoc:
|
||||
attr_reader :address
|
||||
|
||||
def initialize(address = 'localhost')
|
||||
@address = address
|
||||
@data, @mutex = MemCache.new(address), Mutex.new
|
||||
end
|
||||
end
|
||||
|
||||
class FileStore #:nodoc:
|
||||
attr_reader :cache_path
|
||||
|
||||
def initialize(cache_path)
|
||||
@cache_path = cache_path
|
||||
end
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
require 'action_controller/cgi_ext/cgi_ext'
|
||||
require 'action_controller/cgi_ext/cookie_performance_fix'
|
||||
require 'action_controller/cgi_ext/raw_post_data_fix'
|
||||
require 'action_controller/session/drb_store'
|
||||
require 'action_controller/session/mem_cache_store'
|
||||
if Object.const_defined?(:ActiveRecord)
|
||||
require 'action_controller/session/active_record_store'
|
||||
end
|
||||
|
||||
module ActionController #:nodoc:
|
||||
class Base
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
module ActionController #:nodoc:
|
||||
module Session #:nodoc:
|
||||
def self.append_features(base) #:nodoc:
|
||||
super #:nodoc:
|
||||
base.after_filter(:clear_persistant_model_associations)
|
||||
end
|
||||
|
||||
private
|
||||
def clear_persistant_model_associations #:doc:
|
||||
session = @session.instance_variable_get("@data")
|
||||
session.each { |key, obj| obj.clear_association_cache if obj.respond_to?(:clear_association_cache) } if session
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,12 +1,17 @@
|
||||
require 'action_controller/session/drb_store'
|
||||
require 'action_controller/session/mem_cache_store'
|
||||
if Object.const_defined?(:ActiveRecord)
|
||||
require 'action_controller/session/active_record_store'
|
||||
end
|
||||
|
||||
module ActionController #:nodoc:
|
||||
module SessionManagement #:nodoc:
|
||||
def self.append_features(base)
|
||||
super
|
||||
base.extend(ClassMethods)
|
||||
base.class_eval do
|
||||
alias_method :process_without_session_management_support, :process
|
||||
alias_method :process, :process_with_session_management_support
|
||||
end
|
||||
base.send(:alias_method, :process_without_session_management_support, :process)
|
||||
base.send(:alias_method, :process, :process_with_session_management_support)
|
||||
base.after_filter(:clear_persistant_model_associations)
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
@@ -15,7 +20,7 @@ module ActionController #:nodoc:
|
||||
# :mem_cache_store, or :memory_store) or use your own class.
|
||||
def session_store=(store)
|
||||
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS[:database_manager] =
|
||||
store.is_a?(Symbol) ? CGI::Session.const_get(store.to_s.camelize) : store
|
||||
store.is_a?(Symbol) ? CGI::Session.const_get(store == :drb_store ? "DRbStore" : store.to_s.camelize) : store
|
||||
end
|
||||
|
||||
# Returns the session store class currently used.
|
||||
@@ -100,5 +105,11 @@ module ActionController #:nodoc:
|
||||
request.session_options = self.class.session_options_for(request, action)
|
||||
process_without_session_management_support(request, response, method, *arguments)
|
||||
end
|
||||
|
||||
private
|
||||
def clear_persistant_model_associations #:doc:
|
||||
session = @session.instance_variable_get("@data")
|
||||
session.each { |key, obj| obj.clear_association_cache if obj.respond_to?(:clear_association_cache) } if session
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
45
actionpack/test/controller/fragment_store_setting_test.rb
Normal file
45
actionpack/test/controller/fragment_store_setting_test.rb
Normal file
@@ -0,0 +1,45 @@
|
||||
require File.dirname(__FILE__) + '/../abstract_unit'
|
||||
|
||||
MemCache = Struct.new(:MemCache, :address)
|
||||
|
||||
class FragmentCacheStoreSettingTest < Test::Unit::TestCase
|
||||
def teardown
|
||||
ActionController::Base.fragment_cache_store = ActionController::Caching::Fragments::MemoryStore.new
|
||||
end
|
||||
|
||||
def test_file_fragment_cache_store
|
||||
ActionController::Base.fragment_cache_store = :file_store, "/path/to/cache/directory"
|
||||
assert_kind_of(
|
||||
ActionController::Caching::Fragments::FileStore,
|
||||
ActionController::Base.fragment_cache_store
|
||||
)
|
||||
assert_equal "/path/to/cache/directory", ActionController::Base.fragment_cache_store.cache_path
|
||||
end
|
||||
|
||||
def test_drb_fragment_cache_store
|
||||
ActionController::Base.fragment_cache_store = :drb_store, "druby://localhost:9192"
|
||||
assert_kind_of(
|
||||
ActionController::Caching::Fragments::DRbStore,
|
||||
ActionController::Base.fragment_cache_store
|
||||
)
|
||||
assert_equal "druby://localhost:9192", ActionController::Base.fragment_cache_store.address
|
||||
end
|
||||
|
||||
def test_mem_cache_fragment_cache_store
|
||||
ActionController::Base.fragment_cache_store = :mem_cache_store, "localhost"
|
||||
assert_kind_of(
|
||||
ActionController::Caching::Fragments::MemCacheStore,
|
||||
ActionController::Base.fragment_cache_store
|
||||
)
|
||||
assert_equal "localhost", ActionController::Base.fragment_cache_store.address
|
||||
end
|
||||
|
||||
def test_object_assigned_fragment_cache_store
|
||||
ActionController::Base.fragment_cache_store = ActionController::Caching::Fragments::FileStore.new("/path/to/cache/directory")
|
||||
assert_kind_of(
|
||||
ActionController::Caching::Fragments::FileStore,
|
||||
ActionController::Base.fragment_cache_store
|
||||
)
|
||||
assert_equal "/path/to/cache/directory", ActionController::Base.fragment_cache_store.cache_path
|
||||
end
|
||||
end
|
||||
@@ -81,4 +81,14 @@ class SessionManagementTest < Test::Unit::TestCase
|
||||
get :conditional, :ws => "ws"
|
||||
assert_equal false, @request.session_options
|
||||
end
|
||||
|
||||
def test_session_store_setting
|
||||
ActionController::Base.session_store = :drb_store
|
||||
assert_equal CGI::Session::DRbStore, ActionController::Base.session_store
|
||||
|
||||
if Object.const_defined?(:ActiveRecord)
|
||||
ActionController::Base.session_store = :active_record_store
|
||||
assert_equal CGI::Session::ActiveRecordStore, ActionController::Base.session_store
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user