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_tolerantObject

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

Raises:

  • If the cache backend encounters a failure

Parameters:

  • key [String] The cache key

Returns:

  • [Object, nil] The object if present, otherwise nil



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

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

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

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.

Raises:

  • If the cache backend encounters a failure

Parameters:

  • key [String] The cache key

  • expires_in [Integer, nil] The number of seconds until this cache entry expires. If nil, no expiration is set.

  • value [Object] The value to write to the cache

Returns:

  • [void]



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

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