cached_resource 5.0.0 → 5.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +2 -0
- data/LICENSE +1 -1
- data/README.md +85 -48
- data/gemfiles/4.2.gemfile.lock +6 -6
- data/gemfiles/5.0.gemfile.lock +9 -10
- data/gemfiles/5.1.gemfile.lock +9 -10
- data/lib/cached_resource/cached_resource.rb +2 -2
- data/lib/cached_resource/caching.rb +33 -9
- data/lib/cached_resource/version.rb +1 -1
- data/spec/cached_resource/cached_resource_spec.rb +29 -0
- data/spec/cached_resource/caching_spec.rb +66 -37
- metadata +5 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 769d5356d807aba4f41cc6be8c2d256921d028b1f205350f0e77024a4db15829
|
4
|
+
data.tar.gz: 89d9d4bb95b3f325265124bc2d6dd877f68ce0fe6eabe5af2eb9d85327c03ca8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6f2574db0f3bd31b1204a361c0c24c9494860947a63e9de90e5dc02762e2d78a034e6e68b380e763af574b2a5d0fc64c3107dbb86644de85fdfe0f8b16ff257c
|
7
|
+
data.tar.gz: 06c1087d5b036408ef7729e728ad27f8ae59dc2ec954a388fbbe455dc399fcc06b6d69a0bfa9f949c4e86761241a9a15a63d8956c18a1a3d696c4d9669a660d1
|
data/.travis.yml
CHANGED
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
-
# CachedResource [![Build Status](https://
|
1
|
+
# CachedResource [![Build Status](https://travis-ci.org/mhgbrown/cached_resource.svg?branch=master)](http://travis-ci.org/mhgbrown/cached_resource)
|
2
2
|
CachedResource is a Ruby gem whose goal is to increase the performance of interacting with web services via ActiveResource by caching responses based on request parameters. It can help reduce the lag created by making repeated requests across a network.
|
3
3
|
|
4
4
|
## Installation
|
5
|
-
|
5
|
+
|
6
|
+
```ruby
|
7
|
+
gem install cached_resource
|
8
|
+
```
|
6
9
|
|
7
10
|
## Compatibility
|
8
11
|
CachedResource supports the following Ruby versions:
|
@@ -24,15 +27,19 @@ For previously supported versions, use 4.2.0 or below.
|
|
24
27
|
## Configuration
|
25
28
|
**Set up CachedResource across all ActiveResources:**
|
26
29
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
+
```ruby
|
31
|
+
class ActiveResource::Base
|
32
|
+
cached_resource
|
33
|
+
end
|
34
|
+
```
|
30
35
|
|
31
36
|
Or set up CachedResource for a single class:
|
32
37
|
|
33
|
-
|
34
|
-
|
35
|
-
|
38
|
+
```ruby
|
39
|
+
class MyActiveResource < ActiveResource::Base
|
40
|
+
cached_resource
|
41
|
+
end
|
42
|
+
```
|
36
43
|
|
37
44
|
### Options
|
38
45
|
CachedResource accepts the following options:
|
@@ -49,91 +56,121 @@ CachedResource accepts the following options:
|
|
49
56
|
|
50
57
|
You can set them like this:
|
51
58
|
|
52
|
-
|
59
|
+
```ruby
|
60
|
+
cached_resource :cache => MyCacheStore.new, :ttl => 60, :collection_synchronize => true, :logger => MyLogger.new
|
61
|
+
```
|
53
62
|
|
54
63
|
You can also change them on the fly.
|
55
64
|
|
56
65
|
Turn CachedResource off. This will cause all responses to be retrieved normally (i.e. via the network). All responses will still be cached.
|
57
66
|
|
58
|
-
|
67
|
+
```ruby
|
68
|
+
MyActiveResource.cached_resource.off!
|
69
|
+
```
|
59
70
|
|
60
71
|
Turn CachedResource on.
|
61
|
-
|
62
|
-
|
72
|
+
```ruby
|
73
|
+
MyActiveResource.cached_resource.on!
|
74
|
+
```
|
63
75
|
|
64
76
|
Set the cache expiry time to 60 seconds.
|
65
77
|
|
66
|
-
|
78
|
+
```ruby
|
79
|
+
MyActiveResource.cached_resource.ttl = 60
|
80
|
+
```
|
67
81
|
|
68
82
|
Enable cache expiry time randomization, allowing it to fall randomly between 60 and 120 seconds.
|
69
83
|
|
70
|
-
|
84
|
+
```ruby
|
85
|
+
MyActiveResource.cached_resource.ttl_randomization = true
|
86
|
+
```
|
71
87
|
|
72
88
|
Change the cache expiry time randomization scale so that the cache expiry time falls randomly between 30 and 180 seconds.
|
73
89
|
|
74
|
-
|
75
|
-
|
90
|
+
```ruby
|
91
|
+
MyActiveResource.cached_resource.ttl_randomization_scale = 0.5..3
|
92
|
+
```
|
76
93
|
Enable collection synchronization. This will cause a call to `MyActiveResource.all` to also create cache entries for each of its members. So, for example, a later call to `MyActiveResource.find(1)` will be read from the cache instead of requested from the remote service.
|
77
94
|
|
78
|
-
|
79
|
-
|
95
|
+
```ruby
|
96
|
+
MyActiveResource.cached_resource.collection_synchronize = true
|
97
|
+
```
|
80
98
|
Change the arguments that identify the principal collection request. If for some reason you are concerned with a collection that is retrieved at a "non-standard" URL, you may specify the Ruby arguments that produce that URL. When `collection_synchronize` is `true`, the collection returned from a request that matches these arguments will be cached and later updated when one of its members or a subset is retrieved.
|
81
99
|
|
82
|
-
|
83
|
-
|
100
|
+
```ruby
|
101
|
+
MyActiveResource.cached_resource.collection_arguments = [:all, :params => {:name => "Bob"}]
|
102
|
+
```
|
84
103
|
Set a different logger.
|
85
104
|
|
86
|
-
|
87
|
-
|
105
|
+
```ruby
|
106
|
+
MyActiveResource.cached_resource.logger = MyLogger.new
|
107
|
+
```
|
88
108
|
Set a different cache store.
|
89
109
|
|
90
|
-
|
110
|
+
```ruby
|
111
|
+
MyActiveResource.cached_resource.cache = MyCacheStore.new
|
112
|
+
```
|
91
113
|
|
92
114
|
### Caveats
|
93
115
|
If you set up CachedResource across all ActiveResources or any subclass of ActiveResource that will be inherited by other classes and you want some of those others to have independent CachedResource configurations, then check out the example below:
|
94
116
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
117
|
+
```ruby
|
118
|
+
class ActiveResource::Base
|
119
|
+
cached_resource
|
120
|
+
end
|
121
|
+
```
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
class MyActiveResource < ActiveResource::Base
|
125
|
+
self.cached_resource = CachedResource::Configuration.new(:collection_synchronize => true)
|
126
|
+
end
|
127
|
+
```
|
103
128
|
## Usage
|
104
129
|
Sit back and relax! If you need to reload a particular request you can pass `:reload => true` into the options hash like this:
|
105
130
|
|
106
|
-
|
107
|
-
|
131
|
+
```ruby
|
132
|
+
MyActiveResource.find(:all, :reload => true)
|
133
|
+
```
|
108
134
|
If you need to clear the entire cache just do the following:
|
109
135
|
|
110
|
-
|
111
|
-
|
136
|
+
```ruby
|
137
|
+
MyActiveResource.clear_cache
|
138
|
+
```
|
112
139
|
---
|
113
140
|
Sometimes you might have a case the resource pathing is non-unique per call. This can create a situation where your caching the same result for multiple calls:
|
114
141
|
|
115
|
-
|
142
|
+
```ruby
|
143
|
+
MyActiveResource.find(:one, from: "/admin/shop.json")
|
144
|
+
```
|
116
145
|
|
117
146
|
Since resources are cached with an argument based key, you may pass in extra data to be appended to the cache key:
|
118
147
|
|
119
|
-
|
120
|
-
|
148
|
+
```ruby
|
149
|
+
MyActiveResource.find(:one, from: "/admin/shop.json", uid: "unique value")
|
150
|
+
```
|
121
151
|
## Testing
|
122
|
-
rake
|
123
152
|
|
124
|
-
|
125
|
-
|
126
|
-
|
153
|
+
```ruby
|
154
|
+
rake
|
155
|
+
```
|
127
156
|
|
128
|
-
|
129
|
-
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.
|
157
|
+
or to test all supported environments, make sure appraisal is setup
|
130
158
|
|
131
|
-
|
132
|
-
|
159
|
+
```ruby
|
160
|
+
bundle exec appraisal install
|
161
|
+
```
|
133
162
|
|
134
|
-
|
135
|
-
* Consider checksums to improve the determination of freshness/changédness
|
163
|
+
and then run
|
136
164
|
|
165
|
+
```ruby
|
166
|
+
bundle exec appraisal rake
|
167
|
+
```
|
137
168
|
|
169
|
+
For more details, head over to the [appraisal](https://github.com/thoughtbot/appraisal) documentation.
|
138
170
|
|
171
|
+
## Credit/Inspiration
|
172
|
+
* quamen and [this gist](http://gist.github.com/947734)
|
173
|
+
* latimes and [this plugin](http://github.com/latimes/cached_resource)
|
139
174
|
|
175
|
+
## Feedback/Problems
|
176
|
+
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.
|
data/gemfiles/4.2.gemfile.lock
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
PATH
|
2
2
|
remote: ..
|
3
3
|
specs:
|
4
|
-
cached_resource (
|
5
|
-
activeresource (>=
|
6
|
-
activesupport (>=
|
4
|
+
cached_resource (5.1.3)
|
5
|
+
activeresource (>= 4.0)
|
6
|
+
activesupport (>= 4.0)
|
7
7
|
nilio (>= 1.0)
|
8
|
-
rake
|
9
8
|
|
10
9
|
GEM
|
11
10
|
remote: http://rubygems.org/
|
@@ -94,7 +93,7 @@ GEM
|
|
94
93
|
rails-deprecated_sanitizer (>= 1.0.1)
|
95
94
|
rails-html-sanitizer (1.0.3)
|
96
95
|
loofah (~> 2.0)
|
97
|
-
rails-observers (0.1.
|
96
|
+
rails-observers (0.1.5)
|
98
97
|
activemodel (>= 4.0)
|
99
98
|
railties (4.2.9)
|
100
99
|
actionpack (= 4.2.9)
|
@@ -134,7 +133,8 @@ DEPENDENCIES
|
|
134
133
|
appraisal
|
135
134
|
cached_resource!
|
136
135
|
rails (~> 4.2.0)
|
136
|
+
rake
|
137
137
|
rspec
|
138
138
|
|
139
139
|
BUNDLED WITH
|
140
|
-
1.
|
140
|
+
1.17.3
|
data/gemfiles/5.0.gemfile.lock
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
PATH
|
2
2
|
remote: ..
|
3
3
|
specs:
|
4
|
-
cached_resource (
|
5
|
-
activeresource (>=
|
6
|
-
activesupport (>=
|
4
|
+
cached_resource (5.1.3)
|
5
|
+
activeresource (>= 4.0)
|
6
|
+
activesupport (>= 4.0)
|
7
7
|
nilio (>= 1.0)
|
8
|
-
rake
|
9
8
|
|
10
9
|
GEM
|
11
10
|
remote: http://rubygems.org/
|
@@ -38,19 +37,18 @@ GEM
|
|
38
37
|
globalid (>= 0.3.6)
|
39
38
|
activemodel (5.0.2)
|
40
39
|
activesupport (= 5.0.2)
|
41
|
-
activemodel-serializers-xml (1.0.
|
40
|
+
activemodel-serializers-xml (1.0.2)
|
42
41
|
activemodel (> 5.x)
|
43
|
-
activerecord (> 5.x)
|
44
42
|
activesupport (> 5.x)
|
45
43
|
builder (~> 3.1)
|
46
44
|
activerecord (5.0.2)
|
47
45
|
activemodel (= 5.0.2)
|
48
46
|
activesupport (= 5.0.2)
|
49
47
|
arel (~> 7.0)
|
50
|
-
activeresource (5.
|
51
|
-
activemodel (>= 5.0, <
|
48
|
+
activeresource (5.1.1)
|
49
|
+
activemodel (>= 5.0, < 7)
|
52
50
|
activemodel-serializers-xml (~> 1.0)
|
53
|
-
activesupport (>= 5.0, <
|
51
|
+
activesupport (>= 5.0, < 7)
|
54
52
|
activesupport (5.0.2)
|
55
53
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
56
54
|
i18n (~> 0.7)
|
@@ -144,7 +142,8 @@ DEPENDENCIES
|
|
144
142
|
appraisal
|
145
143
|
cached_resource!
|
146
144
|
rails (~> 5.0.0)
|
145
|
+
rake
|
147
146
|
rspec
|
148
147
|
|
149
148
|
BUNDLED WITH
|
150
|
-
1.
|
149
|
+
1.17.3
|
data/gemfiles/5.1.gemfile.lock
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
PATH
|
2
2
|
remote: ..
|
3
3
|
specs:
|
4
|
-
cached_resource (
|
5
|
-
activeresource (>=
|
6
|
-
activesupport (>=
|
4
|
+
cached_resource (5.1.3)
|
5
|
+
activeresource (>= 4.0)
|
6
|
+
activesupport (>= 4.0)
|
7
7
|
nilio (>= 1.0)
|
8
|
-
rake
|
9
8
|
|
10
9
|
GEM
|
11
10
|
remote: http://rubygems.org/
|
@@ -38,19 +37,18 @@ GEM
|
|
38
37
|
globalid (>= 0.3.6)
|
39
38
|
activemodel (5.1.2)
|
40
39
|
activesupport (= 5.1.2)
|
41
|
-
activemodel-serializers-xml (1.0.
|
40
|
+
activemodel-serializers-xml (1.0.2)
|
42
41
|
activemodel (> 5.x)
|
43
|
-
activerecord (> 5.x)
|
44
42
|
activesupport (> 5.x)
|
45
43
|
builder (~> 3.1)
|
46
44
|
activerecord (5.1.2)
|
47
45
|
activemodel (= 5.1.2)
|
48
46
|
activesupport (= 5.1.2)
|
49
47
|
arel (~> 8.0)
|
50
|
-
activeresource (5.
|
51
|
-
activemodel (>= 5.0, <
|
48
|
+
activeresource (5.1.1)
|
49
|
+
activemodel (>= 5.0, < 7)
|
52
50
|
activemodel-serializers-xml (~> 1.0)
|
53
|
-
activesupport (>= 5.0, <
|
51
|
+
activesupport (>= 5.0, < 7)
|
54
52
|
activesupport (5.1.2)
|
55
53
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
56
54
|
i18n (~> 0.7)
|
@@ -144,7 +142,8 @@ DEPENDENCIES
|
|
144
142
|
appraisal
|
145
143
|
cached_resource!
|
146
144
|
rails (~> 5.1.0)
|
145
|
+
rake
|
147
146
|
rspec
|
148
147
|
|
149
148
|
BUNDLED WITH
|
150
|
-
1.
|
149
|
+
1.17.3
|
@@ -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,13 +84,19 @@ 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 |
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
87
|
+
object = cached_resource.cache.read(key).try do |json_cache|
|
88
|
+
|
89
|
+
json = ActiveSupport::JSON.decode(json_cache)
|
90
|
+
|
91
|
+
unless json.nil?
|
92
|
+
cache = json_to_object(json)
|
93
|
+
if cache.is_a? Enumerable
|
94
|
+
restored = cache.map { |record| full_dup(record) }
|
95
|
+
next restored unless respond_to?(:collection_parser)
|
96
|
+
collection_parser.new(restored)
|
97
|
+
else
|
98
|
+
full_dup(cache)
|
99
|
+
end
|
94
100
|
end
|
95
101
|
end
|
96
102
|
object && cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} READ #{key}")
|
@@ -99,7 +105,7 @@ module CachedResource
|
|
99
105
|
|
100
106
|
# Write an entry to the cache for the given key and value.
|
101
107
|
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)
|
108
|
+
result = cached_resource.cache.write(key, object_to_json(object), :race_condition_ttl => cached_resource.race_condition_ttl, :expires_in => cached_resource.generate_ttl)
|
103
109
|
result && cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} WRITE #{key}")
|
104
110
|
result
|
105
111
|
end
|
@@ -124,6 +130,24 @@ module CachedResource
|
|
124
130
|
end
|
125
131
|
end
|
126
132
|
|
133
|
+
def json_to_object(json)
|
134
|
+
if json.is_a? Array
|
135
|
+
json.map { |attrs|
|
136
|
+
self.new(attrs["object"], attrs["persistence"]) }
|
137
|
+
else
|
138
|
+
self.new(json["object"], json["persistence"])
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def object_to_json(object)
|
143
|
+
if object.is_a? Enumerable
|
144
|
+
object.map { |o| { :object => o, :persistence => o.persisted? } }.to_json
|
145
|
+
elsif object.nil?
|
146
|
+
nil.to_json
|
147
|
+
else
|
148
|
+
{ :object => object, :persistence => object.persisted? }.to_json
|
149
|
+
end
|
150
|
+
end
|
127
151
|
end
|
128
152
|
end
|
129
153
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe CachedResource do
|
4
|
+
before do
|
5
|
+
class BaseThing < ActiveResource::Base
|
6
|
+
end
|
7
|
+
|
8
|
+
class FirstChildThing < BaseThing
|
9
|
+
self.site = 'http://api.first-child-thing.com'
|
10
|
+
cached_resource
|
11
|
+
end
|
12
|
+
|
13
|
+
class SecondChildThing < BaseThing
|
14
|
+
self.site = 'http://api.second-child-thing.com'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
after do
|
19
|
+
[:BaseThing, :FirstChildThing, :SecondChildThing].each do |klass|
|
20
|
+
Object.send(:remove_const, klass)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '.inherited' do
|
25
|
+
it 'should include descendants when calling .descendants' do
|
26
|
+
BaseThing.descendants.sort_by { |klass| klass.name }.should == [FirstChildThing, SecondChildThing]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
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"
|
@@ -15,10 +19,13 @@ describe CachedResource do
|
|
15
19
|
@thing3 = {:thing => {:id => 3, :name => "Stu"}}
|
16
20
|
@string_thing = {:thing => {:id => "fded", :name => "Lev"}}
|
17
21
|
@other_string_thing = {:thing => {:id => "fded", :name => "Lon"}}
|
22
|
+
@date_thing = {:thing => {:id => 4, :created_at => Time.utc(2020).iso8601}}
|
18
23
|
@thing_json = @thing.to_json
|
19
24
|
@other_thing_json = @other_thing.to_json
|
20
25
|
@string_thing_json = @string_thing.to_json
|
21
26
|
@other_string_thing_json = @other_string_thing.to_json
|
27
|
+
@date_thing_json = @date_thing.to_json
|
28
|
+
@nil_thing = nil.to_json
|
22
29
|
end
|
23
30
|
|
24
31
|
after(:each) do
|
@@ -38,32 +45,41 @@ describe CachedResource do
|
|
38
45
|
mock.get "/things/1.json", {}, @thing_json
|
39
46
|
mock.get "/things/1.json?foo=bar", {}, @thing_json
|
40
47
|
mock.get "/things/fded.json", {}, @string_thing_json
|
48
|
+
mock.get "/things.json?name=42", {}, @nil_thing, 404
|
49
|
+
mock.get "/things/4.json", {}, @date_thing_json
|
41
50
|
end
|
42
51
|
end
|
43
52
|
|
44
53
|
it "should cache a response" do
|
45
54
|
result = Thing.find(1)
|
46
|
-
|
55
|
+
|
56
|
+
read_from_cache("thing/1").should == result
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should cache a nil response" do
|
60
|
+
result = Thing.find(:all, :params => { :name => '42' })
|
61
|
+
read_from_cache("thing/all/name/42").should == nil
|
47
62
|
end
|
48
63
|
|
49
64
|
it "should cache a response for a string primary key" do
|
50
65
|
result = Thing.find("fded")
|
51
|
-
|
66
|
+
read_from_cache("thing/fded").should == result
|
52
67
|
end
|
53
68
|
|
54
69
|
it "should cache without whitespace in keys" do
|
55
70
|
result = Thing.find(1, :from => 'path', :params => { :foo => 'bar' })
|
56
|
-
|
71
|
+
read_from_cache('thing/1/{:from=>"path",:params=>{:foo=>"bar"}}').should == result
|
57
72
|
end
|
58
73
|
|
59
74
|
it "should empty the cache when clear_cache is called" do
|
60
75
|
result = Thing.find(1)
|
61
76
|
Thing.clear_cache
|
62
|
-
|
77
|
+
read_from_cache("thing/1").should == nil
|
63
78
|
end
|
64
79
|
|
65
80
|
it "should cache a response with the same persistence" do
|
66
81
|
result1 = Thing.find(1)
|
82
|
+
result1.persisted?.should be true
|
67
83
|
result2 = Thing.find(1)
|
68
84
|
result1.persisted?.should == result2.persisted?
|
69
85
|
end
|
@@ -108,7 +124,7 @@ describe CachedResource do
|
|
108
124
|
# make a request
|
109
125
|
Thing.find(1)
|
110
126
|
# get the cached result of the request
|
111
|
-
old_result =
|
127
|
+
old_result = read_from_cache("thing/1")
|
112
128
|
|
113
129
|
# change the response
|
114
130
|
ActiveResource::HttpMock.reset!
|
@@ -117,7 +133,7 @@ describe CachedResource do
|
|
117
133
|
end
|
118
134
|
|
119
135
|
Thing.find(1, :reload => true)
|
120
|
-
new_result =
|
136
|
+
new_result = read_from_cache("thing/1")
|
121
137
|
# since active resources are equal if and only if they
|
122
138
|
# are the same object or an instance of the same class,
|
123
139
|
# not new?, and have the same id.
|
@@ -153,10 +169,23 @@ describe CachedResource do
|
|
153
169
|
result1.should_not be_frozen
|
154
170
|
end
|
155
171
|
|
172
|
+
describe "when ActiveSupport.parse_json_times is enabled" do
|
173
|
+
before(:all) do
|
174
|
+
Time.zone = 'UTC'
|
175
|
+
ActiveSupport.parse_json_times = true
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should convert date times to objects when reading from cache" do
|
179
|
+
Thing.find(4)
|
180
|
+
|
181
|
+
read_from_cache("thing/4").created_at.should == @date_thing[:thing][:created_at]
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
156
185
|
shared_examples "collection_return_type" do
|
157
186
|
if ActiveResource::VERSION::MAJOR >= 4
|
158
187
|
it "should return an ActiveResource::Collection" do
|
159
|
-
cached =
|
188
|
+
cached = read_from_cache("thing/all")
|
160
189
|
cached.should be_instance_of(ActiveResource::Collection)
|
161
190
|
end
|
162
191
|
|
@@ -165,12 +194,12 @@ describe CachedResource do
|
|
165
194
|
class CustomCollection < ActiveResource::Collection; end
|
166
195
|
Thing.collection_parser = CustomCollection
|
167
196
|
Thing.all
|
168
|
-
cached =
|
197
|
+
cached = read_from_cache("thing/all")
|
169
198
|
cached.should be_instance_of(CustomCollection)
|
170
199
|
end
|
171
200
|
else
|
172
201
|
it "should return an Array" do
|
173
|
-
cached =
|
202
|
+
cached = read_from_cache("thing/all")
|
174
203
|
cached.should be_instance_of(Array)
|
175
204
|
end
|
176
205
|
end
|
@@ -222,8 +251,8 @@ describe CachedResource do
|
|
222
251
|
shared_examples "collection_cache_clearing" do
|
223
252
|
it "should empty the cache when clear_cache is called" do
|
224
253
|
Thing.clear_cache
|
225
|
-
|
226
|
-
|
254
|
+
read_from_cache("thing/all").should == nil
|
255
|
+
read_from_cache("thing/1").should == nil
|
227
256
|
end
|
228
257
|
|
229
258
|
end
|
@@ -254,8 +283,8 @@ describe CachedResource do
|
|
254
283
|
# only the all request should have been made
|
255
284
|
ActiveResource::HttpMock.requests.length.should == 1
|
256
285
|
# the result should be cached with the appropriate key
|
257
|
-
|
258
|
-
|
286
|
+
read_from_cache("thing/1").should == result
|
287
|
+
read_from_cache("thing/fded").should == string_result
|
259
288
|
end
|
260
289
|
|
261
290
|
include_examples "collection_cache_clearing"
|
@@ -274,11 +303,11 @@ describe CachedResource do
|
|
274
303
|
Thing.all(:reload => true)
|
275
304
|
# get the updated result, read from the cache
|
276
305
|
result = Thing.find(1)
|
277
|
-
|
278
|
-
|
306
|
+
read_from_cache("thing/all")[0].should == result
|
307
|
+
read_from_cache("thing/all")[0].name.should == result.name
|
279
308
|
string_result = Thing.find("fded")
|
280
|
-
|
281
|
-
|
309
|
+
read_from_cache("thing/all")[1].should == string_result
|
310
|
+
read_from_cache("thing/all")[1].name.should == string_result.name
|
282
311
|
end
|
283
312
|
|
284
313
|
it "should update the collection when an individual request is reloaded" do
|
@@ -291,11 +320,11 @@ describe CachedResource do
|
|
291
320
|
|
292
321
|
# reload the individual
|
293
322
|
result = Thing.find(1, :reload => true)
|
294
|
-
|
295
|
-
|
323
|
+
read_from_cache("thing/all")[0].should == result
|
324
|
+
read_from_cache("thing/all")[0].name.should == result.name
|
296
325
|
string_result = Thing.find("fded", :reload => true)
|
297
|
-
|
298
|
-
|
326
|
+
read_from_cache("thing/all")[1].should == string_result
|
327
|
+
read_from_cache("thing/all")[1].name.should == string_result.name
|
299
328
|
end
|
300
329
|
|
301
330
|
it "should update both the collection and the member cache entries when a subset of the collection is retrieved" do
|
@@ -312,20 +341,20 @@ describe CachedResource do
|
|
312
341
|
# make a request for a subset of the "mother" collection
|
313
342
|
result = Thing.find(:all, :params => {:name => "Ari"})
|
314
343
|
# the collection should be updated to reflect the server change
|
315
|
-
|
316
|
-
|
344
|
+
read_from_cache("thing/all")[0].should == result[0]
|
345
|
+
read_from_cache("thing/all")[0].name.should == result[0].name
|
317
346
|
# the individual should be updated to reflect the server change
|
318
|
-
|
319
|
-
|
347
|
+
read_from_cache("thing/1").should == result[0]
|
348
|
+
read_from_cache("thing/1").name.should == result[0].name
|
320
349
|
|
321
350
|
# make a request for a subset of the "mother" collection
|
322
351
|
result = Thing.find(:all, :params => {:name => "Lon"})
|
323
352
|
# the collection should be updated to reflect the server change
|
324
|
-
|
325
|
-
|
353
|
+
read_from_cache("thing/all")[1].should == result[0]
|
354
|
+
read_from_cache("thing/all")[1].name.should == result[0].name
|
326
355
|
# the individual should be updated to reflect the server change
|
327
|
-
|
328
|
-
|
356
|
+
read_from_cache("thing/fded").should == result[0]
|
357
|
+
read_from_cache("thing/fded").name.should == result[0].name
|
329
358
|
end
|
330
359
|
|
331
360
|
it "should maintain the order of the collection when updating it" do
|
@@ -407,12 +436,12 @@ describe CachedResource do
|
|
407
436
|
# reload the individual
|
408
437
|
result = Thing.find(1, :reload => true)
|
409
438
|
# the ids are the same, but the names should be different
|
410
|
-
|
439
|
+
read_from_cache("thing/all")[0].name.should_not == result.name
|
411
440
|
|
412
441
|
# reload the individual
|
413
442
|
string_result = Thing.find("fded", :reload => true)
|
414
443
|
# the ids are the same, but the names should be different
|
415
|
-
|
444
|
+
read_from_cache("thing/all")[1].name.should_not == string_result.name
|
416
445
|
end
|
417
446
|
end
|
418
447
|
|
@@ -452,12 +481,12 @@ describe CachedResource do
|
|
452
481
|
|
453
482
|
it "should cache a response" do
|
454
483
|
result = Thing.find(1)
|
455
|
-
|
484
|
+
read_from_cache("thing/1").should == result
|
456
485
|
end
|
457
486
|
|
458
487
|
it "should cache a response for a string primary key" do
|
459
488
|
result = Thing.find("fded")
|
460
|
-
|
489
|
+
read_from_cache("thing/fded").should == result
|
461
490
|
end
|
462
491
|
|
463
492
|
it "should always remake the request" do
|
@@ -476,7 +505,7 @@ describe CachedResource do
|
|
476
505
|
|
477
506
|
it "should rewrite the cache for each request" do
|
478
507
|
Thing.find(1)
|
479
|
-
old_result =
|
508
|
+
old_result = read_from_cache("thing/1")
|
480
509
|
|
481
510
|
# change the response
|
482
511
|
ActiveResource::HttpMock.reset!
|
@@ -485,7 +514,7 @@ describe CachedResource do
|
|
485
514
|
end
|
486
515
|
|
487
516
|
Thing.find(1)
|
488
|
-
new_result =
|
517
|
+
new_result = read_from_cache("thing/1")
|
489
518
|
# since active resources are equal if and only if they
|
490
519
|
# are the same object or an instance of the same class,
|
491
520
|
# not new?, and have the same id.
|
@@ -494,7 +523,7 @@ describe CachedResource do
|
|
494
523
|
|
495
524
|
it "should rewrite the cache for each request for a string primary key" do
|
496
525
|
Thing.find("fded")
|
497
|
-
old_result =
|
526
|
+
old_result = read_from_cache("thing/fded")
|
498
527
|
|
499
528
|
# change the response
|
500
529
|
ActiveResource::HttpMock.reset!
|
@@ -503,7 +532,7 @@ describe CachedResource do
|
|
503
532
|
end
|
504
533
|
|
505
534
|
Thing.find("fded")
|
506
|
-
new_result =
|
535
|
+
new_result = read_from_cache("thing/fded")
|
507
536
|
# since active resources are equal if and only if they
|
508
537
|
# are the same object or an instance of the same class,
|
509
538
|
# 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.
|
4
|
+
version: 5.1.3
|
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: 2020-11-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activeresource
|
@@ -120,6 +120,7 @@ files:
|
|
120
120
|
- lib/cached_resource/caching.rb
|
121
121
|
- lib/cached_resource/configuration.rb
|
122
122
|
- lib/cached_resource/version.rb
|
123
|
+
- spec/cached_resource/cached_resource_spec.rb
|
123
124
|
- spec/cached_resource/caching_spec.rb
|
124
125
|
- spec/cached_resource/configuration_spec.rb
|
125
126
|
- spec/spec_helper.rb
|
@@ -142,12 +143,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
143
|
- !ruby/object:Gem::Version
|
143
144
|
version: '0'
|
144
145
|
requirements: []
|
145
|
-
|
146
|
-
rubygems_version: 2.5.2
|
146
|
+
rubygems_version: 3.0.8
|
147
147
|
signing_key:
|
148
148
|
specification_version: 4
|
149
149
|
summary: Caching for ActiveResource
|
150
|
-
test_files:
|
151
|
-
- spec/cached_resource/caching_spec.rb
|
152
|
-
- spec/cached_resource/configuration_spec.rb
|
153
|
-
- spec/spec_helper.rb
|
150
|
+
test_files: []
|