db_memoize 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/db_memoize.gemspec +1 -6
- data/lib/db_memoize.rb +6 -1
- data/lib/db_memoize/helpers.rb +18 -0
- data/lib/db_memoize/model.rb +25 -24
- data/lib/db_memoize/version.rb +1 -1
- data/lib/tasks/warmup.rake +9 -4
- metadata +3 -86
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2134329e831a84420b606a2f64aeae8703ebcd7a
|
4
|
+
data.tar.gz: 2abf35de9b06d53424513a0432cb884c73f1f4cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 208ba78d6912596d059e40e4f77f9393025a926c4c973a2599882a6980ce836c008442466d3db0c84fd8e1a9e2a625ca901f26d697f64054abdccf7976c700f9
|
7
|
+
data.tar.gz: 994bdc8506e1886f57276380da78431812d9c74b99c66ea9f02ce9bffef87b1f13e27182c556ba5dab8363f8896de9c4ae9d2442be214a1c324cf007cb416c9f
|
data/db_memoize.gemspec
CHANGED
@@ -22,12 +22,8 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.require_paths = ['lib']
|
23
23
|
|
24
24
|
spec.add_development_dependency 'railties', '~> 4.2'
|
25
|
-
spec.add_development_dependency 'activemodel', '~> 4.2'
|
26
|
-
spec.add_development_dependency 'actionpack', '~> 4.2'
|
27
|
-
spec.add_development_dependency 'activesupport', '~> 4.2'
|
28
25
|
spec.add_development_dependency 'activerecord', '~> 4.2'
|
29
|
-
|
30
|
-
spec.add_development_dependency 'arel', '6.0.0'
|
26
|
+
|
31
27
|
spec.add_development_dependency 'rake', '~> 10.5'
|
32
28
|
spec.add_development_dependency 'sqlite3'
|
33
29
|
spec.add_development_dependency 'rspec-rails', '~> 3.4'
|
@@ -39,7 +35,6 @@ Gem::Specification.new do |spec|
|
|
39
35
|
spec.add_development_dependency 'database_cleaner', '~> 1.5.3'
|
40
36
|
spec.add_development_dependency 'factory_girl', '~> 4.7.0'
|
41
37
|
spec.add_development_dependency 'ruby-progressbar', '~> 1.7'
|
42
|
-
spec.add_development_dependency 'ffaker'
|
43
38
|
spec.add_development_dependency 'awesome_print'
|
44
39
|
spec.add_development_dependency 'gem-release'
|
45
40
|
end
|
data/lib/db_memoize.rb
CHANGED
@@ -4,15 +4,20 @@ require 'digest'
|
|
4
4
|
require 'benchmark'
|
5
5
|
require 'db_memoize/version'
|
6
6
|
require 'db_memoize/value'
|
7
|
+
require 'db_memoize/helpers'
|
7
8
|
require 'db_memoize/model'
|
8
9
|
require 'db_memoize/railtie' if defined?(Rails)
|
9
10
|
|
10
11
|
module DbMemoize
|
11
12
|
class << self
|
12
|
-
attr_writer :logger
|
13
|
+
attr_writer :logger, :log_level
|
13
14
|
|
14
15
|
def logger
|
15
16
|
@logger ||= ::Logger.new(STDOUT)
|
16
17
|
end
|
18
|
+
|
19
|
+
def log_level
|
20
|
+
@log_level ||= :debug
|
21
|
+
end
|
17
22
|
end
|
18
23
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module DbMemoize
|
2
|
+
# The Helpers module contains some helper methods, mostly to not pollute
|
3
|
+
# the namespace of memoized objects and classes.
|
4
|
+
module Helpers
|
5
|
+
module_function
|
6
|
+
|
7
|
+
def find_ids(records_or_ids)
|
8
|
+
records_or_ids = Array(records_or_ids)
|
9
|
+
return [] if records_or_ids.empty?
|
10
|
+
|
11
|
+
records_or_ids.first.is_a?(ActiveRecord::Base) ? records_or_ids.map(&:id) : records_or_ids
|
12
|
+
end
|
13
|
+
|
14
|
+
def log(model, method_name, msg)
|
15
|
+
DbMemoize.logger.send(DbMemoize.log_level, "DbMemoize <#{model.class.name} id: #{model.id}>##{method_name} - #{msg}")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/db_memoize/model.rb
CHANGED
@@ -12,18 +12,14 @@ module DbMemoize
|
|
12
12
|
cached_value = find_memoized_value(method_name, args_hash)
|
13
13
|
|
14
14
|
if cached_value
|
15
|
-
log(method_name, 'cache hit')
|
16
15
|
value = Marshal.load(cached_value.value)
|
16
|
+
Helpers.log(self, method_name, 'cache hit')
|
17
17
|
else
|
18
18
|
time = ::Benchmark.realtime do
|
19
19
|
value = send("#{method_name}_without_memoize", *args)
|
20
|
+
create_memoized_value(method_name, args_hash, value)
|
20
21
|
end
|
21
|
-
|
22
|
-
# `format` method themselves.
|
23
|
-
# rubocop:disable Style/FormatString
|
24
|
-
log(method_name, "cache miss. took #{sprintf('%.2f', time * 1_000)}ms")
|
25
|
-
# rubocop:enable Style/FormatString
|
26
|
-
create_memoized_value(method_name, args_hash, value)
|
22
|
+
Helpers.log(self, method_name, "cache miss. took #{Kernel.format '%.2f msecs', time * 1_000}")
|
27
23
|
end
|
28
24
|
|
29
25
|
value
|
@@ -31,13 +27,22 @@ module DbMemoize
|
|
31
27
|
|
32
28
|
def unmemoize(method_name = :all)
|
33
29
|
if method_name != :all
|
34
|
-
# FIXME: this works, but isn't immediately visible on the record
|
30
|
+
# FIXME: this works, but isn't immediately visible on the record.
|
31
|
+
# See also note in create_memoized_value.
|
35
32
|
memoized_values.where(method_name: method_name).delete_all
|
36
33
|
else
|
37
34
|
memoized_values.clear
|
38
35
|
end
|
39
36
|
end
|
40
37
|
|
38
|
+
#
|
39
|
+
# Used to set multiple memoized values in one go.
|
40
|
+
#
|
41
|
+
# Example:
|
42
|
+
#
|
43
|
+
# product.memoize_values full_title: "my full title",
|
44
|
+
# autocomplete_info: "my autocomplete_info"
|
45
|
+
#
|
41
46
|
def memoize_values(values, *args)
|
42
47
|
args_hash = ::Digest::MD5.hexdigest(Marshal.dump(args))
|
43
48
|
|
@@ -49,6 +54,9 @@ module DbMemoize
|
|
49
54
|
private
|
50
55
|
|
51
56
|
def create_memoized_value(method_name, args_hash, value)
|
57
|
+
# [TODO] - It would be nice to have an optimized, pg-based inserter
|
58
|
+
# here, for up to 10 times speed. However, the memoized_values
|
59
|
+
# array must then be properly reset.
|
52
60
|
memoized_values.create!(
|
53
61
|
entity_table_name: self.class.table_name,
|
54
62
|
method_name: method_name.to_s,
|
@@ -58,22 +66,22 @@ module DbMemoize
|
|
58
66
|
end
|
59
67
|
|
60
68
|
def find_memoized_value(method_name, args_hash)
|
69
|
+
method_name = method_name.to_s
|
70
|
+
|
61
71
|
memoized_values.detect do |rec|
|
62
|
-
rec.method_name == method_name
|
72
|
+
rec.method_name == method_name &&
|
63
73
|
rec.arguments_hash == args_hash
|
64
74
|
end
|
65
75
|
end
|
66
76
|
|
67
|
-
def log(method_name, msg)
|
68
|
-
DbMemoize.logger.info "DbMemoize <#{self.class.name} id: #{id}>##{method_name} - #{msg}"
|
69
|
-
end
|
70
|
-
|
71
77
|
module ClassMethods
|
72
78
|
def db_memoize(method_name)
|
73
79
|
@db_memoized_methods ||= []
|
74
80
|
@db_memoized_methods.push(method_name.to_sym)
|
75
81
|
|
76
|
-
|
82
|
+
# [TODO] - should the create_memoized_** functions really be called
|
83
|
+
# when the method_name is in @db_memoized_methods already?
|
84
|
+
create_memoized_alias_method(method_name)
|
77
85
|
create_memoized_values_association
|
78
86
|
end
|
79
87
|
|
@@ -85,7 +93,7 @@ module DbMemoize
|
|
85
93
|
def unmemoize(records_or_ids, method_name = :all)
|
86
94
|
conditions = {
|
87
95
|
entity_table_name: table_name,
|
88
|
-
entity_id: find_ids(records_or_ids)
|
96
|
+
entity_id: Helpers.find_ids(records_or_ids)
|
89
97
|
}
|
90
98
|
conditions[:method_name] = method_name unless method_name == :all
|
91
99
|
|
@@ -94,7 +102,7 @@ module DbMemoize
|
|
94
102
|
|
95
103
|
def memoize_values(records_or_ids, values, *args)
|
96
104
|
transaction do
|
97
|
-
ids = find_ids(records_or_ids)
|
105
|
+
ids = Helpers.find_ids(records_or_ids)
|
98
106
|
args_hash = ::Digest::MD5.hexdigest(Marshal.dump(args))
|
99
107
|
|
100
108
|
ids.each do |id|
|
@@ -113,14 +121,7 @@ module DbMemoize
|
|
113
121
|
|
114
122
|
private
|
115
123
|
|
116
|
-
def
|
117
|
-
records_or_ids = Array(records_or_ids)
|
118
|
-
return [] if records_or_ids.empty?
|
119
|
-
|
120
|
-
records_or_ids.first.is_a?(ActiveRecord::Base) ? records_or_ids.map(&:id) : records_or_ids
|
121
|
-
end
|
122
|
-
|
123
|
-
def create_alias_method(method_name)
|
124
|
+
def create_memoized_alias_method(method_name)
|
124
125
|
define_method "#{method_name}_with_memoize" do |*args|
|
125
126
|
memoized_value(method_name, args)
|
126
127
|
end
|
data/lib/db_memoize/version.rb
CHANGED
data/lib/tasks/warmup.rake
CHANGED
@@ -1,11 +1,15 @@
|
|
1
1
|
namespace :db_memoize do
|
2
|
-
desc "generates memoized values (pass e.g. '
|
2
|
+
desc "generates memoized values (pass e.g. 'CLASS=Product [ METHODS=to_document,to_hash ]')"
|
3
3
|
task warmup: :environment do
|
4
4
|
require 'ruby-progressbar'
|
5
5
|
|
6
|
-
klass_name = ENV['class']
|
7
|
-
methods = ENV['methods'].split(',')
|
6
|
+
klass_name = ENV['class'] || ENV['CLASS'] || raise('Missing CLASS environment value')
|
8
7
|
klass = klass_name.constantize
|
8
|
+
|
9
|
+
methods = ENV['methods'] || ENV['METHODS']
|
10
|
+
methods = methods.split(',') if methods
|
11
|
+
methods ||= klass.db_memoized_methods.map(&:to_s)
|
12
|
+
|
9
13
|
count = klass.count
|
10
14
|
|
11
15
|
progressbar = ProgressBar.create(
|
@@ -17,7 +21,8 @@ namespace :db_memoize do
|
|
17
21
|
remainder_mark: '.'
|
18
22
|
)
|
19
23
|
|
20
|
-
|
24
|
+
klass.find_each do |record|
|
25
|
+
# calling each method will build the cached entries for these objects.
|
21
26
|
methods.each do |meth|
|
22
27
|
record.send(meth)
|
23
28
|
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.1.
|
4
|
+
version: 0.1.6
|
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: 2017-01-
|
11
|
+
date: 2017-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -24,48 +24,6 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '4.2'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: activemodel
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '4.2'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '4.2'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: actionpack
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '4.2'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '4.2'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: activesupport
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - "~>"
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '4.2'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - "~>"
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '4.2'
|
69
27
|
- !ruby/object:Gem::Dependency
|
70
28
|
name: activerecord
|
71
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,34 +38,6 @@ dependencies:
|
|
80
38
|
- - "~>"
|
81
39
|
- !ruby/object:Gem::Version
|
82
40
|
version: '4.2'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: activejob
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - "~>"
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '4.2'
|
90
|
-
type: :development
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - "~>"
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: '4.2'
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: arel
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
100
|
-
requirements:
|
101
|
-
- - '='
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: 6.0.0
|
104
|
-
type: :development
|
105
|
-
prerelease: false
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
107
|
-
requirements:
|
108
|
-
- - '='
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: 6.0.0
|
111
41
|
- !ruby/object:Gem::Dependency
|
112
42
|
name: rake
|
113
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -262,20 +192,6 @@ dependencies:
|
|
262
192
|
- - "~>"
|
263
193
|
- !ruby/object:Gem::Version
|
264
194
|
version: '1.7'
|
265
|
-
- !ruby/object:Gem::Dependency
|
266
|
-
name: ffaker
|
267
|
-
requirement: !ruby/object:Gem::Requirement
|
268
|
-
requirements:
|
269
|
-
- - ">="
|
270
|
-
- !ruby/object:Gem::Version
|
271
|
-
version: '0'
|
272
|
-
type: :development
|
273
|
-
prerelease: false
|
274
|
-
version_requirements: !ruby/object:Gem::Requirement
|
275
|
-
requirements:
|
276
|
-
- - ">="
|
277
|
-
- !ruby/object:Gem::Version
|
278
|
-
version: '0'
|
279
195
|
- !ruby/object:Gem::Dependency
|
280
196
|
name: awesome_print
|
281
197
|
requirement: !ruby/object:Gem::Requirement
|
@@ -323,6 +239,7 @@ files:
|
|
323
239
|
- bin/setup
|
324
240
|
- db_memoize.gemspec
|
325
241
|
- lib/db_memoize.rb
|
242
|
+
- lib/db_memoize/helpers.rb
|
326
243
|
- lib/db_memoize/migrations.rb
|
327
244
|
- lib/db_memoize/model.rb
|
328
245
|
- lib/db_memoize/railtie.rb
|