cache_keeper 0.4.0 → 0.4.1

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: 04c55e34ac5e0676e81456a04f7edae71cf0c79c6cd5be9e135c56561f6439be
4
- data.tar.gz: 6f9d9bc0bd404cd350f1cf7ff48b8c59bbff60dbeeda754c052ca22457720b8a
3
+ metadata.gz: bc9efe0608912e0c97f6f89659ad73e64588e33eb384701afe0c52afb981c568
4
+ data.tar.gz: 22b645e8a15de837f4d72de70bc64a1d966c9ce96f9ada07a6fb2f7ba6a0d32f
5
5
  SHA512:
6
- metadata.gz: 1eec12fc16cf629cbe1b3502918b67d6d420bb812c1ed88088a809b07faf31ec981dfddb0d094507f0cd9522238134b1bc60ff011f0d99a12339144a5ba0a3d2
7
- data.tar.gz: 6a93852da524a5e478166bc24d0bee53973a3343a8b55c2574ed67259f3e5ffc7f927b31bd35e257fe0107073e69fc32a99e608614b07ab736fa103e8ce33adc
6
+ metadata.gz: e60f463b994d17a08f047cd00cd7ef7114940cf5458a6c42d25efacf1968d93a01b2b9cbd6c1bad23252a9b357f88cae5909b291b24756a566ebe95efb0cf578
7
+ data.tar.gz: 25e87146ea75af98f0112aa8ba6acb730d6314962357589e4b8c0a540a728e9e59c2ebecb4764612f1eba375a6ad45069686ad5e3934bec05cbe31781522eee6
data/README.md CHANGED
@@ -30,8 +30,8 @@ CacheKeeper provides a `caches` method that will cache the result of the methods
30
30
 
31
31
  ```ruby
32
32
  class Recording < ApplicationRecord
33
- caches :slow_method, :really_slow_method, expires_in: 1.hour, must_revalidate: true
34
- caches :incredibly_slow_method, expires_in: 2.hours, key: -> { "custom-key/#{id}" }
33
+ caches :slow_method, :really_slow_method, expires_in: 1.hour
34
+ caches :incredibly_slow_method, expires_in: 2.hours, must_revalidate: true
35
35
 
36
36
  def slow_method
37
37
  ...
@@ -51,17 +51,29 @@ It's automatically available in your ActiveRecord models and in your controllers
51
51
 
52
52
  By default, it will immediately run the method call if it hasn't been cached before. The next time it is called, it will return the cached value if it hasn't expired yet. If it has expired, it will enqueue a job to refresh the cache in the background and return the stale value in the meantime. You can avoid returning stale values by setting `must_revalidate: true` in the options.
53
53
 
54
- CacheKeeper will compose cache keys from the name of the method and the instance's `cache_key` if it's defined or the name of the class otherwise. You can pass a `key` option to customize the cache key if you need it. It accepts [the same values](https://guides.rubyonrails.org/caching_with_rails.html#cache-keys) as `Rails.cache.fetch`, as well as procs or lambdas in case you need access to the instance.
55
-
56
54
  It's important to note that it will only work with methods that don't take any arguments.
57
55
 
56
+ ### Cache key
57
+
58
+ CacheKeeper will compose cache keys from the name of the method and the instance's `cache_key` if it's defined or the name of the class otherwise. You can pass a `key` option to customize the cache key if you need it. It accepts [the same values](https://guides.rubyonrails.org/caching_with_rails.html#cache-keys) as `Rails.cache.fetch`, as well as procs or lambdas in case you need access to the instance:
59
+
60
+ ```ruby
61
+ class NebulaNoodleTwister
62
+ caches :twist_noodles, :dish_of_the_day, key: ->(method_name) { [:recoding, id, method_name] }
63
+ caches :synchronize_taste_buds, key: -> { [:recoding, id, :synchronize_taste_buds] }
64
+ caches :space_soup_simulation, key: :space_soup_simulation
65
+
66
+ ...
67
+ end
68
+ ```
69
+
58
70
  ### Serialization
59
71
 
60
72
  CacheKeeper needs to pass the instance on which the cached method is called along to the refresh job. As any other job argument, ActiveJob requires it to be serializable. ActiveRecord instances are serializable by default, but controllers, POROs and other classes are not. CacheKeeper provides a `serializer` option that will work in most cases:
61
73
 
62
74
  ```ruby
63
- class Example
64
- # Generate a new instance using an empty initializer (Example.new)
75
+ class QuantumQuackerator
76
+ # Generate a new instance using an empty initializer (QuantumQuackerator.new)
65
77
  # Useful for controllers and for POROs with no arguments
66
78
  caches :slow_method, serializer: :new_instance
67
79
 
@@ -39,8 +39,14 @@ class CacheKeeper::CachedMethod
39
39
  end
40
40
 
41
41
  def cache_key(target)
42
- if options[:key].present?
43
- options[:key].is_a?(Proc) ? target.instance_exec(&options[:key]) : options[:key]
42
+ if options[:key].is_a?(Proc)
43
+ if options[:key].arity == 1
44
+ target.instance_exec(method_name, &options[:key])
45
+ else
46
+ target.instance_exec(&options[:key])
47
+ end
48
+ elsif options[:key].present?
49
+ options[:key]
44
50
  else
45
51
  target.respond_to?(:cache_key) ? ["CacheKeeper", target, method_name] : ["CacheKeeper", klass, method_name]
46
52
  end
@@ -1,3 +1,3 @@
1
1
  module CacheKeeper
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -12,6 +12,41 @@ class CacheKeeper::CachedMethodTest < ActiveSupport::TestCase
12
12
  assert cache_has_key? "CacheKeeper/recordings/#{recording.id}/another_method"
13
13
  end
14
14
 
15
+ test ":key option accepts arrays" do
16
+ cached_method = manager.handle(Recording, :another_method, key: ["QuantumQuackerator", "dimensional_duckling"])
17
+ cache_key = cached_method.send :cache_key, Recording.new
18
+
19
+ assert_equal ["QuantumQuackerator", "dimensional_duckling"], cache_key
20
+ end
21
+
22
+ test ":key option accepts procs with no arguments" do
23
+ cached_method = manager.handle(Recording, :another_method, key: proc { 123 })
24
+ cache_key = cached_method.send :cache_key, Recording.new
25
+
26
+ assert_equal 123, cache_key
27
+ end
28
+
29
+ test ":key option accepts procs with an argument" do
30
+ cached_method = manager.handle(Recording, :another_method, key: proc { |method_name| [method_name, 123] })
31
+ cache_key = cached_method.send :cache_key, Recording.new
32
+
33
+ assert_equal [:another_method, 123], cache_key
34
+ end
35
+
36
+ test ":key option accepts lambdas with no arguments" do
37
+ cached_method = manager.handle(Recording, :another_method, key: -> { 123 })
38
+ cache_key = cached_method.send :cache_key, Recording.new
39
+
40
+ assert_equal 123, cache_key
41
+ end
42
+
43
+ test ":key option accepts lambdas with an argument" do
44
+ cached_method = manager.handle(Recording, :another_method, key: ->(method_name) { [method_name, 123] })
45
+ cache_key = cached_method.send :cache_key, Recording.new
46
+
47
+ assert_equal [:another_method, 123], cache_key
48
+ end
49
+
15
50
  private
16
51
 
17
52
  def manager
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cache_keeper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Zamuner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-25 00:00:00.000000000 Z
11
+ date: 2023-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails