Class: Faulty::Cache::Rails

Inherits:
Object
  • Object
show all
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



11
12
13
14
# File 'lib/faulty/cache/rails.rb', line 11

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)


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

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



17
18
19
# File 'lib/faulty/cache/rails.rb', line 17

def read(key)
  @cache.read(key)
end

#write(key, value, expires_in: nil) ⇒ 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



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

def write(key, value, expires_in: nil)
  @cache.write(key, value, expires_in: expires_in)
end