db_memoize 0.3.11 → 0.3.12
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/migrations.rb +1 -2
- data/lib/db_memoize/model.rb +11 -1
- data/lib/db_memoize/value.rb +7 -13
- 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: ce34701844a9a089e61d5010352b335795bcfb477d858c60189866012162e4d3
|
4
|
+
data.tar.gz: a2e8c91d767ff7550c6a84f3adcdb61288c60557700ac629f6473e73ce4ed64c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 264acf8b331b10ed97680e1ee56752d2bbf9fa990ff0910c54711dd6d7b70b1e2aaa37c328f85161b38217e91dd1e9564ee86695f1ba5c7a3854e5d3ba1cfbe1
|
7
|
+
data.tar.gz: 58869f58b470ecff3ea1ab33e98ca81e657dce81e86df549e18aa5d2f5a7dabd8047145640c36a1661c187333e64baa2913190eb30d6a50225d9c29e50a97f81
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.12
|
@@ -22,8 +22,7 @@ module DbMemoize
|
|
22
22
|
|
23
23
|
-- entity_id/entity_table_name should have a better chance to be useful, since
|
24
24
|
-- there is more variance in entity_ids than there is in entity_table_names.
|
25
|
-
|
26
|
-
ON db_memoize.memoized_values(entity_id, entity_table_name, method_name);
|
25
|
+
DROP INDEX IF EXISTS memoized_attributes_idx2;
|
27
26
|
SQL
|
28
27
|
end
|
29
28
|
end
|
data/lib/db_memoize/model.rb
CHANGED
@@ -52,6 +52,14 @@ module DbMemoize
|
|
52
52
|
def find_memoized_value(method_name)
|
53
53
|
method_name = method_name.to_s
|
54
54
|
|
55
|
+
# In order to prevent database level deadlocks we don't manage any unique
|
56
|
+
# index on memoized values. This can result in duplicate matching memoized
|
57
|
+
# values.
|
58
|
+
#
|
59
|
+
# It is important to always return the freshest value. To make sure this
|
60
|
+
# happens the \a memoized_values association is ordered by its creation
|
61
|
+
# time (via "created_at DESC"), which lets us just return the first matching
|
62
|
+
# entry here.
|
55
63
|
memoized_values.detect do |rec|
|
56
64
|
rec.method_name == method_name
|
57
65
|
end
|
@@ -124,7 +132,9 @@ module DbMemoize
|
|
124
132
|
rec.memoized_values.delete_all_ordered
|
125
133
|
end
|
126
134
|
|
127
|
-
|
135
|
+
# memoized_values for this object. These values must be returned
|
136
|
+
# newest first, see the comment in \a find_memoized_value.
|
137
|
+
has_many :memoized_values, -> { where(conditions).order('created_at DESC') },
|
128
138
|
dependent: :delete_all, class_name: 'DbMemoize::Value', foreign_key: :entity_id
|
129
139
|
|
130
140
|
end
|
data/lib/db_memoize/value.rb
CHANGED
@@ -37,7 +37,7 @@ module DbMemoize
|
|
37
37
|
SQL.ask <<-SQL
|
38
38
|
DO $$DECLARE c record;
|
39
39
|
BEGIN
|
40
|
-
FOR c IN #{sql} ORDER BY ctid LOOP
|
40
|
+
FOR c IN SELECT * FROM (#{sql}) sq ORDER BY ctid LOOP
|
41
41
|
DELETE FROM #{DbMemoize::Value.table_name} WHERE ctid = c.ctid;
|
42
42
|
END LOOP;
|
43
43
|
END$$;
|
@@ -57,11 +57,6 @@ module DbMemoize
|
|
57
57
|
def self.fast_create(entity_table_name, entity_id, method_name, value)
|
58
58
|
method_name = method_name.to_s
|
59
59
|
|
60
|
-
# clear out old entry (if any). This makes sure that any existing value
|
61
|
-
# is cleared out properly.
|
62
|
-
SQL.ask "DELETE FROM #{table_name} WHERE(entity_table_name, entity_id, method_name) = ($1, $2, $3)",
|
63
|
-
entity_table_name, entity_id, method_name
|
64
|
-
|
65
60
|
column = case value
|
66
61
|
when String then :val_string
|
67
62
|
when Integer then :val_integer
|
@@ -82,15 +77,14 @@ module DbMemoize
|
|
82
77
|
when :val_time then value = _reformat_time(value)
|
83
78
|
end
|
84
79
|
|
85
|
-
|
86
|
-
|
87
|
-
|
80
|
+
# We initialize created_at with +statement_timestamp()+ since this
|
81
|
+
# reflects the current time when running the insert, resulting in
|
82
|
+
# increasing timestamps even within the same transaction.
|
83
|
+
#
|
84
|
+
# (This is only relevant for tests, though.)
|
88
85
|
sql = <<~SQL.freeze
|
89
86
|
INSERT INTO #{table_name}(entity_table_name, entity_id, method_name, #{column}, created_at)
|
90
|
-
VALUES($1,$2,$3,$4,
|
91
|
-
ON CONFLICT (entity_id, entity_table_name, method_name)
|
92
|
-
DO UPDATE
|
93
|
-
SET #{default_updates.join(', ')}, #{column}=$4, created_at=NOW()
|
87
|
+
VALUES($1,$2,$3,$4,statement_timestamp())
|
94
88
|
SQL
|
95
89
|
|
96
90
|
SQL.ask sql, entity_table_name, entity_id, method_name, value
|
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.12
|
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-
|
11
|
+
date: 2018-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simple-sql
|