cache_rocket 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +86 -31
- data/lib/cache_rocket/fragment.rb +9 -8
- data/lib/cache_rocket/key.rb +2 -4
- data/lib/cache_rocket/version.rb +3 -1
- data/lib/cache_rocket.rb +25 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 378ac5399bdb7f58e8c5b3dfeaea5a76c4369997
|
4
|
+
data.tar.gz: 6d0835c56392e598f0b2fff73c6296bb25efd2be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 85ff2503338f81cc30a5f9ccb68917e1ff9cd3395294ae7a51503c6f4e46920d25b76d84d5d0ee87277b9a134eb2d603e235cbdcd416f247c454ecb8e01dcdaf
|
7
|
+
data.tar.gz: 47af1c4e271361ae739086a237c574d5bca195eee62ea06557d417db16c2c10a62b405e94b47d7e01c7b7e9bec1d7218dec268a409d5125d90075ab2263cdfd6
|
data/README.md
CHANGED
@@ -14,7 +14,7 @@ CacheRocket improves server-side fragment caching efficiency in Rails.
|
|
14
14
|
CacheRocket allows caching more generic html fragments and allows the contents of the cached fragments
|
15
15
|
to be replaced with dynamic content.
|
16
16
|
|
17
|
-
|
17
|
+
## Install
|
18
18
|
|
19
19
|
Add the gem to your Gemfile:
|
20
20
|
|
@@ -29,15 +29,48 @@ Most likely you would put this in your `ApplicationHelper`:
|
|
29
29
|
include CacheRocket
|
30
30
|
```
|
31
31
|
|
32
|
-
|
32
|
+
## Use
|
33
33
|
|
34
34
|
CacheRocket allows you to cache a fragment of html and replace inner html.
|
35
35
|
You inject dynamic content into a static, cached outer partial.
|
36
|
-
You cache the donut and replace the donut hole.
|
37
36
|
|
38
|
-
|
37
|
+
### `cache_replace`
|
38
|
+
|
39
|
+
The simplest usage is inline with the `cache_replace` helper. It works
|
40
|
+
like Rails' `cache` method, plus it replaces content that you do not want
|
41
|
+
to cache because it is impractical or inefficient.
|
42
|
+
|
43
|
+
For example:
|
44
|
+
|
45
|
+
#### Before
|
46
|
+
|
47
|
+
```haml
|
48
|
+
.htmls
|
49
|
+
= Time.now
|
50
|
+
```
|
51
|
+
|
52
|
+
#### After
|
53
|
+
|
54
|
+
```haml
|
55
|
+
- cache_replace("the-time", replace: { time: Time.now }) do
|
56
|
+
.htmls
|
57
|
+
= cache_replace_key :time
|
58
|
+
```
|
59
|
+
|
60
|
+
Now the fragment is cached once with a placeholder, and the time is always replaced when rendered.
|
61
|
+
|
62
|
+
Obviously, the above example is contrived and would not result in any
|
63
|
+
performance benefit. When the block of html you are rendering is large,
|
64
|
+
caching can help.
|
65
|
+
|
66
|
+
### `render_cached`
|
67
|
+
|
68
|
+
`render_cached` has options to deal with partials and collections.
|
69
|
+
Use it as a replacement for Rails' `render`, with `replace` options.
|
39
70
|
For example:
|
40
71
|
|
72
|
+
#### Before
|
73
|
+
|
41
74
|
##### file.html.haml:
|
42
75
|
```haml
|
43
76
|
= render 'outer'
|
@@ -48,20 +81,17 @@ For example:
|
|
48
81
|
.lots
|
49
82
|
.of
|
50
83
|
.htmls
|
51
|
-
=
|
84
|
+
= Time.now
|
52
85
|
```
|
53
86
|
|
54
|
-
|
55
|
-
```haml
|
56
|
-
= uncacheable_content
|
57
|
-
```
|
87
|
+
#### After
|
58
88
|
|
59
|
-
|
60
|
-
with `render_cached` in `file`, specify the
|
89
|
+
Replace `render`
|
90
|
+
with `render_cached` in `file`, specify the content to replace, and cache `outer`:
|
61
91
|
|
62
92
|
##### file.html.haml:
|
63
93
|
```haml
|
64
|
-
= render_cached 'outer', replace:
|
94
|
+
= render_cached 'outer', replace: { inner: Time.now }
|
65
95
|
```
|
66
96
|
|
67
97
|
##### _outer.html.haml:
|
@@ -70,19 +100,39 @@ with `render_cached` in `file`, specify the partial to replace in `outer`, and c
|
|
70
100
|
.lots
|
71
101
|
.of
|
72
102
|
.htmls
|
73
|
-
= cache_replace_key
|
103
|
+
= cache_replace_key :inner
|
104
|
+
```
|
105
|
+
|
106
|
+
Here is the same example with an inner partial:
|
107
|
+
|
108
|
+
#### Before
|
109
|
+
|
110
|
+
##### file.html.haml:
|
111
|
+
```haml
|
112
|
+
= render 'outer'
|
113
|
+
```
|
114
|
+
|
115
|
+
##### _outer.html.haml:
|
116
|
+
```haml
|
117
|
+
.lots
|
118
|
+
.of
|
119
|
+
.htmls
|
120
|
+
= render 'inner'
|
74
121
|
```
|
75
122
|
|
76
123
|
##### _inner.html.haml:
|
77
|
-
```
|
78
|
-
=
|
124
|
+
```haml
|
125
|
+
= Time.now
|
79
126
|
```
|
80
127
|
|
81
|
-
|
128
|
+
#### After
|
129
|
+
|
130
|
+
Replace `render` with `render_cached` in `file`,
|
131
|
+
specify the partial to replace in `outer`, and cache `outer`:
|
82
132
|
|
83
133
|
##### file.html.haml:
|
84
134
|
```haml
|
85
|
-
= render_cached 'outer', replace:
|
135
|
+
= render_cached 'outer', replace: 'inner'
|
86
136
|
```
|
87
137
|
|
88
138
|
##### _outer.html.haml:
|
@@ -94,6 +144,11 @@ With the example above, you could also remove the `_inner.html.haml` file altoge
|
|
94
144
|
= cache_replace_key :inner
|
95
145
|
```
|
96
146
|
|
147
|
+
##### _inner.html.haml:
|
148
|
+
``` haml
|
149
|
+
= Time.now
|
150
|
+
```
|
151
|
+
|
97
152
|
### Options
|
98
153
|
|
99
154
|
`render_cached` supports several styles of arguments:
|
@@ -105,16 +160,19 @@ render_cached 'outer', replace: 'inner'
|
|
105
160
|
```
|
106
161
|
|
107
162
|
#### Array of partials to replace
|
163
|
+
|
108
164
|
```ruby
|
109
165
|
render_cached 'outer', replace: ['inner', 'footer']
|
110
166
|
```
|
111
167
|
|
112
168
|
#### Hash of keys to replace with values
|
169
|
+
|
113
170
|
```ruby
|
114
171
|
render_cached 'outer', replace: { key_name: a_helper_method(object) }
|
115
172
|
```
|
116
173
|
|
117
174
|
#### Block containing a hash of keys to replace with values
|
175
|
+
|
118
176
|
```ruby
|
119
177
|
render_cached 'outer' do
|
120
178
|
{ key_name: a_helper_method(object) }
|
@@ -122,12 +180,14 @@ end
|
|
122
180
|
```
|
123
181
|
|
124
182
|
#### Render a collection with hash of keys, using a lambda for each collection item
|
183
|
+
|
125
184
|
```ruby
|
126
185
|
render_cached 'outer', collection: objects,
|
127
186
|
replace: { key_name: -> (object) { a_helper_method(object) } }
|
128
187
|
```
|
129
188
|
|
130
189
|
#### Render a collection with block syntax
|
190
|
+
|
131
191
|
```ruby
|
132
192
|
render_cached 'outer', collection: objects do
|
133
193
|
{ key_name: -> (object) { a_helper_method(object) } }
|
@@ -135,6 +195,7 @@ end
|
|
135
195
|
```
|
136
196
|
|
137
197
|
#### Render a collection with block syntax with multiple keys
|
198
|
+
|
138
199
|
```ruby
|
139
200
|
render_cached 'outer', collection: objects do
|
140
201
|
{
|
@@ -144,12 +205,12 @@ render_cached 'outer', collection: objects do
|
|
144
205
|
end
|
145
206
|
```
|
146
207
|
|
147
|
-
|
208
|
+
## YMMV
|
148
209
|
|
149
210
|
`cache_rocket` is not magic. It should not be used in all situations.
|
150
211
|
Benchmark your page rendering times before and after to see if it helps.
|
151
212
|
|
152
|
-
|
213
|
+
## Benefits
|
153
214
|
|
154
215
|
#### More server-side caching
|
155
216
|
|
@@ -159,41 +220,35 @@ See the example above.
|
|
159
220
|
|
160
221
|
Typically, one would key the `users/bio` partial on the `user` object like so:
|
161
222
|
|
162
|
-
##### users/bio.haml:
|
223
|
+
##### users/bio.html.haml:
|
224
|
+
|
163
225
|
```haml
|
164
226
|
- cache [user, 'bio'] do
|
165
227
|
.lots-of-htmls
|
166
228
|
= user.bio
|
167
229
|
```
|
168
230
|
|
169
|
-
```haml
|
170
|
-
= render 'users/bio'
|
171
|
-
```
|
172
|
-
|
173
231
|
With 1000 users, there are 1000 cached items. This can use a lot of memory.
|
174
232
|
Instead we can cache the `users/bio` partial once and replace the content we need using
|
175
233
|
`cache_replace`. With 1000 users, we use 1/1000th the memory.
|
176
234
|
|
177
|
-
##### users/bio.haml:
|
235
|
+
##### users/bio.html.haml:
|
178
236
|
```haml
|
179
|
-
-
|
237
|
+
- cache_replace('users/bio', replace: { bio: user.bio }) do
|
180
238
|
.lots-of-htmls
|
181
239
|
= cache_replace_key :bio
|
182
240
|
```
|
183
241
|
|
184
|
-
```haml
|
185
|
-
= render_cached 'users/bio', replace: { bio: user.bio }
|
186
|
-
```
|
187
|
-
|
188
242
|
#### Simpler cache keys
|
189
243
|
|
190
244
|
If you have a cache key containing multiple models, it will generally be very inefficient:
|
245
|
+
|
191
246
|
```haml
|
192
247
|
- cache(user, other_user) do
|
193
248
|
= render 'common_interests'
|
194
249
|
```
|
195
250
|
|
196
|
-
If the cached content is rarely retrieved, `
|
251
|
+
If the cached content is rarely retrieved, `render_cached` can help:
|
197
252
|
|
198
253
|
```haml
|
199
254
|
- cache('common_interests') do
|
@@ -1,19 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module CacheRocket
|
2
4
|
class Fragment
|
3
5
|
include Key
|
4
6
|
|
5
|
-
attr_accessor :value
|
6
|
-
|
7
7
|
def initialize(value)
|
8
|
-
|
8
|
+
@value = value
|
9
9
|
end
|
10
10
|
|
11
11
|
def to_s
|
12
|
-
value
|
12
|
+
@value
|
13
13
|
end
|
14
14
|
|
15
15
|
def gsub!(key, value)
|
16
|
-
|
16
|
+
@value.gsub! key, value
|
17
17
|
end
|
18
18
|
|
19
19
|
def replace(hash, collection)
|
@@ -22,6 +22,7 @@ module CacheRocket
|
|
22
22
|
else
|
23
23
|
replace_from_hash hash
|
24
24
|
end
|
25
|
+
self
|
25
26
|
end
|
26
27
|
|
27
28
|
private
|
@@ -36,14 +37,14 @@ module CacheRocket
|
|
36
37
|
html = ""
|
37
38
|
|
38
39
|
collection.each do |item|
|
39
|
-
html
|
40
|
+
html += replace_item_hash(item, replace_hash)
|
40
41
|
end
|
41
42
|
|
42
|
-
|
43
|
+
@value = html
|
43
44
|
end
|
44
45
|
|
45
46
|
def replace_item_hash(item, hash)
|
46
|
-
item_fragment = value.dup
|
47
|
+
item_fragment = @value.dup
|
47
48
|
|
48
49
|
hash.each do |key, proc|
|
49
50
|
item_fragment.gsub! cache_replace_key(key), proc.call(item)
|
data/lib/cache_rocket/key.rb
CHANGED
@@ -1,12 +1,10 @@
|
|
1
1
|
module CacheRocket
|
2
2
|
module Key
|
3
|
-
CACHE_REPLACE_KEY_OPEN = "<cr "
|
4
|
-
|
5
3
|
# string key containing the partial file name or placeholder key.
|
6
4
|
# It is a tag that should never be returned to be rendered by the
|
7
|
-
# client, but if so, it will be hidden since
|
5
|
+
# client, but if so, it will be hidden since CRK is not a valid html tag.
|
8
6
|
def cache_replace_key(key)
|
9
|
-
"#{
|
7
|
+
"<crk #{key}>".html_safe
|
10
8
|
end
|
11
9
|
end
|
12
10
|
end
|
data/lib/cache_rocket/version.rb
CHANGED
data/lib/cache_rocket.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_support/core_ext/string"
|
2
4
|
require "cache_rocket/key"
|
3
5
|
require "cache_rocket/fragment"
|
@@ -7,6 +9,7 @@ module CacheRocket
|
|
7
9
|
include Key
|
8
10
|
|
9
11
|
ERROR_MISSING_KEY_OR_BLOCK = "You must either pass a `replace` key or a block to render_cached."
|
12
|
+
ERROR_MISSING_KEY = "You must pass a `replace` key to cache_replace."
|
10
13
|
|
11
14
|
# Supports 5 options:
|
12
15
|
#
|
@@ -53,4 +56,26 @@ module CacheRocket
|
|
53
56
|
|
54
57
|
fragment.to_s.html_safe
|
55
58
|
end
|
59
|
+
|
60
|
+
# This method works like Rails' CacheHelper#cache,
|
61
|
+
# plus it replaces content in the replace hash.
|
62
|
+
# It must have a `replace` option.
|
63
|
+
#
|
64
|
+
# - cache_replace(["key1", "key2"], replace: { name: "x" }) do
|
65
|
+
# .htmls
|
66
|
+
# = cache_replace_key :name
|
67
|
+
#
|
68
|
+
# https://github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/cache_helper.rb
|
69
|
+
def cache_replace(name = {}, options = {}, &block)
|
70
|
+
replace_hash = options.delete(:replace)
|
71
|
+
raise(ArgumentError, ERROR_MISSING_KEY) unless replace_hash
|
72
|
+
|
73
|
+
name_options = options.slice(:skip_digest, :virtual_path)
|
74
|
+
safe_concat \
|
75
|
+
Fragment.new(
|
76
|
+
fragment_for(cache_fragment_name(name, name_options), options, &block)
|
77
|
+
).replace(replace_hash, nil).to_s.html_safe
|
78
|
+
|
79
|
+
nil
|
80
|
+
end
|
56
81
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cache_rocket
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tee Parham
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-07-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -85,8 +85,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
85
|
version: '0'
|
86
86
|
requirements: []
|
87
87
|
rubyforge_project:
|
88
|
-
rubygems_version: 2.6.
|
88
|
+
rubygems_version: 2.6.12
|
89
89
|
signing_key:
|
90
90
|
specification_version: 4
|
91
91
|
summary: Rails rendering extension for server-side html caching
|
92
92
|
test_files: []
|
93
|
+
has_rdoc:
|