Class: Faulty::Cache::Default

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/faulty/cache/default.rb

Overview

The default cache implementation

It tries to make a logical decision of what cache implementation to use based on the current environment.

  • If Rails is loaded, it will use Rails.cache
  • If ActiveSupport is available, it will use an ActiveSupport::Cache::MemoryStore
  • Otherwise it will use a Null

Instance Method Summary collapse

Constructor Details

#initializeDefault

Returns a new instance of Default.



16
17
18
19
20
21
22
23
24
# File 'lib/faulty/cache/default.rb', line 16

def initialize
  @cache = if defined?(::Rails)
    Cache::Rails.new(::Rails.cache)
  elsif defined?(::ActiveSupport::Cache::MemoryStore)
    Cache::Rails.new(ActiveSupport::Cache::MemoryStore.new, fault_tolerant: true)
  else
    Cache::Null.new
  end
end

Instance Method Details

#fault_tolerantBoolean

Can this cache backend raise an error?

If the cache backend returns false from this method, it will be wrapped in a FaultTolerantProxy, otherwise it will be used as-is.

Returns:

  • (Boolean)

    True if this cache backend is fault tolerant



34
# File 'lib/faulty/cache/default.rb', line 34

def_delegators :@cache, :read, :write, :fault_tolerant?

#read(key) ⇒ Object?

Retrieve a value from the cache if available

Parameters:

  • key (String)

    The cache key

Returns:

  • (Object, nil)

    The object if present, otherwise nil

Raises:

  • If the cache backend encounters a failure



34
# File 'lib/faulty/cache/default.rb', line 34

def_delegators :@cache, :read, :write, :fault_tolerant?

#write(key, value, expires_in: expires_in) ⇒ void

This method returns an undefined value.

Write a value to the cache

This may be any object. It's up to the cache implementation to serialize if necessary or raise an error if unsupported.

Parameters:

  • key (String)

    The cache key

  • expires_in (Integer, nil) (defaults to: expires_in)

    The number of seconds until this cache entry expires. If nil, no expiration is set.

  • value (Object)

    The value to write to the cache

Raises:

  • If the cache backend encounters a failure



34
# File 'lib/faulty/cache/default.rb', line 34

def_delegators :@cache, :read, :write, :fault_tolerant?