rails-cache-tags 1.3.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f4b353fedb116bc1b8d576beb054b76a8dba9530
4
- data.tar.gz: 7bc4378c384faf161f3dd4887e0b162375a57429
3
+ metadata.gz: 3af1f3228eb57a6a08b6a1bb904f8c1008fe6a9c
4
+ data.tar.gz: bb81c31d6447f9bd198cc3ef77a60d0fcc3ffefd
5
5
  SHA512:
6
- metadata.gz: 1e453e001334ad6ffbc62401436fd4e6673ca567d93bc11cc5d51d6c351af1329ff686fbb8dbefde9572eec5beaf1ba3679cf7cfffddf6b5baa3ec9de9e187f8
7
- data.tar.gz: 917d425de030d371e3ef44556cd280a2dab22626fa469a13c9881a708aaa993875dc97fcf19dcf1481e4d058ef114f5289b544d04c74321fc7339664a0413ff0
6
+ metadata.gz: 128dd18180631c49207e3070b61bbe1ba4d4ce89db46efc4ba7d2f16d5cd1a828230fa536df1a7b817b11fd9824b1de75e194a08f5e34b510ef9801260a1b02f
7
+ data.tar.gz: 362939552ce228fe73efc07c1698a3a48f5e0f051e2233de6db66abb8d6c03a964138435c7a16a088d7813265d9aa6fc4c27f4fa0347c4e5eded5a2c4d12d307
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+
3
+ require 'request_store'
4
+
5
+ module Rails
6
+ module Cache
7
+ module Tags
8
+ class LocalCache
9
+ delegate :fetch, :exist?, :[], :[]=, to: :store
10
+
11
+ def clear
12
+ store.clear!
13
+ end
14
+
15
+ private
16
+
17
+ def store
18
+ if RequestStore.active?
19
+ RequestStore
20
+ else
21
+ DummyStore
22
+ end
23
+ end
24
+
25
+ class DummyStore
26
+ def self.fetch(_)
27
+ yield
28
+ end
29
+
30
+ def self.clear!; end
31
+ def self.exist?(_); end
32
+ def self.[](_); end
33
+ def self.[]=(*); end
34
+ end
35
+ end # class LocalCache
36
+ end # module Tags
37
+ end # module Cache
38
+ end # module Rails
@@ -1,24 +1,28 @@
1
1
  # coding: utf-8
2
2
 
3
3
  require 'rails/cache/tags/tag'
4
+ require 'rails/cache/tags/local_cache'
4
5
 
5
6
  module Rails
6
7
  module Cache
7
8
  module Tags
8
9
  class Set
9
- # @param [ActiveSupport::Cache::Store] cache
10
- def initialize(cache)
11
- @cache = cache
10
+ # @param [ActiveSupport::Cache::Store] store
11
+ def initialize(store)
12
+ @store = store
13
+ @tags_cache = LocalCache.new
12
14
  end
13
15
 
14
16
  def current(tag)
15
- @cache.fetch_without_tags(tag.to_key) { 1 }.to_i
17
+ key = tag.to_key
18
+ @tags_cache.fetch(key) do
19
+ @store.fetch_without_tags(key) { 1 }.to_i
20
+ end
16
21
  end
17
22
 
18
23
  def expire(tag)
19
24
  version = current(tag) + 1
20
-
21
- @cache.write_without_tags(tag.to_key, version, :expires_in => nil)
25
+ @store.write_without_tags(tag.to_key, version, :expires_in => nil)
22
26
 
23
27
  version
24
28
  end
@@ -30,14 +34,35 @@ module Rails
30
34
  tags = Tag.build(entry.tags.keys)
31
35
 
32
36
  saved_versions = entry.tags.values.map(&:to_i)
33
- current_versions = read_multi(tags).values.map(&:to_i)
34
37
 
35
- saved_versions == current_versions ? entry.value : nil
38
+ saved_versions == current_versions(tags) ? entry.value : nil
36
39
  end
37
40
 
38
41
  private
