Skip to content

Commit 254ab7d

Browse files
wycatsCarlhuda
authored and
Carlhuda
committed
First pass at Rack::Cache
1 parent 9cd094b commit 254ab7d

File tree

5 files changed

+87
-13
lines changed

5 files changed

+87
-13
lines changed

actionpack/actionpack.gemspec

+11-9
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ Gem::Specification.new do |s|
1919

2020
s.has_rdoc = true
2121

22-
s.add_dependency('activesupport', version)
23-
s.add_dependency('activemodel', version)
24-
s.add_dependency('builder', '~> 2.1.2')
25-
s.add_dependency('i18n', '~> 0.4.1')
26-
s.add_dependency('rack', '~> 1.2.1')
27-
s.add_dependency('rack-test', '~> 0.5.4')
28-
s.add_dependency('rack-mount', '~> 0.6.13')
29-
s.add_dependency('tzinfo', '~> 0.3.23')
30-
s.add_dependency('erubis', '~> 2.6.6')
22+
s.add_dependency('activesupport', version)
23+
s.add_dependency('activemodel', version)
24+
s.add_dependency('rack-cache', '~> 0.5.2')
25+
s.add_dependency('rack-cache-purge', '~> 0.0.1')
26+
s.add_dependency('builder', '~> 2.1.2')
27+
s.add_dependency('i18n', '~> 0.4.1')
28+
s.add_dependency('rack', '~> 1.2.1')
29+
s.add_dependency('rack-test', '~> 0.5.4')
30+
s.add_dependency('rack-mount', '~> 0.6.13')
31+
s.add_dependency('tzinfo', '~> 0.3.23')
32+
s.add_dependency('erubis', '~> 2.6.6')
3133
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require "rack/cache"
2+
require "rack/cache/context"
3+
require "active_support/cache"
4+
5+
module ActionDispatch
6+
class RailsMetaStore < Rack::Cache::MetaStore
7+
def self.resolve(uri)
8+
new
9+
end
10+
11+
# TODO: Finally deal with the RAILS_CACHE global
12+
def initialize(store = RAILS_CACHE)
13+
@store = store
14+
end
15+
16+
def read(key)
17+
@store.read(key) || []
18+
end
19+
20+
def write(key, value)
21+
@store.write(key, value)
22+
end
23+
24+
def purge(key)
25+
@store.delete(key)
26+
nil
27+
end
28+
29+
::Rack::Cache::MetaStore::RAILS = self
30+
end
31+
32+
class RailsEntityStore < Rack::Cache::EntityStore
33+
def self.resolve(uri)
34+
new
35+
end
36+
37+
def initialize(store = RAILS_CACHE)
38+
@store = store
39+
end
40+
41+
def exist?(key)
42+
@store.exist?(key)
43+
end
44+
45+
def open(key)
46+
@store.read(key)
47+
end
48+
49+
def read(key)
50+
body = open(key)
51+
body.join if body
52+
end
53+
54+
def write(body)
55+
buf = []
56+
key, size = slurp(body) { |part| buf << part }
57+
@store.write(key, buf)
58+
[key, size]
59+
end
60+
61+
def purge(key)
62+
@store.delete(key)
63+
end
64+
65+
::Rack::Cache::EntityStore::RAILS = self
66+
end
67+
end

actionpack/lib/action_dispatch/railtie.rb

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Railtie < Rails::Railtie
99
config.action_dispatch.show_exceptions = true
1010
config.action_dispatch.best_standards_support = true
1111
config.action_dispatch.tld_length = 1
12+
config.action_dispatch.rack_cache = {:metastore => "rails:/", :entitystore => "rails:/", :verbose => true}
1213

1314
initializer "action_dispatch.configure" do |app|
1415
ActionDispatch::Http::URL.tld_length = app.config.action_dispatch.tld_length

railties/lib/rails/application.rb

+3
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ def config
147147

148148
def default_middleware_stack
149149
ActionDispatch::MiddlewareStack.new.tap do |middleware|
150+
require "action_dispatch/http/rack_cache" if config.action_dispatch.rack_cache
151+
152+
middleware.use ::Rack::Cache, config.action_dispatch.rack_cache if config.action_dispatch.rack_cache
150153
middleware.use ::ActionDispatch::Static, config.static_asset_paths if config.serve_static_assets
151154
middleware.use ::Rack::Lock if !config.allow_concurrency
152155
middleware.use ::Rack::Runtime

railties/test/application/middleware_test.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def app
1919
boot!
2020

2121
assert_equal [
22+
"Rack::Cache",
2223
"ActionDispatch::Static",
2324
"Rack::Lock",
2425
"ActiveSupport::Cache::Strategy::LocalCache",
@@ -81,24 +82,24 @@ def app
8182
test "insert middleware after" do
8283
add_to_config "config.middleware.insert_after ActionDispatch::Static, Rack::Config"
8384
boot!
84-
assert_equal "Rack::Config", middleware.second
85+
assert_equal "Rack::Config", middleware.third
8586
end
8687

8788
test "RAILS_CACHE does not respond to middleware" do
8889
add_to_config "config.cache_store = :memory_store"
8990
boot!
90-
assert_equal "Rack::Runtime", middleware.third
91+
assert_equal "Rack::Runtime", middleware.fourth
9192
end
9293

9394
test "RAILS_CACHE does respond to middleware" do
9495
boot!
95-
assert_equal "Rack::Runtime", middleware.fourth
96+
assert_equal "Rack::Runtime", middleware.fifth
9697
end
9798

9899
test "insert middleware before" do
99100
add_to_config "config.middleware.insert_before ActionDispatch::Static, Rack::Config"
100101
boot!
101-
assert_equal "Rack::Config", middleware.first
102+
assert_equal "Rack::Config", middleware.second
102103
end
103104

104105
# x_sendfile_header middleware

0 commit comments

Comments
 (0)