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 +4 -4
- data/Gemfile.lock +2 -2
- data/README.md +37 -0
- data/lib/cache_json/base.rb +1 -1
- data/lib/cache_json/version.rb +1 -1
- data/lib/cache_json/worker.rb +49 -40
- 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: d97faf1cadc565cb04a69dee9bd89df23574d72e9b8b412c2e55dffe201d88db
|
4
|
+
data.tar.gz: 69e444bf0badc19dc320d644ca4929712d23552537d6493845aab0cb682dff59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2eaae958e037c974bcc9b7042d35b75165c331976989dad8837aee846e60d176a81c4595971f174e5ba25d78f8af9c5fd2035743e0f2ebbbe49118cfbd741e8d
|
7
|
+
data.tar.gz: b6cd26c9c6e4318844be79eb4193af22318feabed40054e5d0b4b28c2cf705d3abd6c32927f5e6b4853eda4838c16ff07a6377b90ffbfd005eef94f378e0858f
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
cache_json (1.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.
|
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.
|
data/lib/cache_json/base.rb
CHANGED
data/lib/cache_json/version.rb
CHANGED
data/lib/cache_json/worker.rb
CHANGED
@@ -22,62 +22,71 @@ module CacheJSON
|
|
22
22
|
if klass
|
23
23
|
klass.new.refresh_cache!(args: args)
|
24
24
|
else
|
25
|
-
|
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
|
32
|
-
|
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
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
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
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
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
|
-
|
80
|
-
|
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.
|
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-
|
11
|
+
date: 2020-10-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|