39
- def read_multi(tags)
40
- @cache.read_multi_without_tags(*Array.wrap(tags).map(&:to_key))
42
+
43
+ def current_versions(tags)
44
+ keys = Array.wrap(tags).map(&:to_key)
45
+ versions = keys.inject({}) do |memo, key|
46
+ if @tags_cache.exist?(key)
47
+ memo[key] = @tags_cache[key]
48
+ else
49
+ memo[key] = nil
50
+ end
51
+
52
+ memo
53
+ end
54
+
55
+ keys = versions.map { |key, version| key if version.nil? }.compact
56
+ @store.read_multi_without_tags(*keys).each do |key, value|
57
+ @tags_cache[key] = value
58
+ versions[key] = value
59
+ end if keys.present?
60
+
61
+ versions.values.map(&:to_i)
62
+ end
63
+
64
+ def tags_cache
65
+ @tags_cache
41
66
  end
42
67
  end # class Set
43
68
  end # module Tags
@@ -1,7 +1,7 @@
1
1
  module Rails
2
2
  module Cache
3
3
  module Tags
4
- VERSION = "1.3.3"
4
+ VERSION = "2.0.0"
5
5
  end
6
6
  end
7
7
  end
@@ -67,58 +67,144 @@ describe Rails::Cache::Tags do
67
67
  # assert_blank 'foo'
68
68
  #end
69
69
 
70
- it 'reads and writes a key if hash of tags given' do
71
- cache.write('foo', object, options.merge(:tags => {:baz => 1}))
72
- assert_read 'foo', object
70
+ context 'when no tags local cache' do
71
+ before do
72
+ RequestStore.end!
73
+ end
73
74
 
74
- cache.delete_tag :baz => 2
75
- assert_read 'foo', object
75
+ it 'reads and writes a key if hash of tags given' do
76
+ cache.write('foo', object, options.merge(:tags => {:baz => 1}))
77
+ assert_read 'foo', object
76
78
 
77
- cache.delete_tag :baz => 1
78
- assert_blank 'foo'
79
- end
79
+ cache.delete_tag :baz => 2
80
+ assert_read 'foo', object
80
81
 
81
- it 'reads and writes a key if array of object given as tags' do
82
- tag1 = 1.day.ago
83
- tag2 = 2.days.ago
82
+ cache.delete_tag :baz => 1
83
+ assert_blank 'foo'
84
+ end
84
85
 
85
- cache.write 'foo', object, options.merge(:tags => [tag1, tag2])
86
- assert_read 'foo', object
86
+ it 'reads and writes a key if array of object given as tags' do
87
+ tag1 = 1.day.ago
88
+ tag2 = 2.days.ago
87
89
 
88
- cache.delete_tag tag1
89
- assert_blank 'foo'
90
- end
90
+ cache.write 'foo', object, options.merge(:tags => [tag1, tag2])
91
+ assert_read 'foo', object
91
92
 
92
- it 'reads multiple keys with tags check' do
93
- cache.write 'foo', object, options.merge(:tags => :bar)
94
- cache.write 'bar', object, options.merge(:tags => :baz)
93
+ cache.delete_tag tag1
94
+ assert_blank 'foo'
95
+ end
95
96
 
96
- assert_read 'foo', object
97
- assert_read 'bar', object
97
+ it 'reads multiple keys with tags check' do
98
+ cache.write 'foo', object, options.merge(:tags => :bar)
99
+ cache.write 'bar', object, options.merge(:tags => :baz)
98
100
 
99
- cache.delete_tag :bar
101
+ assert_read 'foo', object
102
+ assert_read 'bar', object
100
103
 
101
- assert_blank 'foo'
102
- assert_read 'bar', object
104
+ cache.delete_tag :bar
103
105
 
104
- expect(cache.read_multi('foo', 'bar', options)).to eq('foo' => nil, 'bar' => object)
106
+ assert_blank 'foo'
107
+ assert_read 'bar', object
108
+
109
+ expect(cache.read_multi('foo', 'bar', options)).to eq('foo' => nil, 'bar' => object)
110
+ end
111
+
112
+ it 'fetches key with tag check' do
113
+ cache.write 'foo', object, options.merge(:tags => :bar)
114
+
115
+ expect(cache.fetch('foo', options) { 'baz' }).to eq object
116
+ expect(cache.fetch('foo', options)).to eq object
117
+
118
+ cache.delete_tag :bar
119
+
120
+ expect(cache.fetch('foo', options)).to be_nil
121
+ expect(cache.fetch('foo', options.merge(:tags => :bar)) { object }).to eq object
122
+ assert_read 'foo', object
123
+
124
+ cache.delete_tag :bar
125
+
126
+ assert_blank 'foo'
127
+ end
105
128
  end
