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 +4 -4
- data/VERSION +1 -1
- data/lib/db_memoize/helpers.rb +0 -6
- data/lib/db_memoize/model.rb +7 -12
- data/lib/db_memoize/railtie.rb +0 -4
- data/lib/db_memoize/value.rb +47 -20
- data/lib/db_memoize.rb +0 -11
- 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: 97b2b4cd0624780588e5aaa90b93bf26e5f272246b043e076a282461e036188f
|
4
|
+
data.tar.gz: 8d440f2716fad476dbf95dd94698e77cb90099e9be330ffff599b788d1e2be1c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 063a5479a255e730d6fc4c2f9b89fc007484662ca2e852536603f974daa1fa8017d783ac5bfebe776b39096c3a70e7d65b2266c1efc80c75562ecb94d5f6da69
|
7
|
+
data.tar.gz: a52b4f47e0a15e82769e2188467c543580ad45203e42817b3341e24f11413af7719f1122b3255eeb59c05c9733fdad1b9c0949d53495211de6a7f14249da9eb6
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.7
|
data/lib/db_memoize/helpers.rb
CHANGED
@@ -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
|
data/lib/db_memoize/model.rb
CHANGED
@@ -8,21 +8,15 @@ module DbMemoize
|
|
8
8
|
memoizable = !changed? && persisted?
|
9
9
|
return send("#{method_name}_without_memoize") unless memoizable
|
10
10
|
|
11
|
-
|
12
|
-
cached_value = find_memoized_value(method_name)
|
11
|
+
memoized_value = find_memoized_value(method_name)
|
13
12
|
|
14
|
-
if
|
15
|
-
|
16
|
-
Helpers.log(self, method_name, 'cache hit')
|
13
|
+
if memoized_value
|
14
|
+
memoized_value.value
|
17
15
|
else
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
|
data/lib/db_memoize/railtie.rb
CHANGED
data/lib/db_memoize/value.rb
CHANGED
@@ -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,
|
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,
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
value = JSON.generate(value) if
|
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, #{
|
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,
|
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.
|
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-
|
11
|
+
date: 2018-10-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simple-sql
|