db_memoize 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -0
- data/db_memoize.gemspec +1 -0
- data/lib/db_memoize/model.rb +33 -11
- data/lib/db_memoize/version.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dfffa2764cf96130f8c5f970af1c4df4569a6863
|
4
|
+
data.tar.gz: 8f56d6aa555a8de336ddaea74de8ba15a6cc028e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a953847cb278bc0c4c323440820cb7747daab3867426bd6519269c4db4e309577c748145cc95044d30375a48ba87748d08a1a2fcf4b3266f983f7d406812883f
|
7
|
+
data.tar.gz: 614e9b9adb32ee2f0e96e5cc9a628d65c84480747c9ae82ce2b8f901e3b920a52bd2e0debb14bd7a806009bac577b52c2d638d29d190166451d0d4608b20bebf
|
data/README.md
CHANGED
@@ -114,6 +114,23 @@ Similarly you can wipe all cached values for a given class
|
|
114
114
|
bundle exec rake db_memoize:clear class=Letter
|
115
115
|
```
|
116
116
|
|
117
|
+
### Setup
|
118
|
+
|
119
|
+
To create the required DB tables add a migration like this:
|
120
|
+
|
121
|
+
```
|
122
|
+
class CreateMemoizedValues < ActiveRecord::Migration
|
123
|
+
def up
|
124
|
+
require 'db_memoize/migrations'
|
125
|
+
DbMemoize::Migrations.create_tables(self)
|
126
|
+
end
|
127
|
+
|
128
|
+
def down
|
129
|
+
drop_table :memoized_values
|
130
|
+
end
|
131
|
+
end
|
132
|
+
```
|
133
|
+
|
117
134
|
|
118
135
|
Have fun!
|
119
136
|
|
data/db_memoize.gemspec
CHANGED
data/lib/db_memoize/model.rb
CHANGED
@@ -68,26 +68,48 @@ module DbMemoize
|
|
68
68
|
end
|
69
69
|
|
70
70
|
def unmemoize(records_or_ids, method_name = :all)
|
71
|
-
records_or_ids = Array(records_or_ids)
|
72
|
-
return if records_or_ids.empty?
|
73
|
-
|
74
|
-
ids = if records_or_ids.first.is_a?(ActiveRecord::Base)
|
75
|
-
records_or_ids.map(&:id)
|
76
|
-
else
|
77
|
-
records_or_ids
|
78
|
-
end
|
79
|
-
|
80
71
|
conditions = {
|
81
72
|
entity_table_name: table_name,
|
82
|
-
entity_id:
|
73
|
+
entity_id: find_ids(records_or_ids)
|
83
74
|
}
|
84
75
|
conditions[:method_name] = method_name unless method_name == :all
|
85
76
|
|
86
77
|
DbMemoize::Value.where(conditions).delete_all
|
87
78
|
end
|
88
79
|
|
80
|
+
def memoize_values(records_or_ids, values)
|
81
|
+
transaction do
|
82
|
+
ids = find_ids(records_or_ids)
|
83
|
+
args_hash = ::Digest::MD5.hexdigest(Marshal.dump([]))
|
84
|
+
|
85
|
+
values = values.inject({}) do |hsh, (key, value)|
|
86
|
+
hsh.merge(key => Marshal.dump(value))
|
87
|
+
end
|
88
|
+
|
89
|
+
ids.each do |id|
|
90
|
+
values.each do |name, value|
|
91
|
+
DbMemoize::Value.create!(
|
92
|
+
entity_table_name: table_name,
|
93
|
+
entity_id: id,
|
94
|
+
method_name: name,
|
95
|
+
arguments_hash: args_hash,
|
96
|
+
custom_key: DbMemoize.default_custom_key.to_s,
|
97
|
+
value: value
|
98
|
+
)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
89
104
|
private
|
90
105
|
|
106
|
+
def find_ids(records_or_ids)
|
107
|
+
records_or_ids = Array(records_or_ids)
|
108
|
+
return [] if records_or_ids.empty?
|
109
|
+
|
110
|
+
records_or_ids.first.is_a?(ActiveRecord::Base) ? records_or_ids.map(&:id) : records_or_ids
|
111
|
+
end
|
112
|
+
|
91
113
|
def create_alias_method(method_name)
|
92
114
|
define_method "#{method_name}_with_memoize" do |*args|
|
93
115
|
memoized_value(method_name, args)
|
@@ -100,7 +122,7 @@ module DbMemoize
|
|
100
122
|
unless reflect_on_association(:memoized_values)
|
101
123
|
conditions = { entity_table_name: table_name }
|
102
124
|
has_many :memoized_values, -> { where(conditions) },
|
103
|
-
dependent: :
|
125
|
+
dependent: :delete_all, class_name: 'DbMemoize::Value', foreign_key: :entity_id
|
104
126
|
end
|
105
127
|
end
|
106
128
|
end
|
data/lib/db_memoize/version.rb
CHANGED
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.1
|
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-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -290,6 +290,20 @@ dependencies:
|
|
290
290
|
- - ">="
|
291
291
|
- !ruby/object:Gem::Version
|
292
292
|
version: '0'
|
293
|
+
- !ruby/object:Gem::Dependency
|
294
|
+
name: gem-release
|
295
|
+
requirement: !ruby/object:Gem::Requirement
|
296
|
+
requirements:
|
297
|
+
- - ">="
|
298
|
+
- !ruby/object:Gem::Version
|
299
|
+
version: '0'
|
300
|
+
type: :development
|
301
|
+
prerelease: false
|
302
|
+
version_requirements: !ruby/object:Gem::Requirement
|
303
|
+
requirements:
|
304
|
+
- - ">="
|
305
|
+
- !ruby/object:Gem::Version
|
306
|
+
version: '0'
|
293
307
|
description: library to cache (memoize) method return values in database
|
294
308
|
email:
|
295
309
|
- goetzinger@mediapeers.com
|