cachengue 0.2.0 → 0.2.1

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: 1d61f4fc468d164194855289a59933c239687ba325f33470816ab45dbf383a50
4
- data.tar.gz: 68fc8ff64e399aca19acc01fbf20272537a67a86c08722967cf7de2e7f19ba0a
3
+ metadata.gz: 2d9a4ba2667c81c7f2c04fc2ee56f742853fb1e6e218a26e21603be01c488379
4
+ data.tar.gz: 3e460d20577417ce8b277916cd0bc942fd970fdf2a63db99fa2f5623fcc98142
5
5
  SHA512:
6
- metadata.gz: edda0834cac796434c6f0e9df1137323f7cd6c1a5ab7713a5ef8fdb251caa8d04682f132ea22ff06a18058d5ec52a8370b6b5b00afb0cb533f4b112a810af2db
7
- data.tar.gz: ea1e368ed2bc7897c66ec6cbf9a20e82489750d41aea1ee4b1d3bf4fef1779009ffe080d69589e40ec3fe7b85616d48ec410cd1c0039258c01ad112fa1e25f06
6
+ metadata.gz: 4e41ed83b12ca627832435ad0a5ef18574635f55a6d4625f663aa17a4f6fb70c178bd15fc5c86de308d60cf8ee5aaafdafa1ad05c11439962a13fbdfbfc68cfc
7
+ data.tar.gz: 3ab80002c30016a4566f1330502387c17bfb4ad3853e879bc7509ea887293b596bde9b33033d5f3052e7746363c5c970ce7ba57ff935ea85b538d8ab1d847f53
data/.gitignore CHANGED
@@ -10,3 +10,6 @@
10
10
 
11
11
  # rspec failure tracking
12
12
  .rspec_status
13
+
14
+ # Gem files
15
+ *.gem
@@ -12,10 +12,10 @@ GEM
12
12
  i18n (>= 0.7, < 2)
13
13
  minitest (~> 5.1)
14
14
  tzinfo (~> 1.1)
15
- concurrent-ruby (1.0.5)
15
+ concurrent-ruby (1.1.3)
16
16
  i18n (1.1.1)
17
17
  concurrent-ruby (~> 1.0)
18
- minitest (5.3.5)
18
+ minitest (5.11.3)
19
19
  rake (10.5.0)
20
20
  thread_safe (0.3.6)
21
21
  tzinfo (1.2.5)
@@ -8,13 +8,34 @@ module Cachengue
8
8
 
9
9
  module_function
10
10
 
11
- def format_key(namespace, method_name, args)
11
+ def format_key_by_options(namespace, method_name, args, options)
12
+ format_key(
13
+ options[:namespace] || namespace,
14
+ options[:key] || options[:method] || method_name,
15
+ options[:args] || args,
16
+ options[:hash_args]
17
+ )
18
+ end
19
+
20
+ def format_key(namespace, method_name, args, args_hash)
12
21
  args = args.is_a?(Array) ? args.map(&:as_json) : [args.as_json]
22
+ args = Digest::SHA256.base64digest(String(args)) if args_hash
13
23
  namespace = key_prefix(namespace)
14
24
 
15
25
  "#{namespace}:#{method_name}:#{args}"
16
26
  end
17
27
 
28
+ def format_options(options)
29
+ options.without(
30
+ :args,
31
+ :key,
32
+ :args_hash,
33
+ :method,
34
+ :multi,
35
+ :namespace
36
+ )
37
+ end
38
+
18
39
  def key_prefix(namespace = nil)
19
40
  if namespace.present?
20
41
  "cachengue:#{namespace.to_s.underscore}"
@@ -47,7 +68,8 @@ module Cachengue
47
68
  key = format_key(
48
69
  options.fetch(:namespace),
49
70
  options[:key] || options.fetch(:method),
50
- options.fetch(:args)
71
+ options.fetch(:args),
72
+ options[:args_hash]
51
73
  )
52
74
 
53
75
  clear_keys([key])
