Class: Faulty::Cache::AutoWire

Inherits:
Object
  • Object
show all
Defined in:
lib/faulty/cache/auto_wire.rb

Overview

Automatically configure a cache backend

Used by Faulty#initialize to setup sensible cache defaults

Defined Under Namespace

Classes: Options

Class Method Summary collapse

Class Method Details

.wrap(cache, **options) {|Options| ... } ⇒ Object

Wrap a cache backend with sensible defaults

If the cache is nil, create a new Default. Since Default may resolve to a non-fault-tolerant backend (e.g. Rails.cache), the result is passed back through wrap so it still receives the CircuitProxy and FaultTolerantProxy wrappers when needed.

If the backend is not fault tolerant, wrap it in CircuitProxy and FaultTolerantProxy.

Parameters:

  • cache (Interface)

    A cache backend

  • options (Hash)

    Attributes for Options

Yields:

  • (Options)

    For setting options in a block



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/faulty/cache/auto_wire.rb', line 43

def wrap(cache, **options, &)
  options = Options.new(options, &)
  if cache.nil?
    wrap(Cache::Default.new, **options.to_h)
  elsif cache.fault_tolerant?
    cache
  else
    Cache::FaultTolerantProxy.new(
      Cache::CircuitProxy.new(cache, circuit: options.circuit, notifier: options.notifier),
      notifier: options.notifier
    )
  end
end