actionpack-page_caching 1.1.0 → 1.1.1
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.
Potentially problematic release.
This version of actionpack-page_caching might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -0
- data/README.md +130 -40
- data/actionpack-page_caching.gemspec +1 -1
- data/lib/action_controller/caching/pages.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a8baf2e26a08aa06aabc67d4783382223dbef4c
|
4
|
+
data.tar.gz: '081b862c7762f68baa7ca15a9358689773616bb0'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62aca2629984d4538f10ed0cad44265e91238f231cdd071fb36875a35e6607f547ab69aa33cf7e45c87b05677db69f777682cd2324de6c4a3de133e922fe8aa6
|
7
|
+
data.tar.gz: 7b354f5a3fc117733b5f92228bae747730dda7c70293aa60e7ea8e337c449897f5fa3e2a36179f8b8b409ebd5ff40ee360ba9a7ffb147bd46c71e2431089a15f
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,14 @@
|
|
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
|
+
|
1
12
|
## 1.1.0 (January 23, 2017)
|
2
13
|
|
3
14
|
* Support dynamic `page_cache_directory` using a Proc, Symbol or callable
|
data/README.md
CHANGED
@@ -1,12 +1,41 @@
|
|
1
|
-
actionpack-page_caching
|
2
|
-
=======================
|
1
|
+
# actionpack-page_caching
|
3
2
|
|
4
3
|
Static page caching for Action Pack (removed from core in Rails 4.0).
|
5
4
|
|
6
|
-
|
7
|
-
|
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 reponse 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.
|
8
24
|
|
9
|
-
|
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`:
|
10
39
|
|
11
40
|
``` ruby
|
12
41
|
gem "actionpack-page_caching"
|
@@ -14,39 +43,71 @@ gem "actionpack-page_caching"
|
|
14
43
|
|
15
44
|
And then execute:
|
16
45
|
|
17
|
-
|
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
|
18
73
|
|
19
|
-
|
74
|
+
#### Default Cache Directory
|
20
75
|
|
21
|
-
|
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.
|
22
78
|
|
23
|
-
|
24
|
-
|
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.
|
25
83
|
|
26
|
-
|
27
|
-
stored as a HTML file that the web server can serve without going through
|
28
|
-
Action Pack. This is the fastest way to cache your content as opposed to going
|
29
|
-
dynamically through the process of generating the content. Unfortunately, this
|
30
|
-
incredible speed-up is only available to stateless pages where all visitors are
|
31
|
-
treated the same. Content management systems -- including weblogs and wikis --
|
32
|
-
have many pages that are a great fit for this approach, but account-based systems
|
33
|
-
where people log in and manipulate their own data are often less likely candidates.
|
84
|
+
#### Custom Cache Directory
|
34
85
|
|
35
|
-
|
86
|
+
The default page caching directory can be overridden:
|
36
87
|
|
37
88
|
``` ruby
|
38
|
-
config.action_controller.page_cache_directory =
|
89
|
+
config.action_controller.page_cache_directory = Rails.root.join("public", "cached_pages")
|
39
90
|
```
|
40
91
|
|
41
|
-
|
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:
|
42
101
|
|
43
102
|
``` ruby
|
44
103
|
class WeblogController < ApplicationController
|
45
|
-
self.page_cache_directory = -> {
|
104
|
+
self.page_cache_directory = -> {
|
105
|
+
Rails.root.join("public", request.domain)
|
106
|
+
}
|
46
107
|
end
|
47
108
|
```
|
48
109
|
|
49
|
-
a
|
110
|
+
a symbol:
|
50
111
|
|
51
112
|
``` ruby
|
52
113
|
class WeblogController < ApplicationController
|
@@ -73,7 +134,11 @@ class WeblogController < ApplicationController
|
|
73
134
|
end
|
74
135
|
```
|
75
136
|
|
76
|
-
|
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:
|
77
142
|
|
78
143
|
``` ruby
|
79
144
|
class WeblogController < ActionController::Base
|
@@ -81,17 +146,44 @@ class WeblogController < ActionController::Base
|
|
81
146
|
end
|
82
147
|
```
|
83
148
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
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.
|
90
159
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
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:
|
95
187
|
|
96
188
|
``` ruby
|
97
189
|
class WeblogController < ActionController::Base
|
@@ -103,12 +195,10 @@ class WeblogController < ActionController::Base
|
|
103
195
|
end
|
104
196
|
```
|
105
197
|
|
106
|
-
Additionally, you can expire caches using
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
rather than the original files. See the [project wiki][1] for example configurations.
|
111
|
-
[1]: https://github.com/rails/actionpack-page_caching/wiki
|
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.
|
112
202
|
|
113
203
|
Contributing
|
114
204
|
------------
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |gem|
|
2
2
|
gem.name = "actionpack-page_caching"
|
3
|
-
gem.version = "1.1.
|
3
|
+
gem.version = "1.1.1"
|
4
4
|
gem.author = "David Heinemeier Hansson"
|
5
5
|
gem.email = "david@loudthinking.com"
|
6
6
|
gem.description = "Static page caching for Action Pack (removed from core in Rails 4.0)"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: actionpack-page_caching
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Heinemeier Hansson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-09-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -92,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
92
|
version: '0'
|
93
93
|
requirements: []
|
94
94
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.6.
|
95
|
+
rubygems_version: 2.6.11
|
96
96
|
signing_key:
|
97
97
|
specification_version: 4
|
98
98
|
summary: Static page caching for Action Pack (removed from core in Rails 4.0)
|