redis-memo 0.0.0.beta.1 → 0.0.0.beta.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: 3e64eaaecf192519453c1accb0d10ca25ed2cf8274c10b6cb128a14e9a2e616e
4
- data.tar.gz: fd98fce04f77660b8fc5960e491695504ef438394a882fb80d36b706038d10e5
3
+ metadata.gz: bfc639839800aa1290622d733d9fd23619de8ccd63f2129e4570eb41a295884b
4
+ data.tar.gz: 26e2d86776244ba5cb0a8cebbc18e468ccf737888de9990c3a2bb9fc931bd4d5
5
5
  SHA512:
6
- metadata.gz: 20ca8f3efa4ca8591d073960c5fd5b9c8fbcd920b58ae6fe378ec3511af082c2cf964c72cda93739b6bef7294d5be9ceb77eb6000cb3df567f18b2bd581cf4c8
7
- data.tar.gz: 1c485024e2eb3f541139bbd8f136279f5b1010b7b81ab30929d2e87a8917c4f0d8fb35d2ae39d94fd14d2f918b6476f53d589026f4412a8b118d6b2b6d732a93
6
+ metadata.gz: 3c6950a588fbf448d4b88e126ee9b32753ef314225d3aa5f69430d7e386a9a1085b517a08f4f6de2725810f35b493c20e2af62729f547891bcc78aaf4ce73ef2
7
+ data.tar.gz: 1e380d3ff6d457a6c93dcdebcbf9be072e919b2f5ff25377f169287ed8ac6c379f9a8db29a494d9174cff91e815e680201970ece43baba69b627678b628a3d77
@@ -12,25 +12,37 @@ class RedisMemo::Memoizable::Dependency
12
12
  @nodes.values
13
13
  end
14
14
 
15
- def depends_on(memo_or_model, **conditions)
16
- if !memo_or_model.is_a?(RedisMemo::Memoizable)
15
+ def depends_on(dependency, **conditions)
16
+ case dependency
17
+ when self.class
18
+ nodes.merge!(dependency.nodes)
19
+ when RedisMemo::Memoizable
20
+ memo = dependency
21
+ return if nodes.include?(memo.cache_key)
22
+ nodes[memo.cache_key] = memo
23
+
24
+ if memo.depends_on
25
+ # Extract dependencies from the current memoizable and recurse
26
+ instance_exec(&memo.depends_on)
27
+ end
28
+ when UsingActiveRecord
17
29
  [
18
- memo_or_model.redis_memo_class_memoizable,
19
- RedisMemo::MemoizeQuery.create_memo(memo_or_model, **conditions),
30
+ dependency.redis_memo_class_memoizable,
31
+ RedisMemo::MemoizeQuery.create_memo(dependency, **conditions),
20
32
  ].each do |memo|
21
33
  nodes[memo.cache_key] = memo
22
34
  end
23
-
24
- return
35
+ else
36
+ raise(
37
+ RedisMemo::ArgumentError,
38
+ "Invalid dependency #{dependency}"
39
+ )
25
40
  end
41
+ end
26
42
 
27
- memo = memo_or_model
28
- return if nodes.include?(memo.cache_key)
29
- nodes[memo.cache_key] = memo
30
-
31
- if memo.depends_on
32
- # Extract dependencies from the current memoizable and recurse
33
- instance_exec(&memo.depends_on)
43
+ class UsingActiveRecord
44
+ def self.===(dependency)
45
+ RedisMemo::MemoizeQuery.using_active_record?(dependency)
34
46
  end
35
47
  end
36
48
  end
@@ -40,6 +40,20 @@ module RedisMemo::MemoizeMethod
40
40
  end
41
41
 
42
42
  alias_method method_name, method_name_with_memo
43
+
44
+ @__redis_memo_method_dependencies ||= Hash.new
45
+ @__redis_memo_method_dependencies[method_name] = depends_on
46
+
47
+ define_method :dependency_of do |method_name, *method_args|
48
+ method_depends_on = self.class.instance_variable_get(:@__redis_memo_method_dependencies)[method_name]
49
+ unless method_depends_on
50
+ raise(
51
+ RedisMemo::ArgumentError,
52
+ "#{method_name} is not a memoized method"
53
+ )
54
+ end
55
+ RedisMemo::MemoizeMethod.extract_dependencies(self, *method_args, &method_depends_on)
56
+ end
43
57
  end
