chunky_cache 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d2311b65e1641bf1f6b38065916c54d4531a8a97f5c564543d3de0f2d9d5dcef
4
- data.tar.gz: 11ec8029a7b365d7bd69ed8c95758feabd58b6696dd495a4dde121072789d4aa
3
+ metadata.gz: 900e71f999ba2cf5a9996aad4a700b7c22df75e1a240a5fc9e4fc7b2df88e39e
4
+ data.tar.gz: 42e100f7f0aef3b9bc932dded4f7d3556bf007ed0057b28932cde7a902025f34
5
5
  SHA512:
6
- metadata.gz: 9474cc4a678990ae89c719df38801edc38cb7b14c670540830836a77a5b3d5b9acc63f0fb2ad650bc0d5b5fea7ef19d2a8001c1c68b9c30b26f900ff94ee41bf
7
- data.tar.gz: 1722b8572dd47cb2c9af38108009392b340f259d56900b98907c158373404cd0f8ab610faf4edd5a67ecc04657849772dfde481560c33fc93c829dc864b3ef58
6
+ metadata.gz: cd58b7c0267f3a015a344e68354a83e4b6a74bac7774795d83cfc137331fb7ee750ad7b3732cf66095f8034636fc1959c7d7390ffb840ca5a484cd25cd0b68af
7
+ data.tar.gz: 4ce371b52d6d27a4b8aafd1cbaa0edcd2a2620574fa105dda0a7db3e0e9810df6e9dc8f0b27484dc5538648c7e6a2fe624f6f769624a270dd85f4d93b5f00be7
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- chunky_cache (0.1.0)
4
+ chunky_cache (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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.
@@ -1,3 +1,3 @@
1
1
  module ChunkyCache
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -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].call
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 keys [*Object] one or multiple objects which respond to `#cache_key` or convert to strings
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(*keys, &block)
56
+ def cache_chunk(*context, &block)
52
57
  raise MissingChunkyCacheError if @chunky_key_blocks.nil?
53
58
 
54
- key = keys.map! { |k| k.try(:cache_key) || k.to_s }.unshift(template_root_key).join(":")
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.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-05 00:00:00.000000000 Z
11
+ date: 2021-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails