Class: Faulty::Cache::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/faulty/cache/mock.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initializeMock

Returns a new instance of Mock.



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

def initialize
  @cache = {}
  @expires = {}
end

Instance Method Details

#fault_tolerant?true

Returns:

  • (true)


34
35
36
# File 'lib/faulty/cache/mock.rb', line 34

def fault_tolerant?
  true
end

#read(key) ⇒ Object?

Read key from the cache

Returns:

  • (Object, nil)

    The value if present and not expired



19
20
21
22
23
# 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) ⇒ void

This method returns an undefined value.

Write key to the cache with an optional expiration



28
29
30
31
# 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