Class: Faulty::Cache::Rails

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

Overview

A wrapper for a Rails or ActiveSupport cache

Instance Method Summary collapse

Constructor Details

#initialize(cache = ::Rails.cache, fault_tolerant: false) ⇒ Rails

Returns a new instance of Rails.

Parameters:

  • cache (defaults to: ::Rails.cache)

    The Rails cache to wrap

  • fault_tolerant (Boolean) (defaults to: false)

    Whether the Rails cache is fault_tolerant. See #fault_tolerant? for more details


13
14
15
16
# File 'lib/faulty/cache/rails.rb', line 13

def initialize(cache = ::Rails.cache, fault_tolerant: false)
  @cache = cache
  @fault_tolerant = fault_tolerant
end

Instance Method Details

#fault_tolerant?Boolean

Although ActiveSupport cache implementations are fault-tolerant, Rails.cache is not guranteed to be fault tolerant. For this reason, we require the user of this class to explicitly mark this cache as fault-tolerant using the #initialize parameter.

Returns:

  • (Boolean)

31
32
33
# File 'lib/faulty/cache/rails.rb', line 31

def fault_tolerant?
  @fault_tolerant
end

#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


23
# File 'lib/faulty/cache/rails.rb', line 23

def_delegators :@cache, :read, :write

#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


23
# File 'lib/faulty/cache/rails.rb', line 23

def_delegators :@cache, :read, :write