openstreetmap-actionpack-page_caching 1.1.2
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.
- checksums.yaml +7 -0
- data/.codeclimate.yml +7 -0
- data/.gitignore +5 -0
- data/.rubocop.yml +116 -0
- data/.travis.yml +43 -0
- data/CHANGELOG.md +47 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +215 -0
- data/Rakefile +11 -0
- data/actionpack-page_caching.gemspec +21 -0
- data/gemfiles/Gemfile-5-0-stable +5 -0
- data/gemfiles/Gemfile-5-1-stable +5 -0
- data/gemfiles/Gemfile-5-2-stable +5 -0
- data/gemfiles/Gemfile-6-0-stable +5 -0
- data/gemfiles/Gemfile-edge +6 -0
- data/lib/action_controller/caching/pages.rb +306 -0
- data/lib/action_controller/page_caching.rb +13 -0
- data/lib/actionpack/page_caching.rb +1 -0
- data/lib/actionpack/page_caching/railtie.rb +17 -0
- data/test/abstract_unit.rb +12 -0
- data/test/caching_test.rb +524 -0
- data/test/log_subscriber_test.rb +57 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e69b08cb6ea991488bfaa2cc9da8e8b622b8274ea783dd2691dba7c6095ec98b
|
4
|
+
data.tar.gz: b0a314a0c11879641dc509c2bf14b619a61a35e7b2981035e382f3af102fe882
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e612b361bcdd0dc7581ae85a9a98b8258cc1716d5a6925a0260657c80b2def7f49fe22420914c38fb3a4b8d6d9eb6cb294f43a417ffa9395e64b20e7e396da6d
|
7
|
+
data.tar.gz: 2e0ad0654a516f12652870838e75c11d7d84d129def2ea94c3f1e8a7a38b53d7a87fe3589ef9ac81c56a7b3f1ada2762cb689163aa0d5840ef42576a0f0f18ac
|
data/.codeclimate.yml
ADDED
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.2
|
3
|
+
# RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
|
4
|
+
# to ignore them, so only the ones explicitly set in this file are enabled.
|
5
|
+
DisabledByDefault: true
|
6
|
+
|
7
|
+
# Prefer &&/|| over and/or.
|
8
|
+
Style/AndOr:
|
9
|
+
Enabled: true
|
10
|
+
|
11
|
+
# Do not use braces for hash literals when they are the last argument of a
|
12
|
+
# method call.
|
13
|
+
Style/BracesAroundHashParameters:
|
14
|
+
Enabled: true
|
15
|
+
|
16
|
+
# Align `when` with `case`.
|
17
|
+
Style/CaseIndentation:
|
18
|
+
Enabled: true
|
19
|
+
|
20
|
+
# Align comments with method definitions.
|
21
|
+
Style/CommentIndentation:
|
22
|
+
Enabled: true
|
23
|
+
|
24
|
+
# No extra empty lines.
|
25
|
+
Style/EmptyLines:
|
26
|
+
Enabled: true
|
27
|
+
|
28
|
+
# In a regular class definition, no empty lines around the body.
|
29
|
+
Style/EmptyLinesAroundClassBody:
|
30
|
+
Enabled: true
|
31
|
+
|
32
|
+
# In a regular module definition, no empty lines around the body.
|
33
|
+
Style/EmptyLinesAroundModuleBody:
|
34
|
+
Enabled: true
|
35
|
+
|
36
|
+
# Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
|
37
|
+
Style/HashSyntax:
|
38
|
+
Enabled: true
|
39
|
+
|
40
|
+
# Method definitions after `private` or `protected` isolated calls need one
|
41
|
+
# extra level of indentation.
|
42
|
+
Style/IndentationConsistency:
|
43
|
+
Enabled: true
|
44
|
+
EnforcedStyle: rails
|
45
|
+
|
46
|
+
# Two spaces, no tabs (for indentation).
|
47
|
+
Style/IndentationWidth:
|
48
|
+
Enabled: true
|
49
|
+
|
50
|
+
Style/SpaceAfterColon:
|
51
|
+
Enabled: true
|
52
|
+
|
53
|
+
Style/SpaceAfterComma:
|
54
|
+
Enabled: true
|
55
|
+
|
56
|
+
Style/SpaceAroundEqualsInParameterDefault:
|
57
|
+
Enabled: true
|
58
|
+
|
59
|
+
Style/SpaceAroundKeyword:
|
60
|
+
Enabled: true
|
61
|
+
|
62
|
+
Style/SpaceAroundOperators:
|
63
|
+
Enabled: true
|
64
|
+
|
65
|
+
Style/SpaceBeforeFirstArg:
|
66
|
+
Enabled: true
|
67
|
+
|
68
|
+
# Defining a method with parameters needs parentheses.
|
69
|
+
Style/MethodDefParentheses:
|
70
|
+
Enabled: true
|
71
|
+
|
72
|
+
# Use `foo {}` not `foo{}`.
|
73
|
+
Style/SpaceBeforeBlockBraces:
|
74
|
+
Enabled: true
|
75
|
+
|
76
|
+
# Use `foo { bar }` not `foo {bar}`.
|
77
|
+
Style/SpaceInsideBlockBraces:
|
78
|
+
Enabled: true
|
79
|
+
|
80
|
+
# Use `{ a: 1 }` not `{a:1}`.
|
81
|
+
Style/SpaceInsideHashLiteralBraces:
|
82
|
+
Enabled: true
|
83
|
+
|
84
|
+
Style/SpaceInsideParens:
|
85
|
+
Enabled: true
|
86
|
+
|
87
|
+
# Check quotes usage according to lint rule below.
|
88
|
+
Style/StringLiterals:
|
89
|
+
Enabled: true
|
90
|
+
EnforcedStyle: double_quotes
|
91
|
+
|
92
|
+
# Detect hard tabs, no hard tabs.
|
93
|
+
Style/Tab:
|
94
|
+
Enabled: true
|
95
|
+
|
96
|
+
# Blank lines should not have any spaces.
|
97
|
+
Style/TrailingBlankLines:
|
98
|
+
Enabled: true
|
99
|
+
|
100
|
+
# No trailing whitespace.
|
101
|
+
Style/TrailingWhitespace:
|
102
|
+
Enabled: true
|
103
|
+
|
104
|
+
# Use quotes for string literals when they are enough.
|
105
|
+
Style/UnneededPercentQ:
|
106
|
+
Enabled: true
|
107
|
+
|
108
|
+
# Align `end` with the matching keyword or starting expression except for
|
109
|
+
# assignments, where it should be aligned with the LHS.
|
110
|
+
Lint/EndAlignment:
|
111
|
+
Enabled: true
|
112
|
+
AlignWith: variable
|
113
|
+
|
114
|
+
# Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
|
115
|
+
Lint/RequireParentheses:
|
116
|
+
Enabled: true
|
data/.travis.yml
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
language: ruby
|
2
|
+
sudo: false
|
3
|
+
|
4
|
+
cache:
|
5
|
+
bundler: true
|
6
|
+
|
7
|
+
before_install:
|
8
|
+
- gem install bundler
|
9
|
+
|
10
|
+
rvm:
|
11
|
+
- 2.4.6
|
12
|
+
- 2.5.5
|
13
|
+
- 2.6.3
|
14
|
+
|
15
|
+
gemfile:
|
16
|
+
- Gemfile
|
17
|
+
- gemfiles/Gemfile-5-0-stable
|
18
|
+
- gemfiles/Gemfile-5-1-stable
|
19
|
+
- gemfiles/Gemfile-5-2-stable
|
20
|
+
- gemfiles/Gemfile-6-0-stable
|
21
|
+
- gemfiles/Gemfile-edge
|
22
|
+
|
23
|
+
matrix:
|
24
|
+
allow_failures:
|
25
|
+
- gemfile: gemfiles/Gemfile-edge
|
26
|
+
exclude:
|
27
|
+
- rvm: 2.4.6
|
28
|
+
gemfile: gemfiles/Gemfile-6-0-stable
|
29
|
+
- rvm: 2.4.6
|
30
|
+
gemfile: gemfiles/Gemfile-edge
|
31
|
+
|
32
|
+
notifications:
|
33
|
+
email: false
|
34
|
+
irc:
|
35
|
+
on_success: change
|
36
|
+
on_failure: always
|
37
|
+
channels:
|
38
|
+
- "irc.freenode.org#rails-contrib"
|
39
|
+
campfire:
|
40
|
+
on_success: change
|
41
|
+
on_failure: always
|
42
|
+
rooms:
|
43
|
+
- secure: "eRCx+FMvH50pmLu0GZTF7NN+2X+CesgodYUlHvCr5EXQ0ZO/YUmeW8vAh/N8\njSrLWYpk/4P/JA63JGWsvFor/zpkTnfwzX3LWgw04GV0V3T9jsn9CD2Coiu6\nFll5u4fUCUwpfbB4RlCkjvFdQmW+F9mmbRGMCDO5CmuPHOyyPH0="
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
## 1.1.1 (September 25, 2018)
|
2
|
+
|
3
|
+
* Fixes handling of several forward slashes as root path.
|
4
|
+
|
5
|
+
*Xavier Noria*
|
6
|
+
|
7
|
+
* Documentation overhaul.
|
8
|
+
|
9
|
+
*Xavier Noria*
|
10
|
+
|
11
|
+
|
12
|
+
## 1.1.0 (January 23, 2017)
|
13
|
+
|
14
|
+
* Support dynamic `page_cache_directory` using a Proc, Symbol or callable
|
15
|
+
|
16
|
+
*Andrew White*
|
17
|
+
|
18
|
+
* Support instance level setting of `page_cache_directory`
|
19
|
+
|
20
|
+
*Andrew White*
|
21
|
+
|
22
|
+
* Add support for Rails 5.0 and master
|
23
|
+
|
24
|
+
*Andrew White*
|
25
|
+
|
26
|
+
|
27
|
+
## 1.0.2 (November 15, 2013)
|
28
|
+
|
29
|
+
* Fix load order problem with other gems.
|
30
|
+
|
31
|
+
*Rafael Mendonça França*
|
32
|
+
|
33
|
+
|
34
|
+
## 1.0.1 (October 24, 2013)
|
35
|
+
|
36
|
+
* Add Railtie to set `page_cache_directory` by default to `public` folder.
|
37
|
+
|
38
|
+
Fixes #5.
|
39
|
+
|
40
|
+
*Žiga Vidic*
|
41
|
+
|
42
|
+
|
43
|
+
## 1.0.0 (February 27, 2013)
|
44
|
+
|
45
|
+
* Extract Action Pack - Action Caching from Rails core.
|
46
|
+
|
47
|
+
*Francesco Rodriguez*, *Rafael Mendonça França*, *Michiel Sikkes*
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 David Heinemeier Hansson
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,215 @@
|
|
1
|
+
# actionpack-page_caching
|
2
|
+
|
3
|
+
Static page caching for Action Pack (removed from core in Rails 4.0).
|
4
|
+
|
5
|
+
## Introduction
|
6
|
+
|
7
|
+
Page caching is an approach to caching in which response bodies are stored in
|
8
|
+
files that the web server can serve directly:
|
9
|
+
|
10
|
+
1. A request to endpoint _E_ arrives.
|
11
|
+
2. Its response is calculated and stored in a file _F_.
|
12
|
+
3. Next time _E_ is requested, the web server sends _F_ directly.
|
13
|
+
|
14
|
+
That applies only to GET or HEAD requests whose response code is 200, the rest
|
15
|
+
are ignored.
|
16
|
+
|
17
|
+
Unlike caching proxies or other more sophisticated setups, page caching results
|
18
|
+
in a dramatic speed up while being dead simple at the same time. Awesome
|
19
|
+
cost/benefit.
|
20
|
+
|
21
|
+
The reason for such performance boost is that cached endpoints are
|
22
|
+
short-circuited by the web server, which is very efficient at serving static
|
23
|
+
files. Requests to cached endpoints do not even reach your Rails application.
|
24
|
+
|
25
|
+
This technique, however, is only suitable for pages that do not need to go
|
26
|
+
through your Rails stack, precisely. For example, content management systems
|
27
|
+
like wikis have typically many pages that are a great fit for this approach, but
|
28
|
+
account-based systems where people log in and manipulate their own data are
|
29
|
+
often less likely candidates. As a use case you can check, [Rails
|
30
|
+
Contributors](https://contributors.rubyonrails.org/) makes heavy use of page
|
31
|
+
caching. Its source code is [here](https://github.com/rails/rails-contributors).
|
32
|
+
|
33
|
+
It is not all or nothing, though, in HTML cached pages JavaScript can still
|
34
|
+
tweak details here and there dynamically as a trade-off.
|
35
|
+
|
36
|
+
## Installation
|
37
|
+
|
38
|
+
Add this line to your application's `Gemfile`:
|
39
|
+
|
40
|
+
``` ruby
|
41
|
+
gem "actionpack-page_caching"
|
42
|
+
```
|
43
|
+
|
44
|
+
And then execute:
|
45
|
+
|
46
|
+
```
|
47
|
+
$ bundle
|
48
|
+
```
|
49
|
+
|
50
|
+
## Usage
|
51
|
+
|
52
|
+
### Enable Caching
|
53
|
+
|
54
|
+
Page caching needs caching enabled:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
config.action_controller.perform_caching = true
|
58
|
+
```
|
59
|
+
|
60
|
+
That goes typically in `config/environments/production.rb`, but you can activate
|
61
|
+
that flag in any mode.
|
62
|
+
|
63
|
+
Since Rails 5 there is a special toggler to easily enable/disable caching in
|
64
|
+
`development` mode without editing its configuration file. Just execute
|
65
|
+
|
66
|
+
```
|
67
|
+
$ bin/rails dev:cache
|
68
|
+
```
|
69
|
+
|
70
|
+
to enable/disable caching in `development` mode.
|
71
|
+
|
72
|
+
### Configure the Cache Directory
|
73
|
+
|
74
|
+
#### Default Cache Directory
|
75
|
+
|
76
|
+
By default, files are stored below the `public` directory of your Rails
|
77
|
+
application, with a path that matches the one in the URL.
|
78
|
+
|
79
|
+
For example, a page-cached request to `/posts/what-is-new-in-rails-6` would be
|
80
|
+
stored by default in the file `public/posts/what-is-new-in-rails-6.html`, and
|
81
|
+
the web server would be configured to check that path in the file system before
|
82
|
+
falling back to Rails. More on this later.
|
83
|
+
|
84
|
+
#### Custom Cache Directory
|
85
|
+
|
86
|
+
The default page caching directory can be overridden:
|
87
|
+
|
88
|
+
``` ruby
|
89
|
+
config.action_controller.page_cache_directory = Rails.root.join("public", "cached_pages")
|
90
|
+
```
|
91
|
+
|
92
|
+
There is no need to ensure the directory exists when the application boots,
|
93
|
+
whenever a page has to be cached, the page cache directory is created if needed.
|
94
|
+
|
95
|
+
#### Custom Cache Directory per Controller
|
96
|
+
|
97
|
+
The globally configured cache directory, default or custom, can be overridden in
|
98
|
+
each controller. There are three ways of doing this.
|
99
|
+
|
100
|
+
With a lambda:
|
101
|
+
|
102
|
+
``` ruby
|
103
|
+
class WeblogController < ApplicationController
|
104
|
+
self.page_cache_directory = -> {
|
105
|
+
Rails.root.join("public", request.domain)
|
106
|
+
}
|
107
|
+
end
|
108
|
+
```
|
109
|
+
|
110
|
+
a symbol:
|
111
|
+
|
112
|
+
``` ruby
|
113
|
+
class WeblogController < ApplicationController
|
114
|
+
self.page_cache_directory = :domain_cache_directory
|
115
|
+
|
116
|
+
private
|
117
|
+
def domain_cache_directory
|
118
|
+
Rails.root.join("public", request.domain)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
```
|
122
|
+
|
123
|
+
or a callable object:
|
124
|
+
|
125
|
+
``` ruby
|
126
|
+
class DomainCacheDirectory
|
127
|
+
def self.call(request)
|
128
|
+
Rails.root.join("public", request.domain)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
class WeblogController < ApplicationController
|
133
|
+
self.page_cache_directory = DomainCacheDirectory
|
134
|
+
end
|
135
|
+
```
|
136
|
+
|
137
|
+
Intermediate directories are created as needed also in this case.
|
138
|
+
|
139
|
+
### Specify Actions to be Cached
|
140
|
+
|
141
|
+
Specifying which actions have to be cached is done through the `caches_page` class method:
|
142
|
+
|
143
|
+
``` ruby
|
144
|
+
class WeblogController < ActionController::Base
|
145
|
+
caches_page :show, :new
|
146
|
+
end
|
147
|
+
```
|
148
|
+
|
149
|
+
### Configure The Web Server
|
150
|
+
|
151
|
+
The [wiki](https://github.com/rails/actionpack-page_caching/wiki) of the project
|
152
|
+
has some examples of web server configuration.
|
153
|
+
|
154
|
+
### Cache Expiration
|
155
|
+
|
156
|
+
Expiration of the cache is handled by deleting the cached files, which results
|
157
|
+
in a lazy regeneration approach in which the content is stored again as cached
|
158
|
+
endpoints are hit.
|
159
|
+
|
160
|
+
#### Full Cache Expiration
|
161
|
+
|
162
|
+
If the cache is stored in a separate directory like `public/cached_pages`, you
|
163
|
+
can easily expire the whole thing by removing said directory.
|
164
|
+
|
165
|
+
Removing a directory recursively with something like `rm -rf` is unreliable
|
166
|
+
because that operation is not atomic and can mess up with concurrent page cache
|
167
|
+
generation.
|
168
|
+
|
169
|
+
In POSIX systems moving a file is atomic, so the recommended approach would be
|
170
|
+
to move the directory first out of the way, and then recursively delete that
|
171
|
+
one. Something like
|
172
|
+
|
173
|
+
```bash
|
174
|
+
#!/bin/bash
|
175
|
+
|
176
|
+
tmp=public/cached_pages-$(date +%s)
|
177
|
+
mv public/cached_pages $tmp
|
178
|
+
rm -rf $tmp
|
179
|
+
```
|
180
|
+
|
181
|
+
As noted before, the page cache directory is created if it does not exist, so
|
182
|
+
moving the directory is enough to have a clean cache, no need to recreate.
|
183
|
+
|
184
|
+
#### Fine-grained Cache Expiration
|
185
|
+
|
186
|
+
The API for doing so mimics the options from `url_for` and friends:
|
187
|
+
|
188
|
+
``` ruby
|
189
|
+
class WeblogController < ActionController::Base
|
190
|
+
def update
|
191
|
+
List.update(params[:list][:id], params[:list])
|
192
|
+
expire_page action: "show", id: params[:list][:id]
|
193
|
+
redirect_to action: "show", id: params[:list][:id]
|
194
|
+
end
|
195
|
+
end
|
196
|
+
```
|
197
|
+
|
198
|
+
Additionally, you can expire caches using
|
199
|
+
[Sweepers](https://github.com/rails/rails-observers#action-controller-sweeper)
|
200
|
+
that act on changes in the model to determine when a cache is supposed to be
|
201
|
+
expired.
|
202
|
+
|
203
|
+
Contributing
|
204
|
+
------------
|
205
|
+
|
206
|
+
1. Fork it.
|
207
|
+
2. Create your feature branch (`git checkout -b my-new-feature`).
|
208
|
+
3. Commit your changes (`git commit -am 'Add some feature'`).
|
209
|
+
4. Push to the branch (`git push origin my-new-feature`).
|
210
|
+
5. Create a new Pull Request.
|
211
|
+
|
212
|
+
Code Status
|
213
|
+
-----------
|
214
|
+
|
215
|
+
* [](https://travis-ci.org/rails/actionpack-page_caching)
|