class Faulty::Cache::Mock

A mock cache for testing

This never clears expired values from memory, and should not be used in production applications. Instead, use a more robust implementation like `ActiveSupport::Cache::MemoryStore`.

Public Class Methods

new() click to toggle source
# File lib/faulty/cache/mock.rb, line 11
def initialize
  @cache = {}
  @expires = {}
end

Public Instance Methods

fault_tolerant?() click to toggle source

@return [true]

# File lib/faulty/cache/mock.rb, line 34
def fault_tolerant?
  true
end
read(key) click to toggle source

Read `key` from the cache

@return [Object, nil] The value if present and not expired

# File lib/faulty/cache/mock.rb, line 19
def read(key)
  return if @expires[key] && @expires[key] < Faulty.current_time

  @cache[key]
end
write(key, value, expires_in: nil) click to toggle source

Write `key` to the cache with an optional expiration

@return [void]

# File lib/faulty/cache/mock.rb, line 28
def write(key, value, expires_in: nil)
  @cache[key] = value
  @expires[key] = Faulty.current_time + expires_in unless expires_in.nil?
end