readthis 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c15a08e16c0c3499ba92207c32a0fb3cd07d6bea
4
- data.tar.gz: 77b6b856ee036051b59a83c7a2013af49ef4c898
3
+ metadata.gz: 3e47cddbb974aaa2df5258b6279db5ac65ac1b7c
4
+ data.tar.gz: d729c1a37bbd6b2ab51d20ff20944e4b51aab561
5
5
  SHA512:
6
- metadata.gz: e1c2bd1bcadb0b1d4636de110c1202adf9f4c197bc189ef5282d86f4765612d5383ec9792d435b0629012732151e5a7468a965cc7d99a8b14e9e95100d6312af
7
- data.tar.gz: 8ab80c80422fa2ad11d70e03ad6697c6a5d5d78fc76460b27cf1a332b023b9b0419c40020d291cc8db01638844dfe33a693c12a59e1d28474b9f858dc06998cf
6
+ metadata.gz: 5b9e58cf2f1dda31681d653e770153d190b1fb5958e52881222b8a350cf94b71eac28c9043e56d1d2ab6673952db14b63872d52452006926cf2818d74837961d
7
+ data.tar.gz: f4950c2133cd938ee7e5b9da6a4ed5605506ef4da347b0b34379ad07b97fcc66d2c63dab6c56ddba72e41a4490313338517360860924bd7a5171fb9d21561b05
data/README.md CHANGED
@@ -137,6 +137,19 @@ sticky and a flag is stored with each cached value. If you subsequently go to
137
137
  deserialize values and haven't configured the same serializers in the same order
138
138
  your application will raise errors.
139
139
 
140
+ ## Fault Tolerance
141
+
142
+ In some situations it is desirable to keep serving requests from disk or the
143
+ database if Redis crashes. This can be achieved wiht connection fault tolerance
144
+ by enabling it at the top level:
145
+
146
+ ```ruby
147
+ Readthis.fault_tolerant = true
148
+ ```
149
+
150
+ The default value is `false`, because while it may work for `fetch` operations,
151
+ it isn't compatible with other state-based commands like `increment`.
152
+
140
153
  ## Differences From ActiveSupport::Cache
141
154
 
142
155
  Readthis supports all of standard cache methods except for the following:
data/lib/readthis.rb CHANGED
@@ -16,4 +16,27 @@ module Readthis
16
16
  def serializers
17
17
  @serializers ||= Readthis::Serializers.new
18
18
  end
19
+
20
+ # Indicates whether connection error tolerance is enabled. With tolerance
21
+ # enabled every operation will return a `nil` value.
22
+ #
23
+ # @returns [Boolean] True for enabled, false for disabled
24
+ #
25
+ def fault_tolerant?
26
+ @fault_tolerant
27
+ end
28
+
29
+ # Toggle fault tolerance for connection errors.
30
+ #
31
+ # @param [Boolean] The new value for fault tolerance
32
+ #
33
+ def fault_tolerant=(value)
34
+ @fault_tolerant = value
35
+ end
36
+
37
+ # @private
38
+ def reset!
39
+ @fault_tolerant = nil
40
+ @serializers = nil
41
+ end
19
42
  end
@@ -345,6 +345,8 @@ module Readthis
345
345
  instrument(operation, key) do
346
346
  pool.with(&block)
347
347
  end
348
+ rescue Redis::BaseError => error
349
+ raise error unless Readthis.fault_tolerant?
348
350
  end
349
351
 
350
352
  def extract_options!(array)
@@ -1,3 +1,3 @@
1
1
  module Readthis
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
@@ -48,7 +48,7 @@ RSpec.describe Readthis::Cache do
48
48
  end
49
49
 
50
50
  it 'uses a custom expiration' do
51
- cache = Readthis::Cache.new(namespace: 'cache', expires_in: 86400)
51
+ cache = Readthis::Cache.new(namespace: 'cache', expires_in: 86_400)
52
52
 
53
53
  cache.write('some-key', 'some-value')
54
54
  cache.write('other-key', 'other-value', expires_in: 1)
@@ -166,6 +166,16 @@ RSpec.describe Readthis::Cache do
166
166
  cache.write('great-key', 'great')
167
167
  expect(cache.fetch('great-key', nil)).to eq('great')
168
168
  end
169
+
170
+ it 'serves computed content when the cache is down and tolerance is enabled' do
171
+ Readthis.fault_tolerant = true
172
+
173
+ allow(cache.pool).to receive(:with).and_raise(Redis::CannotConnectError)
174
+
175
+ computed = cache.fetch('error-key') { 'computed' }
176
+
177
+ expect(computed).to eq('computed')
178
+ end
169
179
  end
170
180
 
171
181
  describe '#read_multi' do
@@ -6,4 +6,16 @@ RSpec.describe Readthis do
6
6
  expect(Readthis.serializers.marshals).to include(Marshal, JSON)
7
7
  end
8
8
  end
9
+
10
+ describe '#fault_tolerant?' do
11
+ it 'defaults to being false' do
12
+ expect(Readthis).not_to be_fault_tolerant
13
+ end
14
+
15
+ it 'can be enabled' do
16
+ Readthis.fault_tolerant = true
17
+
18
+ expect(Readthis).to be_fault_tolerant
19
+ end
20
+ end
9
21
  end
data/spec/spec_helper.rb CHANGED
@@ -20,4 +20,8 @@ RSpec.configure do |config|
20
20
 
21
21
  config.order = :random
22
22
  Kernel.srand config.seed
23
+
24
+ config.before do
25
+ Readthis.reset!
26
+ end
23
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: readthis
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Parker Selbert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-08 00:00:00.000000000 Z
11
+ date: 2015-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis