second_level_cache 2.3.0 → 2.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/lib/second_level_cache.rb +0 -88
- data/lib/second_level_cache/active_record.rb +1 -0
- data/lib/second_level_cache/mixin.rb +89 -0
- data/lib/second_level_cache/version.rb +1 -1
- data/test/test_helper.rb +2 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ca8ef3f66bcc6dcc76e93958357d577a63a4769
|
4
|
+
data.tar.gz: f90a4c724367d36589712a31df03723c2f4b7248
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a544566618c8643153d1bbd7c764878323b67e731cded7b1a0419866f8be54564a4fc8bcfb4c6a6511aa6ee54ad3e7824fc0887206463aed2146d075bb18fd8
|
7
|
+
data.tar.gz: c94834042b2d630e6a3281af8cfedac728d9e4e7f76df5bd6230b577eb2669451391881bb732c42416c15ee1eeee8aff162abe4f9186d401bc28913fef98747d
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
2.3.1
|
2
|
+
-----
|
3
|
+
|
4
|
+
- Fix some cases will raise "uninitialized constant SecondLevelCache::Mixin" error. (#66)
|
5
|
+
|
6
|
+
2.3.0
|
7
|
+
-----
|
8
|
+
|
9
|
+
* Use Model schema digest as cache_version, so you don't need set `:version` option now. (#60)
|
10
|
+
* Fix `store` serialize option (#62)
|
11
|
+
* Remove `acts_as_cached` method now! Please use `second_level_cache`. (#59)
|
12
|
+
|
1
13
|
2.2.7
|
2
14
|
-----
|
3
15
|
|
data/lib/second_level_cache.rb
CHANGED
@@ -12,92 +12,4 @@ module SecondLevelCache
|
|
12
12
|
class << self
|
13
13
|
delegate :logger, :cache_store, :cache_key_prefix, to: Config
|
14
14
|
end
|
15
|
-
|
16
|
-
module Mixin
|
17
|
-
extend ActiveSupport::Concern
|
18
|
-
|
19
|
-
module ClassMethods
|
20
|
-
attr_reader :second_level_cache_options
|
21
|
-
|
22
|
-
delegate :logger, :cache_store, :cache_key_prefix, to: SecondLevelCache
|
23
|
-
|
24
|
-
def second_level_cache(options = {})
|
25
|
-
@second_level_cache_enabled = true
|
26
|
-
@second_level_cache_options = options
|
27
|
-
@second_level_cache_options[:version] ||= 0
|
28
|
-
@second_level_cache_options[:expires_in] ||= 1.week
|
29
|
-
relation.class.send :prepend, SecondLevelCache::ActiveRecord::FinderMethods
|
30
|
-
prepend SecondLevelCache::ActiveRecord::Core
|
31
|
-
end
|
32
|
-
|
33
|
-
def second_level_cache_enabled?
|
34
|
-
if defined? @second_level_cache_enabled
|
35
|
-
@second_level_cache_enabled == true
|
36
|
-
else
|
37
|
-
false
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def without_second_level_cache
|
42
|
-
old = @second_level_cache_enabled
|
43
|
-
@second_level_cache_enabled = false
|
44
|
-
|
45
|
-
yield if block_given?
|
46
|
-
ensure
|
47
|
-
@second_level_cache_enabled = old
|
48
|
-
end
|
49
|
-
|
50
|
-
# Get MD5 digest of this Model schema
|
51
|
-
# http://api.rubyonrails.org/classes/ActiveRecord/Core/ClassMethods.html#method-i-inspect
|
52
|
-
def cache_version
|
53
|
-
return @cache_version if defined? @cache_version
|
54
|
-
# This line is copy from:
|
55
|
-
# https://github.com/rails/rails/blob/f9a5f48/activerecord/lib/active_record/core.rb#L236
|
56
|
-
attr_list = attribute_types.map { |name, type| "#{name}: #{type.type}" } * ', '
|
57
|
-
model_schema_digest = Digest::MD5.hexdigest(attr_list)
|
58
|
-
@cache_version = "#{second_level_cache_options[:version]}/#{model_schema_digest}"
|
59
|
-
end
|
60
|
-
|
61
|
-
def second_level_cache_key(id)
|
62
|
-
"#{cache_key_prefix}/#{table_name.downcase}/#{id}/#{cache_version}"
|
63
|
-
end
|
64
|
-
|
65
|
-
def read_second_level_cache(id)
|
66
|
-
return unless second_level_cache_enabled?
|
67
|
-
RecordMarshal.load(SecondLevelCache.cache_store.read(second_level_cache_key(id)))
|
68
|
-
end
|
69
|
-
|
70
|
-
def expire_second_level_cache(id)
|
71
|
-
return unless second_level_cache_enabled?
|
72
|
-
SecondLevelCache.cache_store.delete(second_level_cache_key(id))
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
def second_level_cache_key
|
77
|
-
self.class.second_level_cache_key(id)
|
78
|
-
end
|
79
|
-
|
80
|
-
def expire_second_level_cache
|
81
|
-
return unless self.class.second_level_cache_enabled?
|
82
|
-
SecondLevelCache.cache_store.delete(second_level_cache_key)
|
83
|
-
end
|
84
|
-
|
85
|
-
def write_second_level_cache
|
86
|
-
return unless self.class.second_level_cache_enabled?
|
87
|
-
marshal = RecordMarshal.dump(self)
|
88
|
-
expires_in = self.class.second_level_cache_options[:expires_in]
|
89
|
-
expire_changed_association_uniq_keys
|
90
|
-
SecondLevelCache.cache_store.write(second_level_cache_key, marshal, expires_in: expires_in)
|
91
|
-
end
|
92
|
-
|
93
|
-
alias update_second_level_cache write_second_level_cache
|
94
|
-
|
95
|
-
def expire_changed_association_uniq_keys
|
96
|
-
reflections = self.class.reflections.select { |_, reflection| reflection.belongs_to? }
|
97
|
-
changed_keys = reflections.map { |_, reflection| reflection.foreign_key } & previous_changes.keys
|
98
|
-
changed_keys.each do |key|
|
99
|
-
SecondLevelCache.cache_store.delete(self.class.send(:cache_uniq_key, key => previous_changes[key][0]))
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
15
|
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module SecondLevelCache
|
2
|
+
module Mixin
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
attr_reader :second_level_cache_options
|
7
|
+
|
8
|
+
delegate :logger, :cache_store, :cache_key_prefix, to: SecondLevelCache
|
9
|
+
|
10
|
+
def second_level_cache(options = {})
|
11
|
+
@second_level_cache_enabled = true
|
12
|
+
@second_level_cache_options = options
|
13
|
+
@second_level_cache_options[:version] ||= 0
|
14
|
+
@second_level_cache_options[:expires_in] ||= 1.week
|
15
|
+
relation.class.send :prepend, SecondLevelCache::ActiveRecord::FinderMethods
|
16
|
+
prepend SecondLevelCache::ActiveRecord::Core
|
17
|
+
end
|
18
|
+
|
19
|
+
def second_level_cache_enabled?
|
20
|
+
if defined? @second_level_cache_enabled
|
21
|
+
@second_level_cache_enabled == true
|
22
|
+
else
|
23
|
+
false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def without_second_level_cache
|
28
|
+
old = @second_level_cache_enabled
|
29
|
+
@second_level_cache_enabled = false
|
30
|
+
|
31
|
+
yield if block_given?
|
32
|
+
ensure
|
33
|
+
@second_level_cache_enabled = old
|
34
|
+
end
|
35
|
+
|
36
|
+
# Get MD5 digest of this Model schema
|
37
|
+
# http://api.rubyonrails.org/classes/ActiveRecord/Core/ClassMethods.html#method-i-inspect
|
38
|
+
def cache_version
|
39
|
+
return @cache_version if defined? @cache_version
|
40
|
+
# This line is copy from:
|
41
|
+
# https://github.com/rails/rails/blob/f9a5f48/activerecord/lib/active_record/core.rb#L236
|
42
|
+
attr_list = attribute_types.map { |name, type| "#{name}: #{type.type}" } * ', '
|
43
|
+
model_schema_digest = Digest::MD5.hexdigest(attr_list)
|
44
|
+
@cache_version = "#{second_level_cache_options[:version]}/#{model_schema_digest}"
|
45
|
+
end
|
46
|
+
|
47
|
+
def second_level_cache_key(id)
|
48
|
+
"#{cache_key_prefix}/#{table_name.downcase}/#{id}/#{cache_version}"
|
49
|
+
end
|
50
|
+
|
51
|
+
def read_second_level_cache(id)
|
52
|
+
return unless second_level_cache_enabled?
|
53
|
+
RecordMarshal.load(SecondLevelCache.cache_store.read(second_level_cache_key(id)))
|
54
|
+
end
|
55
|
+
|
56
|
+
def expire_second_level_cache(id)
|
57
|
+
return unless second_level_cache_enabled?
|
58
|
+
SecondLevelCache.cache_store.delete(second_level_cache_key(id))
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def second_level_cache_key
|
63
|
+
self.class.second_level_cache_key(id)
|
64
|
+
end
|
65
|
+
|
66
|
+
def expire_second_level_cache
|
67
|
+
return unless self.class.second_level_cache_enabled?
|
68
|
+
SecondLevelCache.cache_store.delete(second_level_cache_key)
|
69
|
+
end
|
70
|
+
|
71
|
+
def write_second_level_cache
|
72
|
+
return unless self.class.second_level_cache_enabled?
|
73
|
+
marshal = RecordMarshal.dump(self)
|
74
|
+
expires_in = self.class.second_level_cache_options[:expires_in]
|
75
|
+
expire_changed_association_uniq_keys
|
76
|
+
SecondLevelCache.cache_store.write(second_level_cache_key, marshal, expires_in: expires_in)
|
77
|
+
end
|
78
|
+
|
79
|
+
alias update_second_level_cache write_second_level_cache
|
80
|
+
|
81
|
+
def expire_changed_association_uniq_keys
|
82
|
+
reflections = self.class.reflections.select { |_, reflection| reflection.belongs_to? }
|
83
|
+
changed_keys = reflections.map { |_, reflection| reflection.foreign_key } & previous_changes.keys
|
84
|
+
changed_keys.each do |key|
|
85
|
+
SecondLevelCache.cache_store.delete(self.class.send(:cache_uniq_key, key => previous_changes[key][0]))
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -6,6 +6,8 @@ require 'database_cleaner'
|
|
6
6
|
require 'active_record'
|
7
7
|
|
8
8
|
ActiveSupport.test_order = :sorted if ActiveSupport.respond_to?(:test_order=)
|
9
|
+
# Force hook :active_record on_load event to make sure loader can work.
|
10
|
+
ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base)
|
9
11
|
|
10
12
|
require 'second_level_cache'
|
11
13
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: second_level_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.3.
|
4
|
+
version: 2.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hooopo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-02-
|
11
|
+
date: 2017-02-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -143,6 +143,7 @@ files:
|
|
143
143
|
- lib/second_level_cache/active_record/persistence.rb
|
144
144
|
- lib/second_level_cache/active_record/preloader.rb
|
145
145
|
- lib/second_level_cache/config.rb
|
146
|
+
- lib/second_level_cache/mixin.rb
|
146
147
|
- lib/second_level_cache/record_marshal.rb
|
147
148
|
- lib/second_level_cache/record_relation.rb
|
148
149
|
- lib/second_level_cache/version.rb
|