db_memoize 0.3.6 → 0.3.7

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: a1659eec50470121a2d590b1abef910d5fa5c3888239e75c3249db78f68b87c8
4
- data.tar.gz: 79f73004fe585ef2f71d39ba95a16f7b2a98e78a9fad4c8ca110e8db9e928819
3
+ metadata.gz: 97b2b4cd0624780588e5aaa90b93bf26e5f272246b043e076a282461e036188f
4
+ data.tar.gz: 8d440f2716fad476dbf95dd94698e77cb90099e9be330ffff599b788d1e2be1c
5
5
  SHA512:
6
- metadata.gz: d8682fd173bd5fa2e89345a5df03b2fe833f2ce1dcd9b95596a137a08ad6c71b162f01b5707ce8750f44aecdf3128ec93bf3d44c9c24b8629882ffc49260414f
7
- data.tar.gz: c5826ed544ce66c21588f210c72fc2825a60fb2c46c03617d46d5ff2d1f6235f8278aa865f99cbd5cb8881e9669a1c22788988dcfb1b02fffe1f0546eabba493
6
+ metadata.gz: 063a5479a255e730d6fc4c2f9b89fc007484662ca2e852536603f974daa1fa8017d783ac5bfebe776b39096c3a70e7d65b2266c1efc80c75562ecb94d5f6da69
7
+ data.tar.gz: a52b4f47e0a15e82769e2188467c543580ad45203e42817b3341e24f11413af7719f1122b3255eeb59c05c9733fdad1b9c0949d53495211de6a7f14249da9eb6
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.6
1
+ 0.3.7
@@ -10,11 +10,5 @@ module DbMemoize
10
10
 
11
11
  records_or_ids.first.is_a?(ActiveRecord::Base) ? records_or_ids.map(&:id) : records_or_ids
12
12
  end
13
-
14
- def log(model, method_name, msg)
15
- DbMemoize.logger.send(DbMemoize.log_level) do
16
- "DbMemoize <#{model.class.name}##{model.id}>##{method_name} - #{msg}"
17
- end
18
- end
19
13
  end
20
14
  end
@@ -8,21 +8,15 @@ module DbMemoize
8
8
  memoizable = !changed? && persisted?
9
9
  return send("#{method_name}_without_memoize") unless memoizable
10
10
 
11
- value = nil
12
- cached_value = find_memoized_value(method_name)
11
+ memoized_value = find_memoized_value(method_name)
13
12
 
14
- if cached_value
15
- value = cached_value.value
16
- Helpers.log(self, method_name, 'cache hit')
13
+ if memoized_value
14
+ memoized_value.value
17
15
  else
18
- time = ::Benchmark.realtime do
19
- value = send("#{method_name}_without_memoize")
20
- create_memoized_value(method_name, value)
21
- end
22
- Helpers.log(self, method_name, "cache miss. took #{Kernel.format '%.2f msecs', time * 1_000}")
16
+ value = send("#{method_name}_without_memoize")
17
+ create_memoized_value(method_name, value)
18
+ value
23
19
  end
24
-
25
- value
26
20
  end
27
21
 
28
22
  def unmemoize(method_name = :all)
@@ -51,6 +45,7 @@ module DbMemoize
51
45
  self.class.transaction do
52
46
  ::DbMemoize::Value.fast_create self.class.table_name, id, method_name, value
53
47
  @association_cache.delete :memoized_values
48
+ value
54
49
  end
55
50
  end
56
51
 
@@ -1,9 +1,5 @@
1
1
  module DbMemoize
2
2
  class Railtie < ::Rails::Railtie
3
- initializer 'Rails logger' do
4
- DbMemoize.logger = Rails.logger
5
- end
6
-
7
3
  rake_tasks do
8
4
  load 'tasks/clear.rake'
9
5
  load 'tasks/warmup.rake'
@@ -20,6 +20,7 @@ module DbMemoize
20
20
  when false then self.val_boolean = value
21
21
  when true then self.val_boolean = value
22
22
  when nil then :nop
23
+ else raise "Unsupported type #{value.class.name.inspect}, for DbMemoize"
23
24
  end
