diff options
author | Joshua Peek <josh@joshpeek.com> | 2008-12-22 11:31:18 -0600 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2008-12-22 11:31:18 -0600 |
commit | aa002c0e86afdc83693f14667a710107843f0fbd (patch) | |
tree | 0ec063c6a18b2f9f523ada8a5d338b68dd562be2 | |
parent | 63aac338332a06d3c9e28dde7954679703ec7620 (diff) | |
download | rails-aa002c0e86afdc83693f14667a710107843f0fbd.tar.gz rails-aa002c0e86afdc83693f14667a710107843f0fbd.tar.bz2 rails-aa002c0e86afdc83693f14667a710107843f0fbd.zip |
ActiveRecord::QueryCache middleware
-rw-r--r-- | actionpack/lib/action_controller/caching.rb | 3 | ||||
-rw-r--r-- | actionpack/lib/action_controller/caching/sql_cache.rb | 18 | ||||
-rw-r--r-- | actionpack/lib/action_controller/dispatcher.rb | 1 | ||||
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/query_cache.rb | 38 |
5 files changed, 28 insertions, 34 deletions
diff --git a/actionpack/lib/action_controller/caching.rb b/actionpack/lib/action_controller/caching.rb index b4d251eb3c..1d14df0052 100644 --- a/actionpack/lib/action_controller/caching.rb +++ b/actionpack/lib/action_controller/caching.rb @@ -27,7 +27,6 @@ module ActionController #:nodoc: autoload :Actions, 'action_controller/caching/actions' autoload :Fragments, 'action_controller/caching/fragments' autoload :Pages, 'action_controller/caching/pages' - autoload :SqlCache, 'action_controller/caching/sql_cache' autoload :Sweeping, 'action_controller/caching/sweeping' def self.included(base) #:nodoc: @@ -41,7 +40,7 @@ module ActionController #:nodoc: end include Pages, Actions, Fragments - include Sweeping, SqlCache if defined?(ActiveRecord) + include Sweeping if defined?(ActiveRecord) @@perform_caching = true cattr_accessor :perform_caching diff --git a/actionpack/lib/action_controller/caching/sql_cache.rb b/actionpack/lib/action_controller/caching/sql_cache.rb deleted file mode 100644 index 139be6100d..0000000000 --- a/actionpack/lib/action_controller/caching/sql_cache.rb +++ /dev/null @@ -1,18 +0,0 @@ -module ActionController #:nodoc: - module Caching - module SqlCache - def self.included(base) #:nodoc: - if defined?(ActiveRecord) && ActiveRecord::Base.respond_to?(:cache) - base.alias_method_chain :perform_action, :caching - end - end - - protected - def perform_action_with_caching - ActiveRecord::Base.cache do - perform_action_without_caching - end - end - end - end -end
\ No newline at end of file diff --git a/actionpack/lib/action_controller/dispatcher.rb b/actionpack/lib/action_controller/dispatcher.rb index e1eaaf7cbb..0cfd451c04 100644 --- a/actionpack/lib/action_controller/dispatcher.rb +++ b/actionpack/lib/action_controller/dispatcher.rb @@ -48,6 +48,7 @@ module ActionController !ActionController::Base.allow_concurrency } middleware.use "ActionController::Failsafe" + middleware.use "ActiveRecord::QueryCache" if defined?(ActiveRecord) ["ActionController::Session::CookieStore", "ActionController::Session::MemCacheStore", diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 9db0366c46..9746a46d47 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -3015,7 +3015,7 @@ module ActiveRecord #:nodoc: end Base.class_eval do - extend QueryCache + extend QueryCache::ClassMethods include Validations include Locking::Optimistic, Locking::Pessimistic include AttributeMethods diff --git a/activerecord/lib/active_record/query_cache.rb b/activerecord/lib/active_record/query_cache.rb index a8af89fcb9..eb92bc2545 100644 --- a/activerecord/lib/active_record/query_cache.rb +++ b/activerecord/lib/active_record/query_cache.rb @@ -1,20 +1,32 @@ module ActiveRecord - module QueryCache - # Enable the query cache within the block if Active Record is configured. - def cache(&block) - if ActiveRecord::Base.configurations.blank? - yield - else - connection.cache(&block) + class QueryCache + module ClassMethods + # Enable the query cache within the block if Active Record is configured. + def cache(&block) + if ActiveRecord::Base.configurations.blank? + yield + else + connection.cache(&block) + end end + + # Disable the query cache within the block if Active Record is configured. + def uncached(&block) + if ActiveRecord::Base.configurations.blank? + yield + else + connection.uncached(&block) + end + end + end + + def initialize(app) + @app = app end - # Disable the query cache within the block if Active Record is configured. - def uncached(&block) - if ActiveRecord::Base.configurations.blank? - yield - else - connection.uncached(&block) + def call(env) + ActiveRecord::Base.cache do + @app.call(env) end end end |