active_model_cachers 2.1.1 → 2.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/active_model_cachers/active_record/attr_model.rb +5 -0
- data/lib/active_model_cachers/active_record/cacher.rb +26 -15
- data/lib/active_model_cachers/active_record/extension.rb +16 -2
- data/lib/active_model_cachers/cache_service_factory.rb +18 -4
- data/lib/active_model_cachers/hook/dependencies.rb +15 -3
- data/lib/active_model_cachers/version.rb +1 -1
- 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: 26cbc738077c571fd681dba6f83be1faf69672e52f6d21ef242ff1bec6d7b8d1
|
4
|
+
data.tar.gz: efa2374d6689b0c2fc1c2738293053ecfcc67720840f53ec7ef789c6a84543b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 179179ebf0b2b926963bdfc64392b65c952c5246cfdf4475196bfb9f5af6d2e8996f2c686bc33d031b3ef579c3736a68055e558363df991bb9708275f129f187
|
7
|
+
data.tar.gz: 21bac3df913b18a496c926816d2553c6fb84248d912b6a7861e41f03a37a82059ae86e14264f3f67fbc6bb23894e8bfd3514021d3b14d4d1d1af03c28907eafd
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
## Change Log
|
2
2
|
|
3
|
+
### [v2.1.1](https://github.com/khiav223577/active_model_cachers/compare/v2.1.0...v2.1.1) 2018/05/25
|
4
|
+
- [#35](https://github.com/khiav223577/active_model_cachers/pull/35) Preventing registering same callbacks in some cases (@khiav223577)
|
5
|
+
- [#34](https://github.com/khiav223577/active_model_cachers/pull/34) Enhance - automatically clean cache when `#touch` (@ff2248)
|
6
|
+
- [#33](https://github.com/khiav223577/active_model_cachers/pull/33) [Enhance] Code Climate Gem Deprecation (@berniechiu)
|
7
|
+
|
3
8
|
### [v2.1.0](https://github.com/khiav223577/active_model_cachers/compare/v2.0.3...v2.1.0) 2018/05/18
|
4
9
|
- [#32](https://github.com/khiav223577/active_model_cachers/pull/32) Add test cases to test "store all data in hash" (@khiav223577)
|
5
10
|
- [#31](https://github.com/khiav223577/active_model_cachers/pull/31) Change the syntax of getting self from cache (@khiav223577)
|
@@ -5,38 +5,49 @@ module ActiveModelCachers
|
|
5
5
|
@defined_map = {}
|
6
6
|
|
7
7
|
class << self
|
8
|
+
def get_cacher_klass(klass)
|
9
|
+
@defined_map[klass] ||= create_cacher_klass_at(klass)
|
10
|
+
end
|
11
|
+
|
8
12
|
def define_cacher_method(attr, primary_key, service_klasses)
|
9
13
|
cacher_klass = get_cacher_klass(attr.klass)
|
10
14
|
method = attr.column
|
11
15
|
return cacher_klass.define_find_by(attr, primary_key, service_klasses) if method == nil
|
12
|
-
cacher_klass.
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
def get_cacher_klass(klass)
|
19
|
-
@defined_map[klass] ||= create_cacher_klass_at(klass)
|
16
|
+
cacher_klass.send(:define_methods, method, {
|
17
|
+
method => ->{ exec_by(attr, primary_key, service_klasses, :get) },
|
18
|
+
"peek_#{method}" => ->{ exec_by(attr, primary_key, service_klasses, :peek) },
|
19
|
+
"clean_#{method}" => ->{ exec_by(attr, primary_key, service_klasses, :clean_cache) },
|
20
|
+
})
|
20
21
|
end
|
21
22
|
|
22
23
|
def define_find_by(attr, primary_key, service_klasses)
|
23
24
|
if @find_by_mapping == nil
|
24
25
|
@find_by_mapping = {}
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
26
|
+
define_methods(:find_by, {
|
27
|
+
:find_by => ->(args){ exec_find_by(args, :get) },
|
28
|
+
:peek_by => ->(args){ exec_find_by(args, :peek) },
|
29
|
+
:clean_by => ->(args){ exec_find_by(args, :clean_cache) },
|
30
|
+
})
|
29
31
|
end
|
30
32
|
@find_by_mapping[primary_key] = [attr, service_klasses]
|
31
33
|
end
|
32
34
|
|
35
|
+
private
|
36
|
+
|
37
|
+
def define_methods(attribute, methods_mapping)
|
38
|
+
if attributes.include?(attribute)
|
39
|
+
methods_mapping.keys.each{|s| undef_method(s) }
|
40
|
+
else
|
41
|
+
attributes << attribute
|
42
|
+
end
|
43
|
+
methods_mapping.each{|method, block| define_method(method, &block) }
|
44
|
+
end
|
45
|
+
|
33
46
|
def get_data_from_find_by_mapping(primary_key)
|
34
47
|
return if @find_by_mapping == nil
|
35
48
|
return @find_by_mapping[primary_key]
|
36
49
|
end
|
37
50
|
|
38
|
-
private
|
39
|
-
|
40
51
|
def create_cacher_klass_at(target)
|
41
52
|
cacher_klass = Class.new(self)
|
42
53
|
cacher_klass.instance_variable_set(:@find_by_mapping, nil) # to remove warning: instance variable @find_by_mapping not initialized
|
@@ -60,7 +71,7 @@ module ActiveModelCachers
|
|
60
71
|
|
61
72
|
def exec_find_by(args, method) # e.g. args = {course_id: xx}
|
62
73
|
primary_key = args.keys.sort.first # Support only one key now.
|
63
|
-
attr, service_klasses = self.class.get_data_from_find_by_mapping
|
74
|
+
attr, service_klasses = self.class.send(:get_data_from_find_by_mapping, primary_key)
|
64
75
|
return if service_klasses == nil
|
65
76
|
return exec_by(attr, primary_key, service_klasses, method, data: args[primary_key])
|
66
77
|
end
|
@@ -16,13 +16,21 @@ module ActiveModelCachers
|
|
16
16
|
attr = AttrModel.new(self, column, foreign_key: foreign_key, primary_key: primary_key)
|
17
17
|
return cache_belongs_to(attr) if attr.belongs_to?
|
18
18
|
|
19
|
+
loaded = false
|
20
|
+
class_name, *infos = get_expire_infos(attr, expire_by, foreign_key)
|
21
|
+
set_klass_to_mapping(attr, class_name) do
|
22
|
+
next if !loaded
|
23
|
+
cache_at(column, query, expire_by: expire_by, on: on, foreign_key: foreign_key, primary_key: primary_key)
|
24
|
+
end
|
25
|
+
loaded = true
|
26
|
+
|
19
27
|
query ||= ->(id){ attr.query_model(self, id) }
|
20
28
|
service_klass = CacheServiceFactory.create_for_active_model(attr, query)
|
21
29
|
Cacher.define_cacher_method(attr, attr.primary_key || :id, [service_klass])
|
22
30
|
|
23
|
-
if
|
31
|
+
if class_name
|
24
32
|
with_id = (expire_by.is_a?(Symbol) || query.parameters.size == 1)
|
25
|
-
service_klass.define_callback_for_cleaning_cache(*infos, with_id, on: on)
|
33
|
+
service_klass.define_callback_for_cleaning_cache(class_name, *infos, with_id, on: on)
|
26
34
|
end
|
27
35
|
|
28
36
|
return service_klass
|
@@ -35,6 +43,12 @@ module ActiveModelCachers
|
|
35
43
|
|
36
44
|
private
|
37
45
|
|
46
|
+
def set_klass_to_mapping(attr, class_name)
|
47
|
+
ActiveSupport::Dependencies.onload(class_name || self.to_s) do
|
48
|
+
yield if CacheServiceFactory.set_klass_to_mapping(attr, self)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
38
52
|
def get_expire_infos(attr, expire_by, foreign_key)
|
39
53
|
if expire_by.is_a?(Symbol)
|
40
54
|
expire_attr = get_association_attr(expire_by)
|
@@ -5,6 +5,7 @@ require 'active_model_cachers/cache_service'
|
|
5
5
|
module ActiveModelCachers
|
6
6
|
class CacheServiceFactory
|
7
7
|
@key_class_mapping = {}
|
8
|
+
@cache_key_klass_mapping = {}
|
8
9
|
|
9
10
|
class << self
|
10
11
|
def has_cacher?(attr)
|
@@ -12,9 +13,7 @@ module ActiveModelCachers
|
|
12
13
|
end
|
13
14
|
|
14
15
|
def create_for_active_model(attr, query)
|
15
|
-
|
16
|
-
service_klass = create(cache_key, query)
|
17
|
-
return service_klass
|
16
|
+
create(get_cache_key(attr), query)
|
18
17
|
end
|
19
18
|
|
20
19
|
def create(cache_key, query)
|
@@ -27,15 +26,30 @@ module ActiveModelCachers
|
|
27
26
|
}[]
|
28
27
|
end
|
29
28
|
|
29
|
+
def set_klass_to_mapping(attr, current_klass)
|
30
|
+
cache_key = get_cache_key(attr)
|
31
|
+
reflect = attr.klass.reflect_on_association(:posts)
|
32
|
+
changed = clean_klass_cache_if_reloaded!(cache_key, current_klass, attr)
|
33
|
+
@cache_key_klass_mapping[cache_key] = current_klass
|
34
|
+
return changed
|
35
|
+
end
|
36
|
+
|
30
37
|
private
|
31
38
|
|
32
39
|
def get_cache_key(attr)
|
33
|
-
class_name, column =
|
40
|
+
class_name, column = attr.extract_class_and_column
|
34
41
|
return "active_model_cachers_#{class_name}_at_#{column}" if column
|
35
42
|
foreign_key = attr.foreign_key(reverse: true)
|
36
43
|
return "active_model_cachers_#{class_name}_by_#{foreign_key}" if foreign_key and foreign_key.to_s != 'id'
|
37
44
|
return "active_model_cachers_#{class_name}"
|
38
45
|
end
|
46
|
+
|
47
|
+
def clean_klass_cache_if_reloaded!(cache_key, current_klass, attr)
|
48
|
+
origin_klass, @cache_key_klass_mapping[cache_key] = @cache_key_klass_mapping[cache_key], current_klass
|
49
|
+
return false if origin_klass == nil or origin_klass == current_klass # when code reloaded in development.
|
50
|
+
@key_class_mapping[cache_key] = nil
|
51
|
+
return true
|
52
|
+
end
|
39
53
|
end
|
40
54
|
end
|
41
55
|
end
|
@@ -3,12 +3,12 @@ require 'active_support/dependencies'
|
|
3
3
|
|
4
4
|
module ActiveModelCachers::Hook
|
5
5
|
module Depdenencies
|
6
|
-
def onload(const_name, &block)
|
6
|
+
def onload(const_name, times: 1, &block)
|
7
7
|
const = const_name if not const_name.is_a?(String)
|
8
8
|
if const or Module.const_defined?(const_name)
|
9
9
|
(const || const_name.constantize).instance_exec(&block)
|
10
10
|
else
|
11
|
-
load_hooks[const_name].push(block)
|
11
|
+
load_hooks[const_name].push(block: block, times: times)
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
@@ -17,7 +17,19 @@ module ActiveModelCachers::Hook
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def new_constants_in(*)
|
20
|
-
new_constants = super.each
|
20
|
+
new_constants = super.each do |const_name|
|
21
|
+
hooks = load_hooks[const_name]
|
22
|
+
need_compact = false
|
23
|
+
hooks.each_with_index do |hook, idx|
|
24
|
+
if (hook[:times] -= 1) < 0
|
25
|
+
hooks[idx] = nil
|
26
|
+
need_compact = true
|
27
|
+
next
|
28
|
+
end
|
29
|
+
const_name.constantize.instance_exec(&hook[:block])
|
30
|
+
end
|
31
|
+
hooks.compact! if need_compact
|
32
|
+
end
|
21
33
|
return new_constants
|
22
34
|
end
|
23
35
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_model_cachers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- khiav reoy
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-06-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|