24
25
  end
25
26
 
@@ -43,36 +44,62 @@ module DbMemoize
43
44
  SQL
44
45
  end
45
46
 
46
- def self.fast_create(entity_table_name, id, method_name, value)
47
+ def self.fast_create(entity_table_name, entity_id, method_name, value)
47
48
  method_name = method_name.to_s
48
49
 
49
50
  # clear out old entry (if any). This makes sure that any existing value
50
51
  # is cleared out properly.
51
52
  SQL.ask "DELETE FROM #{table_name} WHERE(entity_table_name, entity_id, method_name) = ($1, $2, $3)",
52
- entity_table_name, id, method_name
53
-
54
- dest_column = case value
55
- when String then :val_string
56
- when Integer then :val_integer
57
- when Float then :val_float
58
- when Time then :val_time
59
- when false then :val_boolean
60
- when true then :val_boolean
61
- when nil then :val_nil
62
- when Hash then :val_object
63
- when Array then :val_object
64
- else
65
- raise "Unsupported value of type #{value.class.name}: #{value.inspect}"
66
- end
67
-
68
- value = JSON.generate(value) if dest_column == :val_object
53
+ entity_table_name, entity_id, method_name
54
+
55
+ column = case value
56
+ when String then :val_string
57
+ when Integer then :val_integer
58
+ when Float then :val_float
59
+ when Time then :val_time
60
+ when false then :val_boolean
61
+ when true then :val_boolean
62
+ when nil then :val_nil
63
+ when Hash then :val_object
64
+ when Array then :val_object
65
+ else
66
+ raise "Unsupported value of type #{value.class.name}: #{value.inspect}"
67
+ end
68
+
69
+ value = JSON.generate(value) if column == :val_object
70
+
71
+ # it looks like Simple::SQL somehow drops subsecond resolutions from
72
+ # time objects. AR does not, though. So, for time values, we use the
73
+ # ActiveRecord method; for everything else we use Simple::SQL (for
74
+ # performance reasons: it is 10 times as fast.)
75
+ if column != :val_time
76
+ simple_sql_create_value column, entity_table_name: entity_table_name, entity_id: entity_id,
77
+ method_name: method_name, value: value
78
+ else
79
+ ar_create_value column, entity_table_name: entity_table_name, entity_id: entity_id,
80
+ method_name: method_name, value: value
81
+ end
82
+ end
83
+
84
+ def self.simple_sql_create_value(column, entity_table_name:, entity_id:, method_name:, value:)
69
85
  sql = <<~SQL.freeze
70
86
  INSERT INTO #{table_name}
71
- (entity_table_name, entity_id, method_name, #{dest_column}, created_at)
87
+ (entity_table_name, entity_id, method_name, #{column}, created_at)
72
88
  VALUES($1,$2,$3,$4,NOW())
73
89
  SQL
74
90
 
75
- SQL.ask sql, entity_table_name, id, method_name, value
91
+ SQL.ask sql, entity_table_name, entity_id, method_name, value
92
+ end
93
+
94
+ def self.ar_create_value(column, entity_table_name:, entity_id:, method_name:, value:)
95
+ data = {
96
+ :entity_table_name => entity_table_name,
97
+ :entity_id => entity_id,
98
+ :method_name => method_name,
99
+ column => value
100
+ }
101
+
102
+ create!(data)
76
103
  end
77
104
  end
78
105
  end
data/lib/db_memoize.rb CHANGED
@@ -10,15 +10,4 @@ require 'db_memoize/model'
10
10
  require 'db_memoize/railtie' if defined?(Rails)
11
11
 
12
12
  module DbMemoize
13
- class << self
14
- attr_writer :logger, :log_level
15
-
16
- def logger
17
- @logger ||= ::Logger.new(STDOUT)
18
- end
19
-
20
- def log_level
21
- @log_level ||= :debug
22
- end
23
- end
24
13
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db_memoize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - johannes-kostas goetzinger
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-10-08 00:00:00.000000000 Z
11
+ date: 2018-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: simple-sql