cached_resource 8.0.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,209 +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 | 🛤️ 7.1 |
16
- |-------|-----|-----|-----|-----|-----|-----|-----|
17
- | 💎 2.3 | ✅ | ✅ | ✅ | | | | |
18
- | 💎 2.4 | ✅ | ✅ | ✅ | | | | |
19
- | 💎 2.5 | ✅ | ✅ | ✅ | ✅ | ✅ | | |
20
- | 💎 2.6 | ✅ | ✅ | ✅ | ✅ | ✅ | |
21
- | 💎 2.7 | | ✅ | ✅ | ✅ | ✅ | ✅ |✅ |
22
- | 💎 3.0 | | | | ✅ | ✅ | ✅ | ✅ |
23
- | 💎 3.1 | | | | ✅ | ✅ | ✅ | ✅ |
24
- | 💎 3.2 | | | | ✅ | ✅ | ✅ | ✅ |
25
- | 💎 3.3 | | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
26
-
27
- ## Limitations
28
-
29
- The following are limitations for ActiveResource/Rails versions
30
-
31
- | ActiveSupport Version | Limitation |
32
- |---------------------- | ----------------------------------------------------------------------- |
33
- | 🛤️ 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'))` |
34
-
35
- ## Configuration
36
-
37
- **Set up CachedResource across all ActiveResources:**
38
-
39
- ```ruby
40
- class ActiveResource::Base
41
- cached_resource
42
- end
43
- ```
44
-
45
- Or set up CachedResource for a single class:
46
-
47
- ```ruby
48
- class MyActiveResource < ActiveResource::Base
49
- cached_resource
50
- end
51
- ```
52
-
53
- ### Options
54
- CachedResource accepts the following options:
55
-
56
- * `:enabled` Default: `true`
57
- * `:cache_collections` Set to false to always remake a request for collections. Default: `true`
58
- * `:cache` The cache store that CacheResource should use. Default: The `Rails.cache` if available, or an `ActiveSupport::Cache::MemoryStore`
59
- * `:collection_arguments` The arguments that identify the principal collection request. Default: `[:all]`
60
- * `: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`
61
- * `:concurrent_write` Set to true to make concurrent writes to cache after successful API response. Default: `false`
62
- * Requires the [concurrent-ruby](https://rubygems.org/gems/concurrent-ruby) gem
63
- * `:logger` The logger to which CachedResource messages should be written. Default: The `Rails.logger` if available, or an `ActiveSupport::Logger`
64
- * `: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
65
- * `:ttl_randomization_scale` A Range from which a random value will be selected to scale the ttl. Default: `1..2`
66
- * `:ttl_randomization` Enable ttl randomization. Default: `false`
67
- * `:ttl` The time in seconds until the cache should expire. Default: `604800`
68
-
69
- You can set them like this:
70
-
71
- ```ruby
72
- cached_resource :cache => MyCacheStore.new, :ttl => 60, :collection_synchronize => true, :logger => MyLogger.new
73
- ```
74
-
75
- You can also change them on the fly.
76
-
77
- Turn CachedResource off. This will cause all responses to be retrieved normally (i.e. via the network). All responses will still be cached.
78
-
79
- ```ruby
80
- MyActiveResource.cached_resource.off!
81
- ```
82
-
83
- Turn CachedResource on.
84
- ```ruby
85
- MyActiveResource.cached_resource.on!
86
- ```
87
-
88
- Set the cache expiry time to 60 seconds.
89
-
90
- ```ruby
91
- MyActiveResource.cached_resource.ttl = 60
92
- ```
93
-
94
- Enable cache expiry time randomization, allowing it to fall randomly between 60 and 120 seconds.
95
-
96
- ```ruby
97
- MyActiveResource.cached_resource.ttl_randomization = true
98
- ```
99
-
100
- Change the cache expiry time randomization scale so that the cache expiry time falls randomly between 30 and 180 seconds.
101
-
102
- ```ruby
103
- MyActiveResource.cached_resource.ttl_randomization_scale = 0.5..3
104
- ```
105
- 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.
106
-
107
- ```ruby
108
- MyActiveResource.cached_resource.collection_synchronize = true
109
- ```
110
- 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.
111
-
112
- ```ruby
113
- MyActiveResource.cached_resource.collection_arguments = [:all, :params => {:name => "Bob"}]
114
- ```
115
- Set a different logger.
116
-
117
- ```ruby
118
- MyActiveResource.cached_resource.logger = MyLogger.new
119
- ```
120
- Set a different cache store.
121
-
122
- ```ruby
123
- MyActiveResource.cached_resource.cache = MyCacheStore.new
124
- ```
125
-
126
- ### Caveats
127
- 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:
128
-
129
- ```ruby
130
- class ActiveResource::Base
131
- cached_resource
132
- end
133
- ```
134
-
135
- ```ruby
136
- class MyActiveResource < ActiveResource::Base
137
- self.cached_resource = CachedResource::Configuration.new(:collection_synchronize => true)
138
- end
139
- ```
140
- ## Usage
141
- Sit back and relax! If you need to reload a particular request you can pass `:reload => true` into the options hash like this:
142
-
143
- ```ruby
144
- MyActiveResource.find(:all, :reload => true)
145
- ```
146
- If you need to clear the entire cache just do the following:
147
-
148
- ```ruby
149
- MyActiveResource.clear_cache
150
- ```
151
- ---
152
- 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:
153
-
154
- ```ruby
155
- MyActiveResource.find(:one, from: "/admin/shop.json")
156
- ```
157
-
158
- Since resources are cached with an argument based key, you may pass in extra data to be appended to the cache key:
159
-
160
- ```ruby
161
- MyActiveResource.find(:one, from: "/admin/shop.json", uid: "unique value")
162
- ```
163
-
164
- ## Testing
165
-
166
- To test the Ruby + Rails combination configured by default:
167
-
168
- ```bash
169
- $ rake
170
- ```
171
-
172
- or to test all supported environments...you have to do a little more work...
173
-
174
- Switch your Ruby version to the desired version. This project's maintainer uses `asdf`, so switching to Ruby 3 looks like this:
175
-
176
- ```bash
177
- $ asdf local ruby 3.0.5
178
- ```
179
-
180
- If you have a `Gemfile.lock`, delete it:
181
-
182
- ```bash
183
- $ rm Gemfile.lock
184
- ```
185
-
186
- Then reinstall your dependencies:
187
-
188
- ```bash
189
- $ bundle install
190
- ```
191
-
192
- and finally, run the tests:
193
-
194
- ```bash
195
- $ rake
196
- ```
197
-
198
- If you want to test with a specific Rails version, start over and install dependencies with `TEST_RAILS_VERSION` set:
199
-
200
- ```bash
201
- $ TEST_RAILS_VERSION=6.1 bundle install
202
- ```
203
-
204
- ## Credit/Inspiration
205
- * quamen and [this gist](http://gist.github.com/947734)
206
- * latimes and [this plugin](http://github.com/latimes/cached_resource)
207
-
208
- ## Feedback/Problems
209
- 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,29 +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
- s.add_development_dependency "concurrent-ruby", '~> 1.2', '>= 1.2.3'
29
- 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"
data/gemfiles/7.1.gemfile DELETED
@@ -1,3 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- gem "rails", "~> 7.1.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