cached_resource 5.0.1 → 5.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +6 -10
- data/gemfiles/4.2.gemfile.lock +1 -1
- data/gemfiles/5.0.gemfile.lock +1 -1
- data/gemfiles/5.1.gemfile.lock +1 -1
- data/lib/cached_resource/caching.rb +12 -3
- data/lib/cached_resource/version.rb +1 -1
- data/spec/cached_resource/caching_spec.rb +42 -37
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 649cd7b534f3afc2628e4400f3b08e05769ed68b
|
4
|
+
data.tar.gz: 4e47e85d1ae399953ae9544bfe8b0ae0de9c99c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dabb024419aeb48a493fe0d4b1bdffdf7177b17a6c4fbcd573f8fbe99ee756acbbfb809daf8dd8a314843ab32bde126b9e5609b941864e5bc95f99949d6e00fe
|
7
|
+
data.tar.gz: 6008d2ed179cd97d1187cfe4a0f15922a48e1b5ae88af77f857643ac05e76eb8d2326675ecaeca2b78b4d3df75d6fb3c294a5492d65458a6d4bf0a7453fb36e5
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -121,19 +121,15 @@ Since resources are cached with an argument based key, you may pass in extra dat
|
|
121
121
|
## Testing
|
122
122
|
rake
|
123
123
|
|
124
|
+
or to test all supported environments
|
125
|
+
|
126
|
+
bundle exec appraisal rake
|
127
|
+
|
128
|
+
For more details, head over to the [appraisal](https://github.com/thoughtbot/appraisal) documentation.
|
129
|
+
|
124
130
|
## Credit/Inspiration
|
125
131
|
* quamen and [this gist](http://gist.github.com/947734)
|
126
132
|
* latimes and [this plugin](http://github.com/latimes/cached_resource)
|
127
133
|
|
128
134
|
## Feedback/Problems
|
129
135
|
Feedback is greatly appreciated! Check out this project's [issue tracker](https://github.com/Ahsizara/cached_resource/issues) if you've got anything to say.
|
130
|
-
|
131
|
-
## Future Considerations
|
132
|
-
This may change at any time.
|
133
|
-
|
134
|
-
* Callbacks on before and after reload
|
135
|
-
* Consider checksums to improve the determination of freshness/changédness
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
data/gemfiles/4.2.gemfile.lock
CHANGED
data/gemfiles/5.0.gemfile.lock
CHANGED
data/gemfiles/5.1.gemfile.lock
CHANGED
@@ -7,7 +7,7 @@ module CachedResource
|
|
7
7
|
included do
|
8
8
|
class << self
|
9
9
|
alias_method :find_without_cache, :find
|
10
|
-
alias_method :find, :find_with_cache
|
10
|
+
alias_method :find, :find_with_cache
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
@@ -84,7 +84,8 @@ module CachedResource
|
|
84
84
|
|
85
85
|
# Read a entry from the cache for the given key.
|
86
86
|
def cache_read(key)
|
87
|
-
object = cached_resource.cache.read(key).try do |
|
87
|
+
object = cached_resource.cache.read(key).try do |json_cache|
|
88
|
+
cache = json_to_object(JSON.parse(json_cache))
|
88
89
|
if cache.is_a? Enumerable
|
89
90
|
restored = cache.map { |record| full_dup(record) }
|
90
91
|
next restored unless respond_to?(:collection_parser)
|
@@ -99,7 +100,7 @@ module CachedResource
|
|
99
100
|
|
100
101
|
# Write an entry to the cache for the given key and value.
|
101
102
|
def cache_write(key, object)
|
102
|
-
result = cached_resource.cache.write(key, object, :race_condition_ttl => cached_resource.race_condition_ttl, :expires_in => cached_resource.generate_ttl)
|
103
|
+
result = cached_resource.cache.write(key, object.to_json, :race_condition_ttl => cached_resource.race_condition_ttl, :expires_in => cached_resource.generate_ttl)
|
103
104
|
result && cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} WRITE #{key}")
|
104
105
|
result
|
105
106
|
end
|
@@ -124,6 +125,14 @@ module CachedResource
|
|
124
125
|
end
|
125
126
|
end
|
126
127
|
|
128
|
+
def json_to_object(json)
|
129
|
+
if json.is_a? Array
|
130
|
+
json.map { |attrs| self.new(attrs) }
|
131
|
+
else
|
132
|
+
self.new(json)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
127
136
|
end
|
128
137
|
end
|
129
138
|
end
|
@@ -2,6 +2,10 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe CachedResource do
|
4
4
|
|
5
|
+
def read_from_cache(key)
|
6
|
+
Thing.send(:cache_read, key)
|
7
|
+
end
|
8
|
+
|
5
9
|
before(:each) do
|
6
10
|
class Thing < ActiveResource::Base
|
7
11
|
self.site = "http://api.thing.com"
|
@@ -43,23 +47,24 @@ describe CachedResource do
|
|
43
47
|
|
44
48
|
it "should cache a response" do
|
45
49
|
result = Thing.find(1)
|
46
|
-
|
50
|
+
|
51
|
+
read_from_cache("thing/1").should == result
|
47
52
|
end
|
48
53
|
|
49
54
|
it "should cache a response for a string primary key" do
|
50
55
|
result = Thing.find("fded")
|
51
|
-
|
56
|
+
read_from_cache("thing/fded").should == result
|
52
57
|
end
|
53
58
|
|
54
59
|
it "should cache without whitespace in keys" do
|
55
60
|
result = Thing.find(1, :from => 'path', :params => { :foo => 'bar' })
|
56
|
-
|
61
|
+
read_from_cache('thing/1/{:from=>"path",:params=>{:foo=>"bar"}}').should == result
|
57
62
|
end
|
58
63
|
|
59
64
|
it "should empty the cache when clear_cache is called" do
|
60
65
|
result = Thing.find(1)
|
61
66
|
Thing.clear_cache
|
62
|
-
|
67
|
+
read_from_cache("thing/1").should == nil
|
63
68
|
end
|
64
69
|
|
65
70
|
it "should cache a response with the same persistence" do
|
@@ -108,7 +113,7 @@ describe CachedResource do
|
|
108
113
|
# make a request
|
109
114
|
Thing.find(1)
|
110
115
|
# get the cached result of the request
|
111
|
-
old_result =
|
116
|
+
old_result = read_from_cache("thing/1")
|
112
117
|
|
113
118
|
# change the response
|
114
119
|
ActiveResource::HttpMock.reset!
|
@@ -117,7 +122,7 @@ describe CachedResource do
|
|
117
122
|
end
|
118
123
|
|
119
124
|
Thing.find(1, :reload => true)
|
120
|
-
new_result =
|
125
|
+
new_result = read_from_cache("thing/1")
|
121
126
|
# since active resources are equal if and only if they
|
122
127
|
# are the same object or an instance of the same class,
|
123
128
|
# not new?, and have the same id.
|
@@ -156,7 +161,7 @@ describe CachedResource do
|
|
156
161
|
shared_examples "collection_return_type" do
|
157
162
|
if ActiveResource::VERSION::MAJOR >= 4
|
158
163
|
it "should return an ActiveResource::Collection" do
|
159
|
-
cached =
|
164
|
+
cached = read_from_cache("thing/all")
|
160
165
|
cached.should be_instance_of(ActiveResource::Collection)
|
161
166
|
end
|
162
167
|
|
@@ -165,12 +170,12 @@ describe CachedResource do
|
|
165
170
|
class CustomCollection < ActiveResource::Collection; end
|
166
171
|
Thing.collection_parser = CustomCollection
|
167
172
|
Thing.all
|
168
|
-
cached =
|
173
|
+
cached = read_from_cache("thing/all")
|
169
174
|
cached.should be_instance_of(CustomCollection)
|
170
175
|
end
|
171
176
|
else
|
172
177
|
it "should return an Array" do
|
173
|
-
cached =
|
178
|
+
cached = read_from_cache("thing/all")
|
174
179
|
cached.should be_instance_of(Array)
|
175
180
|
end
|
176
181
|
end
|
@@ -222,8 +227,8 @@ describe CachedResource do
|
|
222
227
|
shared_examples "collection_cache_clearing" do
|
223
228
|
it "should empty the cache when clear_cache is called" do
|
224
229
|
Thing.clear_cache
|
225
|
-
|
226
|
-
|
230
|
+
read_from_cache("thing/all").should == nil
|
231
|
+
read_from_cache("thing/1").should == nil
|
227
232
|
end
|
228
233
|
|
229
234
|
end
|
@@ -254,8 +259,8 @@ describe CachedResource do
|
|
254
259
|
# only the all request should have been made
|
255
260
|
ActiveResource::HttpMock.requests.length.should == 1
|
256
261
|
# the result should be cached with the appropriate key
|
257
|
-
|
258
|
-
|
262
|
+
read_from_cache("thing/1").should == result
|
263
|
+
read_from_cache("thing/fded").should == string_result
|
259
264
|
end
|
260
265
|
|
261
266
|
include_examples "collection_cache_clearing"
|
@@ -274,11 +279,11 @@ describe CachedResource do
|
|
274
279
|
Thing.all(:reload => true)
|
275
280
|
# get the updated result, read from the cache
|
276
281
|
result = Thing.find(1)
|
277
|
-
|
278
|
-
|
282
|
+
read_from_cache("thing/all")[0].should == result
|
283
|
+
read_from_cache("thing/all")[0].name.should == result.name
|
279
284
|
string_result = Thing.find("fded")
|
280
|
-
|
281
|
-
|
285
|
+
read_from_cache("thing/all")[1].should == string_result
|
286
|
+
read_from_cache("thing/all")[1].name.should == string_result.name
|
282
287
|
end
|
283
288
|
|
284
289
|
it "should update the collection when an individual request is reloaded" do
|
@@ -291,11 +296,11 @@ describe CachedResource do
|
|
291
296
|
|
292
297
|
# reload the individual
|
293
298
|
result = Thing.find(1, :reload => true)
|
294
|
-
|
295
|
-
|
299
|
+
read_from_cache("thing/all")[0].should == result
|
300
|
+
read_from_cache("thing/all")[0].name.should == result.name
|
296
301
|
string_result = Thing.find("fded", :reload => true)
|
297
|
-
|
298
|
-
|
302
|
+
read_from_cache("thing/all")[1].should == string_result
|
303
|
+
read_from_cache("thing/all")[1].name.should == string_result.name
|
299
304
|
end
|
300
305
|
|
301
306
|
it "should update both the collection and the member cache entries when a subset of the collection is retrieved" do
|
@@ -312,20 +317,20 @@ describe CachedResource do
|
|
312
317
|
# make a request for a subset of the "mother" collection
|
313
318
|
result = Thing.find(:all, :params => {:name => "Ari"})
|
314
319
|
# the collection should be updated to reflect the server change
|
315
|
-
|
316
|
-
|
320
|
+
read_from_cache("thing/all")[0].should == result[0]
|
321
|
+
read_from_cache("thing/all")[0].name.should == result[0].name
|
317
322
|
# the individual should be updated to reflect the server change
|
318
|
-
|
319
|
-
|
323
|
+
read_from_cache("thing/1").should == result[0]
|
324
|
+
read_from_cache("thing/1").name.should == result[0].name
|
320
325
|
|
321
326
|
# make a request for a subset of the "mother" collection
|
322
327
|
result = Thing.find(:all, :params => {:name => "Lon"})
|
323
328
|
# the collection should be updated to reflect the server change
|
324
|
-
|
325
|
-
|
329
|
+
read_from_cache("thing/all")[1].should == result[0]
|
330
|
+
read_from_cache("thing/all")[1].name.should == result[0].name
|
326
331
|
# the individual should be updated to reflect the server change
|
327
|
-
|
328
|
-
|
332
|
+
read_from_cache("thing/fded").should == result[0]
|
333
|
+
read_from_cache("thing/fded").name.should == result[0].name
|
329
334
|
end
|
330
335
|
|
331
336
|
it "should maintain the order of the collection when updating it" do
|
@@ -407,12 +412,12 @@ describe CachedResource do
|
|
407
412
|
# reload the individual
|
408
413
|
result = Thing.find(1, :reload => true)
|
409
414
|
# the ids are the same, but the names should be different
|
410
|
-
|
415
|
+
read_from_cache("thing/all")[0].name.should_not == result.name
|
411
416
|
|
412
417
|
# reload the individual
|
413
418
|
string_result = Thing.find("fded", :reload => true)
|
414
419
|
# the ids are the same, but the names should be different
|
415
|
-
|
420
|
+
read_from_cache("thing/all")[1].name.should_not == string_result.name
|
416
421
|
end
|
417
422
|
end
|
418
423
|
|
@@ -452,12 +457,12 @@ describe CachedResource do
|
|
452
457
|
|
453
458
|
it "should cache a response" do
|
454
459
|
result = Thing.find(1)
|
455
|
-
|
460
|
+
read_from_cache("thing/1").should == result
|
456
461
|
end
|
457
462
|
|
458
463
|
it "should cache a response for a string primary key" do
|
459
464
|
result = Thing.find("fded")
|
460
|
-
|
465
|
+
read_from_cache("thing/fded").should == result
|
461
466
|
end
|
462
467
|
|
463
468
|
it "should always remake the request" do
|
@@ -476,7 +481,7 @@ describe CachedResource do
|
|
476
481
|
|
477
482
|
it "should rewrite the cache for each request" do
|
478
483
|
Thing.find(1)
|
479
|
-
old_result =
|
484
|
+
old_result = read_from_cache("thing/1")
|
480
485
|
|
481
486
|
# change the response
|
482
487
|
ActiveResource::HttpMock.reset!
|
@@ -485,7 +490,7 @@ describe CachedResource do
|
|
485
490
|
end
|
486
491
|
|
487
492
|
Thing.find(1)
|
488
|
-
new_result =
|
493
|
+
new_result = read_from_cache("thing/1")
|
489
494
|
# since active resources are equal if and only if they
|
490
495
|
# are the same object or an instance of the same class,
|
491
496
|
# not new?, and have the same id.
|
@@ -494,7 +499,7 @@ describe CachedResource do
|
|
494
499
|
|
495
500
|
it "should rewrite the cache for each request for a string primary key" do
|
496
501
|
Thing.find("fded")
|
497
|
-
old_result =
|
502
|
+
old_result = read_from_cache("thing/fded")
|
498
503
|
|
499
504
|
# change the response
|
500
505
|
ActiveResource::HttpMock.reset!
|
@@ -503,7 +508,7 @@ describe CachedResource do
|
|
503
508
|
end
|
504
509
|
|
505
510
|
Thing.find("fded")
|
506
|
-
new_result =
|
511
|
+
new_result = read_from_cache("thing/fded")
|
507
512
|
# since active resources are equal if and only if they
|
508
513
|
# are the same object or an instance of the same class,
|
509
514
|
# not new?, and have the same id.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cached_resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.0
|
4
|
+
version: 5.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Morgan Brown
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-08-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activeresource
|