@@ -61,29 +83,70 @@ module Cachengue
61
83
  end
62
84
  end
63
85
 
64
- def fetch(namespace, method_name, args, options = {})
65
- key = format_key(
66
- options[:namespace] || namespace,
67
- options[:key] || options[:method] || method_name,
68
- options[:args] || args
69
- )
86
+ def get_value_by(args)
87
+ value = yield(*args)
70
88
 
71
- options = options.without(
72
- :args,
73
- :key,
74
- :method,
75
- :namespace
76
- )
89
+ if defined?(::ActiveRecord) && value.is_a?(::ActiveRecord::Relation)
90
+ value.to_a
91
+ else
92
+ value
93
+ end
94
+ end
95
+
96
+ def fetch(namespace, method_name, args, options = {}, &block)
97
+ if options[:multi]
98
+ fetch_multi(namespace, method_name, args, options, &block)
99
+ else
100
+ fetch_one(namespace, method_name, args, options, &block)
101
+ end
102
+ end
103
+
104
+ def fetch_one(namespace, method_name, args, options = {}, &block)
105
+ key = format_key_by_options(namespace, method_name, args, options)
106
+ options = format_options(options)
77
107
 
78
108
  Rails.cache.fetch(key, options) do
79
- value = yield(*args)
109
+ get_value_by(args, &block)
110
+ end
111
+ end
112
+
113
+ def fetch_multi(namespace, method_name, args_list, options = {})
114
+ return {} if args_list.empty?
115
+
116
+ args_keys = {}
117
+
118
+ args_list.each do |args|
119
+ key = format_key_by_options(namespace, method_name, args, options)
80
120
 
81
- if defined?(::ActiveRecord) && value.is_a?(::ActiveRecord::Relation)
82
- value.to_a
83
- else
84
- value
85
- end
121
+ args_keys[key] = args
86
122
  end
123
+
124
+ options = format_options(options)
125
+ results = read_multi(args_keys.keys)
126
+
127
+ computed_keys = args_keys.keys - results.keys
128
+ computed_values = computed_keys.map { |key| args_keys.fetch(key) }
129
+ computed = write_multi(computed_keys, computed_values, options, &Proc.new)
130
+
131
+ results.merge!(computed)
132
+ results.transform_keys! { |key| args_keys.fetch(key) }
133
+ results
134
+ end
135
+
136
+ def read_multi(keys)
137
+ Rails.cache.read_multi(*keys)
138
+ end
139
+
140
+ def write_multi(keys, values, options)
141
+ results = {}
142
+
143
+ keys.each_with_index do |key, index|
144
+ results[key] = get_value_by(values.fetch(index), &Proc.new)
145
+ end
146
+
147
+ Rails.cache.write_multi(results, options)
148
+
149
+ results
87
150
  end
88
151
  end
89
152
  end
@@ -11,10 +11,18 @@ module Cachengue
11
11
  end
12
12
  end
13
13
 
14
- def module_cachengue(method_name, options = {})
14
+ def module_cachengue(method_name, options = {}, &block)
15
15
  singleton_class.instance_eval do
16
16
  define_method(method_name) do |*args|
17
- Caching.fetch(self, method_name, args, options, &Proc.new)
17
+ Caching.fetch_one(self, method_name, args, options, &block)
18
+ end
19
+ end
20
+
21
+ return unless options[:multi]
22
+
23
+ singleton_class.instance_eval do
24
+ define_method("#{method_name}_multi") do |*args|
25
+ Caching.fetch_multi(self, method_name, args, options, &block)
18
26
  end
19
27
  end
20
28
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cachengue
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cachengue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - shipnow developers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-29 00:00:00.000000000 Z
11
+ date: 2019-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  version: '0'
108
108
  requirements: []
109
109
  rubyforge_project:
110
- rubygems_version: 2.7.6
110
+ rubygems_version: 2.7.7
111
111
  signing_key:
112
112
  specification_version: 4
113
113
  summary: cache gem for ruby on rails