Permalink
Browse files

First pass at Rack::Cache

  • Loading branch information...
1 parent 9cd094b commit 254ab7d916e579b9300951f5f33d3c5d3ee755a2 @wycats wycats committed with Carlhuda Sep 13, 2010
@@ -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
@@ -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
@@ -9,6 +9,7 @@ class Railtie < Rails::Railtie
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
@@ -147,6 +147,9 @@ def config
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
@@ -19,6 +19,7 @@ def app
boot!
assert_equal [
+ "Rack::Cache",
"ActionDispatch::Static",
"Rack::Lock",
"ActiveSupport::Cache::Strategy::LocalCache",
@@ -81,24 +82,24 @@ def app
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

0 comments on commit 254ab7d

Please sign in to comment.