Class: EmailDomainChecker::Cache::BaseAdapter
- Inherits:
-
Object
- Object
- EmailDomainChecker::Cache::BaseAdapter
- Defined in:
- lib/email_domain_checker/cache/base_adapter.rb
Overview
Base class for cache adapters All cache adapters must implement these methods
Direct Known Subclasses
Instance Method Summary collapse
-
#clear ⇒ void
Clear all cache entries.
-
#delete(key) ⇒ void
Delete a key from cache.
-
#exists?(key) ⇒ Boolean
Check if a key exists in cache.
-
#get(key) ⇒ Object?
Get cached value for a key.
-
#set(key, value, ttl: nil) ⇒ void
Set a value in cache.
-
#with(key, ttl: nil, force: false) { ... } ⇒ Object
Fetch value from cache or execute block and cache the result Similar to Rails.cache.fetch.
Instance Method Details
#clear ⇒ void
This method returns an undefined value.
Clear all cache entries
33 34 35 |
# File 'lib/email_domain_checker/cache/base_adapter.rb', line 33 def clear raise NotImplementedError, "Subclasses must implement #clear" end |
#delete(key) ⇒ void
This method returns an undefined value.
Delete a key from cache
27 28 29 |
# File 'lib/email_domain_checker/cache/base_adapter.rb', line 27 def delete(key) raise NotImplementedError, "Subclasses must implement #delete" end |
#exists?(key) ⇒ Boolean
Check if a key exists in cache
40 41 42 |
# File 'lib/email_domain_checker/cache/base_adapter.rb', line 40 def exists?(key) get(key) != nil end |
#get(key) ⇒ Object?
Get cached value for a key
11 12 13 |
# File 'lib/email_domain_checker/cache/base_adapter.rb', line 11 def get(key) raise NotImplementedError, "Subclasses must implement #get" end |
#set(key, value, ttl: nil) ⇒ void
This method returns an undefined value.
Set a value in cache
20 21 22 |
# File 'lib/email_domain_checker/cache/base_adapter.rb', line 20 def set(key, value, ttl: nil) raise NotImplementedError, "Subclasses must implement #set" end |
#with(key, ttl: nil, force: false) { ... } ⇒ Object
Fetch value from cache or execute block and cache the result Similar to Rails.cache.fetch
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/email_domain_checker/cache/base_adapter.rb', line 51 def with(key, ttl: nil, force: false, &block) raise ArgumentError, "Block is required" unless block_given? # Return cached value if not forcing and cache exists unless force return get(key) if exists?(key) end # Execute block and cache the result # Rails.cache.fetch also caches nil values, so we do the same value = yield set(key, value, ttl: ttl) value end |