Class: SafeMemoize::Stores::Base Abstract
- Inherits:
-
Object
- Object
- SafeMemoize::Stores::Base
- Defined in:
- lib/safe_memoize/stores/base.rb
Overview
Abstract base class for SafeMemoize cache store adapters.
Subclass this and implement all abstract methods to plug in a custom backend (Redis, Memcached, Rails.cache, etc.). The Memory class is the built-in reference implementation.
Direct Known Subclasses
Constant Summary collapse
- MISS =
Sentinel returned by #read to signal a cache miss.
Distinct from +nil+ and +false+, which are valid cached values.
Object.new.freeze
Instance Method Summary collapse
-
#clear ⇒ void
abstract
Remove all entries from the store.
-
#delete(key) ⇒ void
abstract
Delete a single entry.
-
#exist?(key) ⇒ Boolean
Check whether a live entry exists for the given key.
-
#keys ⇒ Array<Object>
abstract
Return all live (non-expired) keys.
-
#read(key) ⇒ Object
abstract
Read a value from the store.
-
#write(key, value, expires_in: nil) ⇒ void
abstract
Write a value to the store.
Instance Method Details
#clear ⇒ void
This method returns an undefined value.
Remove all entries from the store.
61 62 63 |
# File 'lib/safe_memoize/stores/base.rb', line 61 def clear raise NotImplementedError, "#{self.class}#clear must be implemented" end |
#delete(key) ⇒ void
This method returns an undefined value.
Delete a single entry.
53 54 55 |
# File 'lib/safe_memoize/stores/base.rb', line 53 def delete(key) raise NotImplementedError, "#{self.class}#delete must be implemented" end |
#exist?(key) ⇒ Boolean
Check whether a live entry exists for the given key.
The default delegates to #read; subclasses may override for stores with a native existence check.
80 81 82 |
# File 'lib/safe_memoize/stores/base.rb', line 80 def exist?(key) read(key) != MISS end |
#keys ⇒ Array<Object>
Return all live (non-expired) keys.
69 70 71 |
# File 'lib/safe_memoize/stores/base.rb', line 69 def keys raise NotImplementedError, "#{self.class}#keys must be implemented" end |
#read(key) ⇒ Object
Read a value from the store.
33 34 35 |
# File 'lib/safe_memoize/stores/base.rb', line 33 def read(key) raise NotImplementedError, "#{self.class}#read must be implemented" end |
#write(key, value, expires_in: nil) ⇒ void
This method returns an undefined value.
Write a value to the store.
44 45 46 |
# File 'lib/safe_memoize/stores/base.rb', line 44 def write(key, value, expires_in: nil) raise NotImplementedError, "#{self.class}#write must be implemented" end |