chunky_cache 0.1.1 → 0.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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +9 -0
- data/lib/chunky_cache/version.rb +1 -1
- data/lib/chunky_cache/view_helpers.rb +10 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 900e71f999ba2cf5a9996aad4a700b7c22df75e1a240a5fc9e4fc7b2df88e39e
|
4
|
+
data.tar.gz: 42e100f7f0aef3b9bc932dded4f7d3556bf007ed0057b28932cde7a902025f34
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd58b7c0267f3a015a344e68354a83e4b6a74bac7774795d83cfc137331fb7ee750ad7b3732cf66095f8034636fc1959c7d7390ffb840ca5a484cd25cd0b68af
|
7
|
+
data.tar.gz: 4ce371b52d6d27a4b8aafd1cbaa0edcd2a2620574fa105dda0a7db3e0e9810df6e9dc8f0b27484dc5538648c7e6a2fe624f6f769624a270dd85f4d93b5f00be7
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -44,6 +44,15 @@ Or install it yourself as:
|
|
44
44
|
|
45
45
|
This will execute only one call to your cache store, using `Rails.cache.fetch_multi`.
|
46
46
|
|
47
|
+
When using `cache_chunk` inside a block, use its ability to pass the key components to the block
|
48
|
+
to persist the context (i.e. it'll be super borked if you don't do this):
|
49
|
+
|
50
|
+
```slim
|
51
|
+
- for article in @articles
|
52
|
+
= cache_chunk(article) do |article|
|
53
|
+
h1= article.title
|
54
|
+
```
|
55
|
+
|
47
56
|
## How does it work?
|
48
57
|
|
49
58
|
The helpers use Rails' built-in helper `capture` to consume the contents of their blocks and turn them into strings. `chunky_cache` does this immediately, and returns the final output after mixing everything together. But `cache_chunk` instead doesn't execute its block, but stores it in an instance variable established by `chunky_cache`, and it then returns a cache key string. At this point the template is thus half-complete, with sections missing and only weird strings in their place.
|
data/lib/chunky_cache/version.rb
CHANGED
@@ -29,13 +29,18 @@ module ChunkyCache
|
|
29
29
|
# we multi-fetch all the keys from the cache, or call the `cache_chunk` blocks
|
30
30
|
# for missing values.
|
31
31
|
chunks = Rails.cache.fetch_multi(*blocks.keys, **cache_options) do |missing_key|
|
32
|
+
logger.debug("Chunk cache miss: #{missing_key}")
|
33
|
+
|
32
34
|
capture do
|
33
|
-
blocks[missing_key]
|
35
|
+
block, context = *blocks[missing_key]
|
36
|
+
block.call(*context)
|
34
37
|
end
|
35
38
|
end
|
36
39
|
|
37
40
|
# Then we replace the placeholders with our new compiled template data
|
38
41
|
chunks.each do |key, chunk|
|
42
|
+
logger.debug("Chunk key replacement: #{key}")
|
43
|
+
|
39
44
|
big_ol_strang.gsub!(key, (chunk || ""))
|
40
45
|
end
|
41
46
|
|
@@ -46,14 +51,14 @@ module ChunkyCache
|
|
46
51
|
# and instead returns just a placeholder string for replacement
|
47
52
|
# at the end of the `chunky_cache` run.
|
48
53
|
#
|
49
|
-
# @param
|
54
|
+
# @param context [*Object] one or multiple objects which respond to `#cache_key` or convert to strings
|
50
55
|
# @return [String] the placeholder key
|
51
|
-
def cache_chunk(*
|
56
|
+
def cache_chunk(*context, &block)
|
52
57
|
raise MissingChunkyCacheError if @chunky_key_blocks.nil?
|
53
58
|
|
54
|
-
key =
|
59
|
+
key = context.map { |k| k.try(:cache_key) || k.to_s }.unshift(template_root_key).join(":")
|
55
60
|
|
56
|
-
@chunky_key_blocks[template_root_key][key] = block
|
61
|
+
@chunky_key_blocks[template_root_key][key] = [block, context]
|
57
62
|
|
58
63
|
return key
|
59
64
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chunky_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert May
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-03-
|
11
|
+
date: 2021-03-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|