Class: Faulty::Cache::Default

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



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

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_tolerant?Boolean

This cache is fault tolerant if the internal one is

Returns:

  • (Boolean)


43
44
45
# File 'lib/faulty/cache/default.rb', line 43

def fault_tolerant?
  @cache.fault_tolerant?
end

#read(key) ⇒ Object?

Read from the internal cache by key

Parameters:

  • key (String)

    The cache key

Returns:

  • (Object, nil)

    The object if present, otherwise nil



28
29
30
# File 'lib/faulty/cache/default.rb', line 28

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

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

Write to the internal cache

Parameters:

  • key (String)

    The cache key

Returns:

  • (Object, nil)

    The object if present, otherwise nil



36
37
38
# File 'lib/faulty/cache/default.rb', line 36

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