From 254ab7d916e579b9300951f5f33d3c5d3ee755a2 Mon Sep 17 00:00:00 2001 From: wycats Date: Mon, 13 Sep 2010 14:03:06 -0700 Subject: First pass at Rack::Cache --- actionpack/actionpack.gemspec | 20 ++++--- actionpack/lib/action_dispatch/http/rack_cache.rb | 67 +++++++++++++++++++++++ actionpack/lib/action_dispatch/railtie.rb | 1 + railties/lib/rails/application.rb | 3 + railties/test/application/middleware_test.rb | 9 +-- 5 files changed, 87 insertions(+), 13 deletions(-) create mode 100644 actionpack/lib/action_dispatch/http/rack_cache.rb diff --git a/actionpack/actionpack.gemspec b/actionpack/actionpack.gemspec index 5b219540f4..75ac47105d 100644 --- a/actionpack/actionpack.gemspec +++ b/actionpack/actionpack.gemspec @@ -19,13 +19,15 @@ Gem::Specification.new do |s| s.has_rdoc = true - s.add_dependency('activesupport', version) - s.add_dependency('activemodel', version) - s.add_dependency('builder', '~> 2.1.2') - s.add_dependency('i18n', '~> 0.4.1') - s.add_dependency('rack', '~> 1.2.1') - s.add_dependency('rack-test', '~> 0.5.4') - s.add_dependency('rack-mount', '~> 0.6.13') - s.add_dependency('tzinfo', '~> 0.3.23') - s.add_dependency('erubis', '~> 2.6.6') + s.add_dependency('activesupport', version) + s.add_dependency('activemodel', version) + s.add_dependency('rack-cache', '~> 0.5.2') + s.add_dependency('rack-cache-purge', '~> 0.0.1') + s.add_dependency('builder', '~> 2.1.2') + s.add_dependency('i18n', '~> 0.4.1') + s.add_dependency('rack', '~> 1.2.1') + s.add_dependency('rack-test', '~> 0.5.4') + s.add_dependency('rack-mount', '~> 0.6.13') + s.add_dependency('tzinfo', '~> 0.3.23') + s.add_dependency('erubis', '~> 2.6.6') end diff --git a/actionpack/lib/action_dispatch/http/rack_cache.rb b/actionpack/lib/action_dispatch/http/rack_cache.rb new file mode 100644 index 0000000000..e5914abc81 --- /dev/null +++ b/actionpack/lib/action_dispatch/http/rack_cache.rb @@ -0,0 +1,67 @@ +require "rack/cache" +require "rack/cache/context" +require "active_support/cache" + +module ActionDispatch + class RailsMetaStore < Rack::Cache::MetaStore + def self.resolve(uri) + new + end + + # TODO: Finally deal with the RAILS_CACHE global + def initialize(store = RAILS_CACHE) + @store = store + end + + def read(key) + @store.read(key) || [] + end + + def write(key, value) + @store.write(key, value) + end + + def purge(key) + @store.delete(key) + nil + end + + ::Rack::Cache::MetaStore::RAILS = self + end + + class RailsEntityStore < Rack::Cache::EntityStore + def self.resolve(uri) + new + end + + def initialize(store = RAILS_CACHE) + @store = store + end + + def exist?(key) + @store.exist?(key) + end + + def open(key) + @store.read(key) + end + + def read(key) + body = open(key) + body.join if body + end + + def write(body) + buf = [] + key, size = slurp(body) { |part| buf << part } + @store.write(key, buf) + [key, size] + end + + def purge(key) + @store.delete(key) + end + + ::Rack::Cache::EntityStore::RAILS = self + end +end diff --git a/actionpack/lib/action_dispatch/railtie.rb b/actionpack/lib/action_dispatch/railtie.rb index 69537be876..0a3bd5fe40 100644 --- a/actionpack/lib/action_dispatch/railtie.rb +++ b/actionpack/lib/action_dispatch/railtie.rb @@ -9,6 +9,7 @@ module ActionDispatch config.action_dispatch.show_exceptions = true config.action_dispatch.best_standards_support = true config.action_dispatch.tld_length = 1 + config.action_dispatch.rack_cache = {:metastore => "rails:/", :entitystore => "rails:/", :verbose => true} initializer "action_dispatch.configure" do |app| ActionDispatch::Http::URL.tld_length = app.config.action_dispatch.tld_length diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 8631a5df3e..b923fedab7 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -147,6 +147,9 @@ module Rails def default_middleware_stack ActionDispatch::MiddlewareStack.new.tap do |middleware| + require "action_dispatch/http/rack_cache" if config.action_dispatch.rack_cache + + middleware.use ::Rack::Cache, config.action_dispatch.rack_cache if config.action_dispatch.rack_cache middleware.use ::ActionDispatch::Static, config.static_asset_paths if config.serve_static_assets middleware.use ::Rack::Lock if !config.allow_concurrency middleware.use ::Rack::Runtime diff --git a/railties/test/application/middleware_test.rb b/railties/test/application/middleware_test.rb index ed8f70dc44..0ce6d482a0 100644 --- a/railties/test/application/middleware_test.rb +++ b/railties/test/application/middleware_test.rb @@ -19,6 +19,7 @@ module ApplicationTests boot! assert_equal [ + "Rack::Cache", "ActionDispatch::Static", "Rack::Lock", "ActiveSupport::Cache::Strategy::LocalCache", @@ -81,24 +82,24 @@ module ApplicationTests test "insert middleware after" do add_to_config "config.middleware.insert_after ActionDispatch::Static, Rack::Config" boot! - assert_equal "Rack::Config", middleware.second + assert_equal "Rack::Config", middleware.third end test "RAILS_CACHE does not respond to middleware" do add_to_config "config.cache_store = :memory_store" boot! - assert_equal "Rack::Runtime", middleware.third + assert_equal "Rack::Runtime", middleware.fourth end test "RAILS_CACHE does respond to middleware" do boot! - assert_equal "Rack::Runtime", middleware.fourth + assert_equal "Rack::Runtime", middleware.fifth end test "insert middleware before" do add_to_config "config.middleware.insert_before ActionDispatch::Static, Rack::Config" boot! - assert_equal "Rack::Config", middleware.first + assert_equal "Rack::Config", middleware.second end # x_sendfile_header middleware -- cgit v1.2.3