cache_json 1.2.2 → 1.2.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7d55f2663e89b29cd7a1722791d2df406c474801ba2fdcb1fc3345b1ef09ec02
4
- data.tar.gz: 3622185a6fd8bb95afa5a1aee4db627e85e6ac9655f74ac01b1f0e69147ef6f9
3
+ metadata.gz: d97faf1cadc565cb04a69dee9bd89df23574d72e9b8b412c2e55dffe201d88db
4
+ data.tar.gz: 69e444bf0badc19dc320d644ca4929712d23552537d6493845aab0cb682dff59
5
5
  SHA512:
6
- metadata.gz: a6e7121140320e4bc2bcf592a5917a8493043f639611d91e765a669c236f035ab2eab2a6566b1e23451dffb1327fecb0e8b6ad6948b6940e313f8f951d627276
7
- data.tar.gz: 50bf2925a8d95546d7c21547e283ac85a4e642cbf98cb198384a3baa4ba8a1b4abc9c2aeb021437022a3ad72557f63f390613ad0b5bb582c3592c3e79d0318bd
6
+ metadata.gz: 2eaae958e037c974bcc9b7042d35b75165c331976989dad8837aee846e60d176a81c4595971f174e5ba25d78f8af9c5fd2035743e0f2ebbbe49118cfbd741e8d
7
+ data.tar.gz: b6cd26c9c6e4318844be79eb4193af22318feabed40054e5d0b4b28c2cf705d3abd6c32927f5e6b4853eda4838c16ff07a6377b90ffbfd005eef94f378e0858f
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cache_json (1.2.2)
4
+ cache_json (1.2.3)
5
5
  redis (>= 3.3.5, < 5)
6
6
 
7
7
  GEM
@@ -15,7 +15,7 @@ GEM
15
15
  parallel (1.19.1)
16
16
  parser (2.7.0.3)
17
17
  ast (~> 2.4.0)
18
- rack (2.2.2)
18
+ rack (2.2.3)
19
19
  rack-protection (2.0.8.1)
20
20
  rack
21
21
  rainbow (3.0.0)
data/README.md CHANGED
@@ -54,6 +54,43 @@ CacheJSON.configure do |config|
54
54
  end
