Class: Faulty::Cache::Mock
- Inherits:
-
Object
- Object
- Faulty::Cache::Mock
- 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
- #fault_tolerant? ⇒ true
-
#initialize ⇒ Mock
constructor
A new instance of Mock.
-
#read(key) ⇒ Object?
Read
key
from the cache. -
#write(key, value, expires_in: nil) ⇒ void
Write
key
to the cache with an optional expiration.
Constructor Details
#initialize ⇒ Mock
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
34 35 36 |
# File 'lib/faulty/cache/mock.rb', line 34 def fault_tolerant? true end |
#read(key) ⇒ Object?
Read key
from the cache
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 |