Skip to content

Commit c83e690

Browse files
author
Nicholas Stuart
committed
Revert the raise_errors param and Removed rescue block from ActiveSupport::Cache::RedisCacheStore#handle_exception
1 parent 22b000b commit c83e690

File tree

4 files changed

+24
-40
lines changed

4 files changed

+24
-40
lines changed

activesupport/CHANGELOG.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
* Add `raise_errors` to `redis_cache_store`, defaults to false
1+
* Removed rescue block from `ActiveSupport::Cache::RedisCacheStore#handle_exception`
22

3-
Previously if an `::Redis::BaseError` was throw while performing a `Rails.cache` operation,
4-
it would result in cache-miss like behavior. This allows cache implementors who want to handle
5-
these connection issues on their own have an option to do so.
6-
7-
```ruby
8-
cache_servers = %w(redis://cache-01:6379/0 redis://cache-02:6379/0)
9-
config.cache_store = :redis_cache_store, { url: cache_servers,
10-
raise_errors: true # Defaults to false. Raise Redis::BaseError instead of dropping them.
11-
}
12-
```
3+
Previously, if you provided a `error_handler` to `redis_cache_store`, any errors thrown by
4+
the error handler would be rescued and logged only. Removed the `rescue` clause from `handle_exception`
5+
to allow these to be thrown.
136

147
* Allow entirely opting out of deprecation warnings.
158

activesupport/lib/active_support/cache/redis_cache_store.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,11 @@ def build_redis_client(url:, **redis_options)
140140
# Race condition TTL is not set by default. This can be used to avoid
141141
# "thundering herd" cache writes when hot cache entries are expired.
142142
# See <tt>ActiveSupport::Cache::Store#fetch</tt> for more.
143-
def initialize(namespace: nil, compress: true, compress_threshold: 1.kilobyte, coder: default_coder, expires_in: nil, race_condition_ttl: nil, error_handler: DEFAULT_ERROR_HANDLER, raise_errors: false, **redis_options)
143+
def initialize(namespace: nil, compress: true, compress_threshold: 1.kilobyte, coder: default_coder, expires_in: nil, race_condition_ttl: nil, error_handler: DEFAULT_ERROR_HANDLER, **redis_options)
144144
@redis_options = redis_options
145145

146146
@max_key_bytesize = MAX_KEY_BYTESIZE
147147
@error_handler = error_handler
148-
@raise_errors = raise_errors
149148

150149
super namespace: namespace,
151150
compress: compress, compress_threshold: compress_threshold,
@@ -461,20 +460,13 @@ def failsafe(method, returning: nil)
461460
yield
462461
rescue ::Redis::BaseError => e
463462
handle_exception exception: e, method: method, returning: returning
464-
465-
if @raise_errors
466-
raise e
467-
else
468-
returning
469-
end
463+
returning
470464
end
471465

472466
def handle_exception(exception:, method:, returning:)
473467
if @error_handler
474468
@error_handler.(method: method, exception: exception, returning: returning)
475469
end
476-
rescue => failsafe
477-
warn "RedisCacheStore ignored exception in handle_exception: #{failsafe.class}: #{failsafe.message}\n #{failsafe.backtrace.join("\n ")}"
478470
end
479471
end
480472
end

activesupport/test/cache/stores/redis_cache_store_test.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -287,34 +287,34 @@ class FailureRaisingFromUnavailableClientTest < StoreTest
287287
include FailureRaisingBehavior
288288

289289
private
290-
def emulating_unavailability
291-
old_client = Redis.send(:remove_const, :Client)
292-
Redis.const_set(:Client, UnavailableRedisClient)
290+
def emulating_unavailability
291+
old_client = Redis.send(:remove_const, :Client)
292+
Redis.const_set(:Client, UnavailableRedisClient)
293293

294-
assert_raise Redis::BaseConnectionError do
295-
yield ActiveSupport::Cache::RedisCacheStore.new(namespace: @namespace, raise_errors: true)
294+
assert_raise Redis::BaseConnectionError do
295+
yield ActiveSupport::Cache::RedisCacheStore.new(namespace: @namespace, error_handler: -> (method:, returning:, exception:) { raise exception })
296+
end
297+
ensure
298+
Redis.send(:remove_const, :Client)
299+
Redis.const_set(:Client, old_client)
296300
end
297-
ensure
298-
Redis.send(:remove_const, :Client)
299-
Redis.const_set(:Client, old_client)
300-
end
301301
end
302302

303303
class FailureRaisingFromMaxClientsReachedErrorTest < StoreTest
304304
include FailureRaisingBehavior
305305

306306
private
307-
def emulating_unavailability
308-
old_client = Redis.send(:remove_const, :Client)
309-
Redis.const_set(:Client, MaxClientsReachedRedisClient)
307+
def emulating_unavailability
308+
old_client = Redis.send(:remove_const, :Client)
309+
Redis.const_set(:Client, MaxClientsReachedRedisClient)
310310

311-
assert_raise Redis::CommandError do
312-
yield ActiveSupport::Cache::RedisCacheStore.new(namespace: @namespace, raise_errors: true)
311+
assert_raise Redis::CommandError do
312+
yield ActiveSupport::Cache::RedisCacheStore.new(namespace: @namespace, error_handler: -> (method:, returning:, exception:) { raise exception })
313+
end
314+
ensure
315+
Redis.send(:remove_const, :Client)
316+
Redis.const_set(:Client, old_client)
313317
end
314-
ensure
315-
Redis.send(:remove_const, :Client)
316-
Redis.const_set(:Client, old_client)
317-
end
318318
end
319319

320320
class FailureSafetyFromUnavailableClientTest < StoreTest

guides/source/caching_with_rails.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,6 @@ config.cache_store = :redis_cache_store, { url: cache_servers,
528528
read_timeout: 0.2, # Defaults to 1 second
529529
write_timeout: 0.2, # Defaults to 1 second
530530
reconnect_attempts: 1, # Defaults to 0
531-
raise_errors: true, # Defaults to false. Raise Redis::BaseError instead of dropping them.
532531
error_handler: -> (method:, returning:, exception:) {
533532
# Report errors to Sentry as warnings
534533
Raven.capture_exception exception, level: 'warning',

0 commit comments

Comments
 (0)