44
58
 
45
59
  def self.method_id(ref, method_name)
@@ -49,15 +63,19 @@ module RedisMemo::MemoizeMethod
49
63
  "#{class_name}#{is_class_method ? '::' : '#'}#{method_name}"
50
64
  end
51
65
 
66
+ def self.extract_dependencies(ref, *method_args, &depends_on)
67
+ dependency = RedisMemo::Memoizable::Dependency.new
68
+
69
+ # Resolve the dependency recursively
70
+ dependency.instance_exec(ref, *method_args, &depends_on)
71
+ dependency
72
+ end
73
+
52
74
  def self.method_cache_keys(future_contexts)
53
75
  memos = Array.new(future_contexts.size)
54
76
  future_contexts.each_with_index do |(ref, _, method_args, depends_on), i|
55
77
  if depends_on
56
- dependency = RedisMemo::Memoizable::Dependency.new
57
-
58
- # Resolve the dependency recursively
59
- dependency.instance_exec(ref, *method_args, &depends_on)
60
-
78
+ dependency = extract_dependencies(ref, *method_args, &depends_on)
61
79
  memos[i] = dependency.memos
62
80
  end
63
81
  end
@@ -44,11 +44,15 @@ if defined?(ActiveRecord)
44
44
  end
45
45
 
46
46
  def self.using_active_record!(model_class)
47
- unless model_class.respond_to?(:<) && model_class < ActiveRecord::Base
47
+ unless using_active_record?(model_class)
48
48
  raise RedisMemo::ArgumentError, "'#{model_class.name}' does not use ActiveRecord"
49
49
  end
50
50
  end
51
51
 
52
+ def self.using_active_record?(model_class)
53
+ model_class.respond_to?(:<) && model_class < ActiveRecord::Base
54
+ end
55
+
52
56
  @@memoized_columns = Hash.new { |h, k| h[k] = [Set.new, Set.new] }
53
57
 
54
58
  def self.memoized_columns(model_or_table, editable_only: false)
@@ -68,26 +72,23 @@ if defined?(ActiveRecord)
68
72
  )
69
73
  end
70
74
 
71
- extra_props.each do |key, values|
75
+ extra_props.each do |key, value|
72
76
  # The data type is ensured by the database, thus we don't need to cast
73
77
  # types here for better performance
74
78
  column_name = key.to_s
75
- values = [values] unless values.is_a?(Enumerable)
76
79
  extra_props[key] =
77
80
  if model_class.defined_enums.include?(column_name)
78
81
  enum_mapping = model_class.defined_enums[column_name]
79
- values.map do |value|
80
- # Assume a value is a converted enum if it does not exist in the
81
- # enum mapping
82
- (enum_mapping[value.to_s] || value).to_s
83
- end
82
+ # Assume a value is a converted enum if it does not exist in the
83
+ # enum mapping
84
+ (enum_mapping[value.to_s] || value).to_s
84
85
  else
85
- values.map(&:to_s)
86
+ value.to_s
86
87
  end
87
88
  end
88
89
 
89
90
  RedisMemo::Memoizable.new(
90
- __redis_memo_memoize_query_model_class_name__: model_class.name,
91
+ __redis_memo_memoize_query_table_name__: model_class.table_name,
91
92
  **extra_props,
92
93
  )
93
94
  end
@@ -33,7 +33,7 @@ class RedisMemo::MemoizeQuery::Invalidation
33
33
  # Methods that won't trigger model callbacks
34
34
  # https://guides.rubyonrails.org/active_record_callbacks.html#skipping-callbacks
35
35
  %i(
36
- import
36
+ import import!
37
37
  decrement_counter
38
38
  delete_all delete_by
39
39
  increment_counter
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-memo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0.beta.1
4
+ version: 0.0.0.beta.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chan Zuckerberg Initiative