Class: Faulty::Cache::FaultTolerantProxy
- Inherits:
-
Object
- Object
- Faulty::Cache::FaultTolerantProxy
- Defined in:
- lib/faulty/cache/fault_tolerant_proxy.rb
Overview
A wrapper for cache backends that may raise errors
Faulty#initialize automatically wraps all non-fault-tolerant cache backends with this class.
If the cache backend raises a StandardError
, it will be captured and
sent to the notifier. Reads errors will return nil
, and writes will be
a no-op.
Defined Under Namespace
Classes: Options
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.wrap(cache, **options, &block) ⇒ Cache::Interface
Wrap a cache in a FaultTolerantProxy unless it's already fault tolerant.
Instance Method Summary collapse
-
#fault_tolerant? ⇒ true
This cache makes any cache fault tolerant, so this is always
true
. -
#initialize(cache, **options) {|Options| ... } ⇒ FaultTolerantProxy
constructor
A new instance of FaultTolerantProxy.
-
#read(key) ⇒ Object?
Read from the cache safely.
-
#write(key, value, expires_in: nil) ⇒ void
Write to the cache safely.
Constructor Details
#initialize(cache, **options) {|Options| ... } ⇒ FaultTolerantProxy
Returns a new instance of FaultTolerantProxy.
33 34 35 36 |
# File 'lib/faulty/cache/fault_tolerant_proxy.rb', line 33 def initialize(cache, **, &block) @cache = cache @options = Options.new(, &block) end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/faulty/cache/fault_tolerant_proxy.rb', line 14 def @options end |
Class Method Details
.wrap(cache, **options, &block) ⇒ Cache::Interface
Wrap a cache in a FaultTolerantProxy unless it's already fault tolerant
42 43 44 45 46 |
# File 'lib/faulty/cache/fault_tolerant_proxy.rb', line 42 def self.wrap(cache, **, &block) return cache if cache.fault_tolerant? new(cache, **, &block) end |
Instance Method Details
#fault_tolerant? ⇒ true
This cache makes any cache fault tolerant, so this is always true
78 79 80 |
# File 'lib/faulty/cache/fault_tolerant_proxy.rb', line 78 def fault_tolerant? true end |
#read(key) ⇒ Object?
Read from the cache safely
If the backend raises a StandardError
, this will return nil
.
55 56 57 58 59 60 |
# File 'lib/faulty/cache/fault_tolerant_proxy.rb', line 55 def read(key) @cache.read(key) rescue StandardError => e .notifier.notify(:cache_failure, key: key, action: :read, error: e) nil end |
#write(key, value, expires_in: nil) ⇒ void
This method returns an undefined value.
Write to the cache safely
If the backend raises a StandardError
, the write will be ignored
68 69 70 71 72 73 |
# File 'lib/faulty/cache/fault_tolerant_proxy.rb', line 68 def write(key, value, expires_in: nil) @cache.write(key, value, expires_in: expires_in) rescue StandardError => e .notifier.notify(:cache_failure, key: key, action: :write, error: e) nil end |