106
129
 
107
- it 'fetches key with tag check' do
108
- cache.write 'foo', object, options.merge(:tags => :bar)
130
+ context 'when tags local cache' do
131
+ before do
132
+ RequestStore.clear!
133
+ RequestStore.begin!
134
+ end
109
135
 
110
- expect(cache.fetch('foo', options) { 'baz' }).to eq object
111
- expect(cache.fetch('foo', options)).to eq object
136
+ after do
137
+ RequestStore.end!
138
+ end
112
139
 
113
- cache.delete_tag :bar
140
+ it 'reads and writes a key if hash of tags given' do
141
+ cache.write('foo', object, options.merge(:tags => {:baz => 1}))
142
+ assert_read 'foo', object
114
143
 
115
- expect(cache.fetch('foo', options)).to be_nil
116
- expect(cache.fetch('foo', options.merge(:tags => :bar)) { object }).to eq object
117
- assert_read 'foo', object
144
+ cache.delete_tag :baz => 2
145
+ assert_read 'foo', object
118
146
 
119
- cache.delete_tag :bar
147
+ cache.delete_tag :baz => 1
148
+ assert_read 'foo', object
120
149
 
121
- assert_blank 'foo'
150
+ cache.tag_set.send(:tags_cache).clear
151
+ assert_blank 'foo'
152
+ end
153
+
154
+ it 'reads and writes a key if array of object given as tags' do
155
+ tag1 = 1.day.ago
156
+ tag2 = 2.days.ago
157
+
158
+ cache.write 'foo', object, options.merge(:tags => [tag1, tag2])
159
+ assert_read 'foo', object
160
+
161
+ cache.delete_tag tag1
162
+ assert_read 'foo', object
163
+
164
+ cache.tag_set.send(:tags_cache).clear
165
+ assert_blank 'foo'
166
+ end
167
+
168
+ it 'reads multiple keys with tags check' do
169
+ cache.write 'foo', object, options.merge(:tags => :bar)
170
+ cache.write 'bar', object, options.merge(:tags => :baz)
171
+
172
+ assert_read 'foo', object
173
+ assert_read 'bar', object
174
+
175
+ cache.delete_tag :bar
176
+
177
+ assert_read 'foo', object
178
+
179
+ cache.tag_set.send(:tags_cache).clear
180
+ assert_blank 'foo'
181
+ assert_read 'bar', object
182
+
183
+ expect(cache.read_multi('foo', 'bar', options)).to eq('foo' => nil, 'bar' => object)
184
+ end
185
+
186
+ it 'fetches key with tag check' do
187
+ cache.write 'foo', object, options.merge(:tags => :bar)
188
+
189
+ expect(cache.fetch('foo', options) { 'baz' }).to eq object
190
+ expect(cache.fetch('foo', options)).to eq object
191
+
192
+ cache.delete_tag :bar
193
+
194
+ expect(cache.fetch('foo', options)).to eq object
195
+
196
+ cache.tag_set.send(:tags_cache).clear
197
+
198
+ expect(cache.fetch('foo', options)).to be_nil
199
+ expect(cache.fetch('foo', options.merge(:tags => :bar)) { object }).to eq object
200
+ assert_read 'foo', object
201
+
202
+ cache.delete_tag :bar
203
+ assert_read 'foo', object
204
+
205
+ cache.tag_set.send(:tags_cache).clear
206
+ assert_blank 'foo'
207
+ end
122
208
  end
123
209
  end
124
210
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-cache-tags
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.3
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexei Mikhailov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-09 00:00:00.000000000 Z
11
+ date: 2017-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: request_store
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: i18n
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -159,6 +173,7 @@ extra_rdoc_files: []
159
173
  files:
160
174
  - lib/rails-cache-tags.rb
161
175
  - lib/rails/cache/tags/action_controller.rb
176
+ - lib/rails/cache/tags/local_cache.rb
162
177
  - lib/rails/cache/tags/set.rb
163
178
  - lib/rails/cache/tags/store.rb
164
179
  - lib/rails/cache/tags/tag.rb
@@ -184,7 +199,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
184
199
  version: '0'
185
200
  requirements: []
186
201
  rubyforge_project:
187
- rubygems_version: 2.4.8
202
+ rubygems_version: 2.5.1
188
203
  signing_key:
189
204
  specification_version: 4
190
205
  summary: Tagged caching support for Rails