cached_resource 5.1.0 → 5.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +81 -40
- data/gemfiles/4.2.gemfile.lock +2 -2
- data/gemfiles/5.0.gemfile.lock +2 -2
- data/gemfiles/5.1.gemfile.lock +2 -2
- data/lib/cached_resource/caching.rb +12 -4
- data/lib/cached_resource/version.rb +1 -1
- data/spec/cached_resource/caching_spec.rb +1 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f2b25cc83718d786c0441bc7cb082944ac7aa4fe3d379792cacb96e87df668eb
|
4
|
+
data.tar.gz: 8fa4f263faee071d9e7361f27b8927113bb36e76b1e5e765e993935cadad8c22
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0523763d1f6d1dd9eb1d68ddb2a196e8f737d5660c91bcb244374804b0ddc31f50037ebb19c0dc1a25b891586cf3b5025df60b16a11c209e03f0510147e78514
|
7
|
+
data.tar.gz: 8ae89f1cd8fcf8dccedc053c7acc15b76ea84853240b0998ee31b5a17f837d001696866262182787d315d4f432f67d87b1ef4d9ef918b530b4e884f4e2f88363
|
data/README.md
CHANGED
@@ -2,7 +2,10 @@
|
|
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,81 +56,115 @@ 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
|
-
|
153
|
+
```ruby
|
154
|
+
rake
|
155
|
+
```
|
156
|
+
|
157
|
+
or to test all supported environments, make sure appraisal is setup
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
bundle exec appraisal install
|
161
|
+
```
|
162
|
+
|
163
|
+
and then run
|
125
164
|
|
126
|
-
|
165
|
+
```ruby
|
166
|
+
bundle exec appraisal rake
|
167
|
+
```
|
127
168
|
|
128
169
|
For more details, head over to the [appraisal](https://github.com/thoughtbot/appraisal) documentation.
|
129
170
|
|
data/gemfiles/4.2.gemfile.lock
CHANGED
data/gemfiles/5.0.gemfile.lock
CHANGED
data/gemfiles/5.1.gemfile.lock
CHANGED
@@ -85,7 +85,7 @@ module CachedResource
|
|
85
85
|
# Read a entry from the cache for the given key.
|
86
86
|
def cache_read(key)
|
87
87
|
object = cached_resource.cache.read(key).try do |json_cache|
|
88
|
-
cache = json_to_object(JSON.parse(json_cache))
|
88
|
+
cache = json_to_object(JSON.parse(json_cache, :symbolize_names => true))
|
89
89
|
if cache.is_a? Enumerable
|
90
90
|
restored = cache.map { |record| full_dup(record) }
|
91
91
|
next restored unless respond_to?(:collection_parser)
|
@@ -100,7 +100,7 @@ module CachedResource
|
|
100
100
|
|
101
101
|
# Write an entry to the cache for the given key and value.
|
102
102
|
def cache_write(key, object)
|
103
|
-
result = cached_resource.cache.write(key, object
|
103
|
+
result = cached_resource.cache.write(key, object_to_json(object), :race_condition_ttl => cached_resource.race_condition_ttl, :expires_in => cached_resource.generate_ttl)
|
104
104
|
result && cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} WRITE #{key}")
|
105
105
|
result
|
106
106
|
end
|
@@ -127,12 +127,20 @@ module CachedResource
|
|
127
127
|
|
128
128
|
def json_to_object(json)
|
129
129
|
if json.is_a? Array
|
130
|
-
json.map { |attrs|
|
130
|
+
json.map { |attrs|
|
131
|
+
self.new(attrs[:object], attrs[:persistence]) }
|
131
132
|
else
|
132
|
-
self.new(json)
|
133
|
+
self.new(json[:object], json[:persistence])
|
133
134
|
end
|
134
135
|
end
|
135
136
|
|
137
|
+
def object_to_json(object)
|
138
|
+
if object.is_a? Enumerable
|
139
|
+
object.map { |o| { :object => o, :persistence => o.persisted? } }.to_json
|
140
|
+
else
|
141
|
+
{ :object => object, :persistence => object.persisted? }.to_json
|
142
|
+
end
|
143
|
+
end
|
136
144
|
end
|
137
145
|
end
|
138
146
|
end
|
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.1.
|
4
|
+
version: 5.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Morgan Brown
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-11-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activeresource
|
@@ -144,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
144
144
|
version: '0'
|
145
145
|
requirements: []
|
146
146
|
rubyforge_project:
|
147
|
-
rubygems_version: 2.
|
147
|
+
rubygems_version: 2.7.6
|
148
148
|
signing_key:
|
149
149
|
specification_version: 4
|
150
150
|
summary: Caching for ActiveResource
|