cached_resource 7.2.0 → 9.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md DELETED
@@ -1,208 +0,0 @@
1
- # CachedResource ![Tests](https://github.com/mhgbrown/cached_resource/actions/workflows/ruby.yml/badge.svg)
2
-
3
- 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.
4
-
5
- ## Installation
6
-
7
- ```ruby
8
- gem install cached_resource
9
- ```
10
-
11
- ## Compatibility
12
-
13
- CachedResource is designed to be framework agnostic, but will hook into Rails for caching and logging if available. CachedResource supports the following ActiveSupport/Rails (right) and Ruby (down) version combinations:
14
-
15
- | | 🛤️ 4.2 | 🛤️ 5.0 | 🛤️ 5.1 | 🛤️ 6.0 | 🛤️ 6.1 | 🛤️ 7.0 |
16
- |-------|-----|-----|-----|-----|-----|-----|
17
- | 💎 1.9 | ✅ | | | | | |
18
- | 💎 2.2 | ✅ | | | | | |
19
- | 💎 2.3 | ✅ | ✅ | ✅ | | | |
20
- | 💎 2.4 | ✅ | ✅ | ✅ | | | |
21
- | 💎 2.5 | ✅ | ✅ | ✅ | ✅ | ✅ | |
22
- | 💎 2.6 | ✅ | ✅ | ✅ | ✅ | ✅ | |
23
- | 💎 2.7 | | ✅ | ✅ | ✅ | ✅ | ✅ |
24
- | 💎 3.0 | | | | ✅ | ✅ | ✅ |
25
- | 💎 3.1 | | | | ✅ | ✅ | ✅ |
26
- | 💎 3.2 | | | | ✅ | ✅ | ✅ |
27
-
28
- ## Limitations
29
-
30
- The following are limitations for ActiveResource/Rails versions
31
-
32
- | ActiveSupport Version | Limitation |
33
- |---------------------- | ----------------------------------------------------------------------- |
34
- | 🛤️ 4.X | You cannot chain calls. Ie `Thing.where(fn: 'foo').where(ln: 'bar')`. <br> However, you can still access `original_params` and the `resource_class` and replicate it with <br>`call1 = Thing.where(fn: 'foo')`<br>`call1.resource_class.where(call1.original_params.merge(ln: 'bar'))` |
35
-
36
- ## Configuration
37
-
38
- **Set up CachedResource across all ActiveResources:**
39
-
40
- ```ruby
41
- class ActiveResource::Base
42
- cached_resource
43
- end
44
- ```
45
-
46
- Or set up CachedResource for a single class:
47
-
48
- ```ruby
49
- class MyActiveResource < ActiveResource::Base
50
- cached_resource
51
- end
52
- ```
53
-
54
- ### Options
55
- CachedResource accepts the following options:
56
-
57
- * `:enabled` Default: `true`
58
- * `:ttl` The time in seconds until the cache should expire. Default: `604800`
59
- * `:race_condition_ttl` The race condition ttl, to prevent [dog pile effect](https://en.wikipedia.org/wiki/Cache_stampede) or [cache stampede](https://en.wikipedia.org/wiki/Cache_stampede). Default: 86400
60
- * `:ttl_randomization` Enable ttl randomization. Default: `false`
61
- * `:ttl_randomization_scale` A Range from which a random value will be selected to scale the ttl. Default: `1..2`
62
- * `:collection_synchronize` Use collections to generate cache entries for individuals. Update the existing cached principal collection when retrieving subsets of the principal collection or individuals. Default: `false`
63
- * `:collection_arguments` The arguments that identify the principal collection request. Default: `[:all]`
64
- * `:logger` The logger to which CachedResource messages should be written. Default: The `Rails.logger` if available, or an `ActiveSupport::Logger`
65
- * `:cache` The cache store that CacheResource should use. Default: The `Rails.cache` if available, or an `ActiveSupport::Cache::MemoryStore`
66
- * `:cache_collections` Set to false to always remake a request for collections. Default: `true`
67
-
68
- You can set them like this:
69
-
70
- ```ruby
71
- cached_resource :cache => MyCacheStore.new, :ttl => 60, :collection_synchronize => true, :logger => MyLogger.new
72
- ```
73
-
74
- You can also change them on the fly.
75
-
76
- Turn CachedResource off. This will cause all responses to be retrieved normally (i.e. via the network). All responses will still be cached.
77
-
78
- ```ruby
79
- MyActiveResource.cached_resource.off!
80
- ```
81
-
82
- Turn CachedResource on.
83
- ```ruby
84
- MyActiveResource.cached_resource.on!
85
- ```
86
-
87
- Set the cache expiry time to 60 seconds.
88
-
89
- ```ruby
90
- MyActiveResource.cached_resource.ttl = 60
91
- ```
92
-
93
- Enable cache expiry time randomization, allowing it to fall randomly between 60 and 120 seconds.
94
-
95
- ```ruby
96
- MyActiveResource.cached_resource.ttl_randomization = true
97
- ```
98
-
99
- Change the cache expiry time randomization scale so that the cache expiry time falls randomly between 30 and 180 seconds.
100
-
101
- ```ruby
102
- MyActiveResource.cached_resource.ttl_randomization_scale = 0.5..3
103
- ```
104
- 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.
105
-
106
- ```ruby
107
- MyActiveResource.cached_resource.collection_synchronize = true
108
- ```
109
- 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.
110
-
111
- ```ruby
112
- MyActiveResource.cached_resource.collection_arguments = [:all, :params => {:name => "Bob"}]
113
- ```
114
- Set a different logger.
115
-
116
- ```ruby
117
- MyActiveResource.cached_resource.logger = MyLogger.new
118
- ```
119
- Set a different cache store.
120
-
121
- ```ruby
122
- MyActiveResource.cached_resource.cache = MyCacheStore.new
123
- ```
124
-
125
- ### Caveats
126
- 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:
127
-
128
- ```ruby
129
- class ActiveResource::Base
130
- cached_resource
131
- end
132
- ```
133
-
134
- ```ruby
135
- class MyActiveResource < ActiveResource::Base
136
- self.cached_resource = CachedResource::Configuration.new(:collection_synchronize => true)
137
- end
138
- ```
139
- ## Usage
140
- Sit back and relax! If you need to reload a particular request you can pass `:reload => true` into the options hash like this:
141
-
142
- ```ruby
143
- MyActiveResource.find(:all, :reload => true)
144
- ```
145
- If you need to clear the entire cache just do the following:
146
-
147
- ```ruby
148
- MyActiveResource.clear_cache
149
- ```
150
- ---
151
- 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:
152
-
153
- ```ruby
154
- MyActiveResource.find(:one, from: "/admin/shop.json")
155
- ```
156
-
157
- Since resources are cached with an argument based key, you may pass in extra data to be appended to the cache key:
158
-
159
- ```ruby
160
- MyActiveResource.find(:one, from: "/admin/shop.json", uid: "unique value")
161
- ```
162
-
163
- ## Testing
164
-
165
- To test the Ruby + Rails combination configured by default:
166
-
167
- ```bash
168
- $ rake
169
- ```
170
-
171
- or to test all supported environments...you have to do a little more work...
172
-
173
- Switch your Ruby version to the desired version. This project's maintainer uses `asdf`, so switching to Ruby 3 looks like this:
174
-
175
- ```bash
176
- $ asdf local ruby 3.0.5
177
- ```
178
-
179
- If you have a `Gemfile.lock`, delete it:
180
-
181
- ```bash
182
- $ rm Gemfile.lock
183
- ```
184
-
185
- Then reinstall your dependencies:
186
-
187
- ```bash
188
- $ bundle install
189
- ```
190
-
191
- and finally, run the tests:
192
-
193
- ```bash
194
- $ rake
195
- ```
196
-
197
- If you want to test with a specific Rails version, start over and install dependencies with `TEST_RAILS_VERSION` set:
198
-
199
- ```bash
200
- $ TEST_RAILS_VERSION=6.1 bundle install
201
- ```
202
-
203
- ## Credit/Inspiration
204
- * quamen and [this gist](http://gist.github.com/947734)
205
- * latimes and [this plugin](http://github.com/latimes/cached_resource)
206
-
207
- ## Feedback/Problems
208
- 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/Rakefile DELETED
@@ -1,7 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require 'rspec/core/rake_task'
3
-
4
- desc "Run all examples"
5
- RSpec::Core::RakeTask.new(:spec)
6
-
7
- task :default => :spec
@@ -1,28 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "cached_resource/version"
4
-
5
- Gem::Specification.new do |s|
6
- s.name = "cached_resource"
7
- s.version = CachedResource::VERSION
8
- s.authors = "Morgan Brown"
9
- s.email = "cached_resource@email.mhgbrown.is"
10
- s.homepage = "https://github.com/mhgbrown/cached_resource"
11
- s.summary = %q{Caching for ActiveResource}
12
- s.description = %q{Enables request-based caching for ActiveResource}
13
- s.licenses = ['MIT']
14
-
15
- s.files = `git ls-files`.split("\n")
16
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
- s.require_paths = ["lib"]
19
-
20
- s.required_ruby_version = '>= 1.9.0'
21
-
22
- s.add_runtime_dependency "activeresource", ">= 4.0"
23
- s.add_runtime_dependency "activesupport", ">= 4.0"
24
- s.add_runtime_dependency "nilio", ">= 1.0"
25
-
26
- s.add_development_dependency "rake"
27
- s.add_development_dependency "rspec"
28
- end
data/gemfiles/4.2.gemfile DELETED
@@ -1,6 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- gem "rails", "~> 4.2.0"
4
-
5
- # https://stackoverflow.com/questions/60226893/rails-nomethoderror-undefined-method-new-for-bigdecimalclass
6
- # gem "bigdecimal", "1.3.5"
data/gemfiles/5.0.gemfile DELETED
@@ -1,3 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- gem "rails", "~> 5.0.0"
data/gemfiles/5.1.gemfile DELETED
@@ -1,3 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- gem "rails", "~> 5.1.0"
data/gemfiles/6.0.gemfile DELETED
@@ -1,3 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- gem "rails", "~> 6.0.0"
data/gemfiles/6.1.gemfile DELETED
@@ -1,3 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- gem "rails", "~> 6.1.0"
data/gemfiles/7.0.gemfile DELETED
@@ -1,3 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- gem "rails", "~> 7.0.0"
@@ -1,29 +0,0 @@
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