1
1
require 'fileutils'
2
2
require 'active_support/core_ext/class/attribute_accessors'
3
3
4
- module ActionController #:nodoc:
4
+ module ActionController
5
5
module Caching
6
- # Page caching is an approach to caching where the entire action output of is stored as a HTML file that the web server
7
- # can serve without going through Action Pack. This is the fastest way to cache your content as opposed to going dynamically
8
- # through the process of generating the content. Unfortunately, this incredible speed-up is only available to stateless pages
9
- # where all visitors are treated the same. Content management systems -- including weblogs and wikis -- have many pages that are
10
- # a great fit for this approach, but account-based systems where people log in and manipulate their own data are often less
11
- # likely candidates.
6
+ # Page caching is an approach to caching where the entire action output of is
7
+ # stored as a HTML file that the web server can serve without going through
8
+ # Action Pack. This is the fastest way to cache your content as opposed to going
9
+ # dynamically through the process of generating the content. Unfortunately, this
10
+ # incredible speed-up is only available to stateless pages where all visitors are
11
+ # treated the same. Content management systems -- including weblogs and wikis --
12
+ # have many pages that are a great fit for this approach, but account-based systems
13
+ # where people log in and manipulate their own data are often less likely candidates.
12
14
#
13
- # Specifying which actions to cache is done through the <tt> caches_page</tt> class method:
15
+ # Specifying which actions to cache is done through the + caches_page+ class method:
14
16
#
15
17
# class WeblogController < ActionController::Base
16
18
# caches_page :show, :new
17
19
# end
18
20
#
19
- # This will generate cache files such as <tt>weblog/show/5.html</tt> and <tt>weblog/new.html</tt>, which match the URLs used
20
- # that would normally trigger dynamic page generation. Page caching works by configuring a web server to first check for the
21
- # existence of files on disk, and to serve them directly when found, without passing the request through to Action Pack.
22
- # This is much faster than handling the full dynamic request in the usual way.
21
+ # This will generate cache files such as <tt>weblog/show/5.html</tt> and
22
+ # <tt>weblog/new.html</tt>, which match the URLs used that would normally trigger
23
+ # dynamic page generation. Page caching works by configuring a web server to first
24
+ # check for the existence of files on disk, and to serve them directly when found,
25
+ # without passing the request through to Action Pack. This is much faster than
26
+ # handling the full dynamic request in the usual way.
23
27
#
24
- # Expiration of the cache is handled by deleting the cached file, which results in a lazy regeneration approach where the cache
25
- # is not restored before another hit is made against it. The API for doing so mimics the options from +url_for+ and friends:
28
+ # Expiration of the cache is handled by deleting the cached file, which results
29
+ # in a lazy regeneration approach where the cache is not restored before another
30
+ # hit is made against it. The API for doing so mimics the options from +url_for+ and friends:
26
31
#
27
32
# class WeblogController < ActionController::Base
28
33
# def update
29
34
# List.update(params[:list][:id], params[:list])
30
- # expire_page : action => " show", :id => params[:list][:id]
31
- # redirect_to : action => " show", :id => params[:list][:id]
35
+ # expire_page action: ' show', id: params[:list][:id]
36
+ # redirect_to action: ' show', id: params[:list][:id]
32
37
# end
33
38
# end
34
39
#
35
- # Additionally, you can expire caches using Sweepers that act on changes in the model to determine when a cache is supposed to be
36
- # expired.
40
+ # Additionally, you can expire caches using Sweepers that act on changes in
41
+ # the model to determine when a cache is supposed to be expired.
37
42
module Pages
38
43
extend ActiveSupport ::Concern
39
44
40
45
included do
41
- # The cache directory should be the document root for the web server and is set using <tt>Base.page_cache_directory = "/document/root"</tt>.
42
- # For Rails, this directory has already been set to Rails.public_path (which is usually set to <tt>Rails.root + "/public"</tt>). Changing
43
- # this setting can be useful to avoid naming conflicts with files in <tt>public/</tt>, but doing so will likely require configuring your
44
- # web server to look in the new location for cached files.
46
+ # The cache directory should be the document root for the web server and is
47
+ # set using <tt>Base.page_cache_directory = "/document/root"</tt>. For Rails,
48
+ # this directory has already been set to Rails.public_path (which is usually
49
+ # set to <tt>Rails.root + "/public"</tt>). Changing this setting can be useful
50
+ # to avoid naming conflicts with files in <tt>public/</tt>, but doing so will
51
+ # likely require configuring your web server to look in the new location for
52
+ # cached files.
45
53
class_attribute :page_cache_directory
46
54
self . page_cache_directory ||= ''
47
55
48
- # Most Rails requests do not have an extension, such as <tt>/weblog/new</tt>. In these cases, the page caching mechanism will add one in
49
- # order to make it easy for the cached files to be picked up properly by the web server. By default, this cache extension is <tt>.html</tt>.
50
- # If you want something else, like <tt>.php</tt> or <tt>.shtml</tt>, just set Base.page_cache_extension. In cases where a request already has an
51
- # extension, such as <tt>.xml</tt> or <tt>.rss</tt>, page caching will not add an extension. This allows it to work well with RESTful apps.
56
+ # Most Rails requests do not have an extension, such as <tt>/weblog/new</tt>.
57
+ # In these cases, the page caching mechanism will add one in order to make it
58
+ # easy for the cached files to be picked up properly by the web server. By
59
+ # default, this cache extension is <tt>.html</tt>. If you want something else,
60
+ # like <tt>.php</tt> or <tt>.shtml</tt>, just set Base.page_cache_extension.
61
+ # In cases where a request already has an extension, such as <tt>.xml</tt>
62
+ # or <tt>.rss</tt>, page caching will not add an extension. This allows it
63
+ # to work well with RESTful apps.
52
64
class_attribute :page_cache_extension
53
65
self . page_cache_extension ||= '.html'
54
66
55
- # The compression used for gzip. If false (default), the page is not compressed.
56
- # If can be a symbol showing the ZLib compression method, for example, :best_compression
57
- # or :best_speed or an integer configuring the compression level.
67
+ # The compression used for gzip. If + false+ (default), the page is not compressed.
68
+ # If can be a symbol showing the ZLib compression method, for example, <tt> :best_compression</tt>
69
+ # or <tt> :best_speed</tt> or an integer configuring the compression level.
58
70
class_attribute :page_cache_compression
59
71
self . page_cache_compression ||= false
60
72
end
61
73
62
74
module ClassMethods
63
75
# Expires the page that was cached with the +path+ as a key.
64
76
#
65
- # expire_page " /lists/show"
77
+ # expire_page ' /lists/show'
66
78
def expire_page ( path )
67
79
return unless perform_caching
68
80
path = page_cache_path ( path )
@@ -75,7 +87,7 @@ def expire_page(path)
75
87
76
88
# Manually cache the +content+ in the key determined by +path+.
77
89
#
78
- # cache_page "I'm the cached content", " /lists/show"
90
+ # cache_page "I'm the cached content", ' /lists/show'
79
91
def cache_page ( content , path , extension = nil , gzip = Zlib ::BEST_COMPRESSION )
80
92
return unless perform_caching
81
93
path = page_cache_path ( path , extension )
@@ -90,19 +102,19 @@ def cache_page(content, path, extension = nil, gzip = Zlib::BEST_COMPRESSION)
90
102
end
91
103
92
104
# Caches the +actions+ using the page-caching approach that'll store
93
- # the cache in a path within the page_cache_directory that
105
+ # the cache in a path within the + page_cache_directory+ that
94
106
# matches the triggering url.
95
107
#
96
- # You can also pass a :gzip option to override the class configuration one.
108
+ # You can also pass a <tt> :gzip</tt> option to override the class configuration one.
97
109
#
98
110
# # cache the index action
99
111
# caches_page :index
100
112
#
101
113
# # cache the index action except for JSON requests
102
- # caches_page :index, :if => Proc.new { !request.format.json? }
114
+ # caches_page :index, if: Proc.new { !request.format.json? }
103
115
#
104
116
# # don't gzip images
105
- # caches_page :image, : gzip => false
117
+ # caches_page :image, gzip: false
106
118
def caches_page ( *actions )
107
119
return unless perform_caching
108
120
options = actions . extract_options!
@@ -144,7 +156,7 @@ def instrument_page_cache(name, path)
144
156
145
157
# Expires the page that was cached with the +options+ as a key.
146
158
#
147
- # expire_page : controller => " lists", : action => " show"
159
+ # expire_page controller: ' lists', action: ' show'
148
160
def expire_page ( options = { } )
149
161
return unless self . class . perform_caching
150
162
@@ -161,10 +173,11 @@ def expire_page(options = {})
161
173
end
162
174
end
163
175
164
- # Manually cache the +content+ in the key determined by +options+. If no content is provided, the contents of response.body is used.
165
- # If no options are provided, the url of the current request being handled is used.
176
+ # Manually cache the +content+ in the key determined by +options+. If no content is provided,
177
+ # the contents of response.body is used. If no options are provided, the url of the current
178
+ # request being handled is used.
166
179
#
167
- # cache_page "I'm the cached content", : controller => " lists", : action => " show"
180
+ # cache_page "I'm the cached content", controller: ' lists', action: ' show'
168
181
def cache_page ( content = nil , options = nil , gzip = Zlib ::BEST_COMPRESSION )
169
182
return unless self . class . perform_caching && caching_allowed?
170
183
0 commit comments