markbates-rack_page_caching 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +3 -0
- data/lib/rack_page_caching/action_controller_base.rb +49 -0
- data/lib/rack_page_caching/rack_page_caching.rb +56 -0
- data/lib/rack_page_caching.rb +7 -0
- metadata +75 -0
data/README
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
unless ActionController.const_defined?('CONFIGURED_PAGE_CACHING_EXTENSIONS')
|
2
|
+
module ActionController
|
3
|
+
class Base
|
4
|
+
|
5
|
+
class << self
|
6
|
+
alias_method :__caches_page, :caches_page
|
7
|
+
|
8
|
+
def caches_page(*actions)
|
9
|
+
before_filter :set_rack_page_caching_header, :only => actions
|
10
|
+
|
11
|
+
RAILS_GEM_VERSION.match(/^(\d\.\d)/)
|
12
|
+
unless $1 >= '2.3'
|
13
|
+
before_filter :read_from_rack_page_cache, :only => actions
|
14
|
+
after_filter :write_into_rack_page_cache, :only => actions
|
15
|
+
end
|
16
|
+
__caches_page(*actions)
|
17
|
+
end
|
18
|
+
|
19
|
+
end # class << self
|
20
|
+
|
21
|
+
private
|
22
|
+
def read_from_rack_page_cache
|
23
|
+
puts "configatron.rack.caching.use_page_caching: #{configatron.rack.caching.use_page_caching}"
|
24
|
+
if request.get? && configatron.rack.caching.use_page_caching
|
25
|
+
cpage = Cachetastic::Caches::PageCache.get(request.url)
|
26
|
+
if cpage
|
27
|
+
response.headers['cache_this_page'] = 'false'
|
28
|
+
response.headers["Content-Type"] = cpage.content_type
|
29
|
+
render(:text => cpage.body, :layout => false)
|
30
|
+
return false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
return true
|
34
|
+
end
|
35
|
+
|
36
|
+
def write_into_rack_page_cache
|
37
|
+
if configatron.rack.caching.use_page_caching && request.get? && response.headers['cache_this_page'] == 'true' && response.status.match(/OK$/)
|
38
|
+
Cachetastic::Caches::PageCache.set(request.url, Rack::PageCaching::Middleware::Page.new(response.body, response.headers["Content-Type"]))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def set_rack_page_caching_header
|
43
|
+
response.headers['cache_this_page'] = 'true'
|
44
|
+
end
|
45
|
+
|
46
|
+
end # Base
|
47
|
+
CONFIGURED_PAGE_CACHING_EXTENSIONS = true
|
48
|
+
end # ActionController
|
49
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Rack
|
2
|
+
module PageCaching
|
3
|
+
class Middleware
|
4
|
+
|
5
|
+
def initialize(app) # :nodoc:
|
6
|
+
@app = app
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env) # :nodoc:
|
10
|
+
if configatron.rack.caching.use_page_caching
|
11
|
+
request = Rack::Request.new(env)
|
12
|
+
if request.get?
|
13
|
+
page = Cachetastic::Caches::PageCache.get(request.fullpath)
|
14
|
+
if page
|
15
|
+
response = Rack::Response.new
|
16
|
+
response["Content-Type"] = page.content_type
|
17
|
+
response.write(page.body)
|
18
|
+
return response.finish
|
19
|
+
end
|
20
|
+
end
|
21
|
+
ret = @app.call(env)
|
22
|
+
unless ret[2].is_a?(Rack::File)
|
23
|
+
res = ret[2]
|
24
|
+
if res["cache_this_page"] && res.successful? && request.get?
|
25
|
+
Cachetastic::Caches::PageCache.set(request.fullpath, Rack::PageCaching::Middleware::Page.new(res.body, res["Content-Type"]))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
return ret
|
29
|
+
end
|
30
|
+
return @app.call(env)
|
31
|
+
end
|
32
|
+
|
33
|
+
class Page # :nodoc:
|
34
|
+
|
35
|
+
attr_reader :body
|
36
|
+
attr_reader :content_type
|
37
|
+
|
38
|
+
def initialize(body, content_type = "text/html")
|
39
|
+
if body.is_a?(Array)
|
40
|
+
raise ArgumentError.new("Multipart pages can not be cached!") if body.size > 1
|
41
|
+
@body = body.first
|
42
|
+
else
|
43
|
+
@body = body
|
44
|
+
end
|
45
|
+
@content_type = content_type
|
46
|
+
end
|
47
|
+
|
48
|
+
def to_s
|
49
|
+
@body
|
50
|
+
end
|
51
|
+
|
52
|
+
end # Page
|
53
|
+
|
54
|
+
end # Middleware
|
55
|
+
end # PageCaching
|
56
|
+
end # Rack
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: markbates-rack_page_caching
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- markbates
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-02 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: cachetastic
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: configatron
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: "rack_page_caching was developed by: markbates"
|
36
|
+
email: ""
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README
|
43
|
+
files:
|
44
|
+
- lib/rack_page_caching/action_controller_base.rb
|
45
|
+
- lib/rack_page_caching/rack_page_caching.rb
|
46
|
+
- lib/rack_page_caching.rb
|
47
|
+
- README
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: ""
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
version:
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project: rack_page_caching
|
70
|
+
rubygems_version: 1.2.0
|
71
|
+
signing_key:
|
72
|
+
specification_version: 2
|
73
|
+
summary: rack_page_caching
|
74
|
+
test_files: []
|
75
|
+
|