55
55
  ```
56
56
 
57
+ ## Automatic refreshing (Sidekiq)
58
+
59
+ There is a simple Sidekiq job that lets you pre-compute selected classes with specified ranges of arguments. All you have to do is add a `refresh` option:
60
+
61
+ ```ruby
62
+ class ExpensiveJob
63
+
64
+ include CacheJSON::Base
65
+
66
+
67
+ cache_json_options(
68
+ time_to_expire: 1.hour,
69
+ refresh: {
70
+ arguments: {
71
+ first: (5..10),
72
+ second: ['one option', 'another option'],
73
+ third: 'the only option',
74
+ fourth: -> { ['proc result'] }
75
+ }
76
+ }
77
+ )
78
+ ...
79
+ end
80
+ ```
81
+
82
+ The Sidekiq job will take the Cartesian product of all the argument ranges/arrays (all the combinations).
83
+
84
+ We leave it to you to schedule the job. If you're using https://github.com/moove-it/sidekiq-scheduler, you can do something like this:
85
+
86
+ ```yml
87
+ cache_json_worker:
88
+ every: "20s"
89
+ class: CacheJSON::Worker
90
+ ```
91
+
92
+ Whenever the worker runs, it checks which results have expired, and refreshes only those.
93
+
57
94
  ## Development
58
95
 
59
96
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -8,7 +8,7 @@ module CacheJSON
8
8
  base.extend(ClassMethods)
9
9
  end
10
10
 
11
- def results(args)
11
+ def results(args = {})
12
12
  raise ArgumentError, 'Must use keyword arguments' unless args.is_a?(Hash)
13
13
 
14
14
  options = self.class.cache_json_full_options
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CacheJSON
4
- VERSION = '1.2.2'
4
+ VERSION = '1.2.3'
5
5
  end
@@ -22,62 +22,71 @@ module CacheJSON
22
22
  if klass
23
23
  klass.new.refresh_cache!(args: args)
24
24
  else
25
- generate_workers
25
+ AllPermutations.new.results.each do |perm|
26
+ if should_refresh?(perm[:klass], perm[:args])
27
+ CacheJSON::Worker.new.perform(klass: perm[:klass], args: perm[:args])
28
+ end
29
+ end
26
30
  end
27
31
  end
28
32
 
29
33
  private
30
34
 
31
- def generate_workers
32
- all_cache_classes.each do |klass|
33
- all_argument_permutations(klass).each do |args|
34
- CacheJSON::Worker.new.perform(klass: klass, args: args) if should_refresh?(klass, args)
35
- end
36
- end
35
+ def should_refresh?(klass, args)
36
+ !klass.new.check_cache(args: args)
37
37
  end
38
38
 
39
- def all_cache_classes
40
- # TODO: make this more efficient
41
- ObjectSpace.each_object(Class).select do |klass|
42
- klass.included_modules.include? CacheJSON::Base
39
+ class AllPermutations
40
+ def results
41
+ all_cache_classes.flat_map do |klass|
42
+ all_argument_permutations(klass).map do |args|
43
+ {
44
+ klass: klass,
45
+ args: args
46
+ }
47
+ end
48
+ end
43
49
  end
44
- end
45
50
 
46
- def all_argument_permutations(klass)
47
- refresh_options = klass.cache_json_full_options[:refresh]
48
- if refresh_options
49
- all_combinations_with_fixed_points({}, refresh_options[:arguments])
50
- else
51
- []
51
+ def all_cache_classes
52
+ # TODO: make this more efficient
53
+ ObjectSpace.each_object(Class).select do |klass|
54
+ klass.included_modules.include? CacheJSON::Base
55
+ end
52
56
  end
53
- end
54
57
 
55
- def all_combinations_with_fixed_points(fixed_points, full_hash)
56
- non_fixed_points = full_hash.dup.delete_if { |k, _| fixed_points.key?(k) }
57
- if non_fixed_points.empty?
58
- [fixed_points]
59
- else
60
- pivot_key = non_fixed_points.keys.first
61
- values_for_key(non_fixed_points, pivot_key).flat_map do |pivot_key_value|
62
- new_fixed_points = fixed_points.merge(Hash[pivot_key, pivot_key_value])
63
- all_combinations_with_fixed_points(new_fixed_points, full_hash)
58
+ def all_argument_permutations(klass)
59
+ refresh_options = klass.cache_json_full_options[:refresh]
60
+ if refresh_options
61
+ all_combinations_with_fixed_points({}, refresh_options[:arguments])
62
+ else
63
+ []
64
64
  end
65
65
  end
66
- end
67
66
 
68
- def values_for_key(hsh, key)
69
- pivot_key_values = hsh[key]
70
- if pivot_key_values.is_a?(Proc)
71
- pivot_key_values.call
72
- elsif pivot_key_values.is_a?(Range) || pivot_key_values.is_a?(Array)
73
- pivot_key_values
74
- else
75
- [pivot_key_values]
67
+ def all_combinations_with_fixed_points(fixed_points, full_hash)
68
+ non_fixed_points = full_hash.dup.delete_if { |k, _| fixed_points.key?(k) }
69
+ if non_fixed_points.empty?
70
+ [fixed_points]
71
+ else
72
+ pivot_key = non_fixed_points.keys.first
73
+ values_for_key(non_fixed_points, pivot_key).flat_map do |pivot_key_value|
74
+ new_fixed_points = fixed_points.merge(Hash[pivot_key, pivot_key_value])
75
+ all_combinations_with_fixed_points(new_fixed_points, full_hash)
76
+ end
77
+ end
76
78
  end
77
- end
78
79
 
79
- def should_refresh?(klass, args)
80
- !klass.new.check_cache(args: args)
80
+ def values_for_key(hsh, key)
81
+ pivot_key_values = hsh[key]
82
+ if pivot_key_values.is_a?(Proc)
83
+ pivot_key_values.call
84
+ elsif pivot_key_values.is_a?(Range) || pivot_key_values.is_a?(Array)
85
+ pivot_key_values
86
+ else
87
+ [pivot_key_values]
88
+ end
89
+ end
81
90
  end
82
91
  end
83
92
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cache_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Gut
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-05-14 00:00:00.000000000 Z
11
+ date: 2020-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis