Class: SafeMemoize::Stores::RailsCache
- Defined in:
- lib/safe_memoize/stores/rails_cache.rb
Overview
Cache store adapter backed by any +ActiveSupport::Cache::Store+.
Not auto-required. Add to your Rails initializer: require "safe_memoize/stores/rails_cache"
Compatible with any +ActiveSupport::Cache::Store+ implementation (+MemoryStore+, +FileStore+, +MemCacheStore+, +RedisCacheStore+, etc.) and with +Rails.cache+ directly.
Because +ActiveSupport::Cache+ returns +nil+ for both a cache miss and a cached +nil+ value, this adapter wraps every value in a two-element sentinel envelope before writing. The envelope is transparent to callers.
TTL is forwarded as +expires_in:+ to the cache, so the underlying store manages expiry natively — there is no lazy-expiry overhead on read.
#clear uses +delete_matched+ scoped to the adapter's namespace, so it never clears entries belonging to other parts of the application. The backend must respond to +delete_matched+ (all standard Rails cache stores do); a +NotImplementedError+ is raised if it does not.
#keys returns an empty array — +ActiveSupport::Cache::Store+ does not expose a standard key enumeration API. Override the method if your backend supports it.
Constant Summary collapse
- VALUE_TAG =
Tag prepended to every stored value so cached +nil+/+false+ are distinguishable from a cache miss.
"safe_memoize:v1"
Constants inherited from Base
Instance Method Summary collapse
-
#clear ⇒ void
Removes all entries written by this adapter (scoped to the namespace).
- #delete(key) ⇒ void
- #exist?(key) ⇒ Boolean
-
#initialize(cache, namespace: "safe_memoize") ⇒ RailsCache
constructor
A new instance of RailsCache.
-
#keys ⇒ Array
Returns an empty array.
-
#read(key) ⇒ Object
The stored value, or Base::MISS if absent or unrecognised.
- #write(key, value, expires_in: nil) ⇒ void
Constructor Details
#initialize(cache, namespace: "safe_memoize") ⇒ RailsCache
Returns a new instance of RailsCache.
58 59 60 61 |
# File 'lib/safe_memoize/stores/rails_cache.rb', line 58 def initialize(cache, namespace: "safe_memoize") @cache = cache @namespace = namespace end |
Instance Method Details
#clear ⇒ void
This method returns an undefined value.
Removes all entries written by this adapter (scoped to the namespace).
Delegates to +delete_matched+ on the underlying store; raises +NotImplementedError+ if the store does not support it.
96 97 98 99 100 101 102 103 |
# File 'lib/safe_memoize/stores/rails_cache.rb', line 96 def clear unless @cache.respond_to?(:delete_matched) raise NotImplementedError, "#{@cache.class} does not support delete_matched — " \ "implement clear manually or use a store that supports it (e.g. MemoryStore, RedisCacheStore)" end @cache.delete_matched(/\A#{Regexp.escape(@namespace)}:/) end |
#delete(key) ⇒ void
This method returns an undefined value.
84 85 86 |
# File 'lib/safe_memoize/stores/rails_cache.rb', line 84 def delete(key) @cache.delete(cache_key(key)) end |
#exist?(key) ⇒ Boolean
117 118 119 |
# File 'lib/safe_memoize/stores/rails_cache.rb', line 117 def exist?(key) @cache.exist?(cache_key(key)) end |
#keys ⇒ Array
Returns an empty array.
+ActiveSupport::Cache::Store+ does not expose a key enumeration API. Override this method if your backend supports key listing.
111 112 113 |
# File 'lib/safe_memoize/stores/rails_cache.rb', line 111 def keys [] end |
#read(key) ⇒ Object
Returns the stored value, or Base::MISS if absent or unrecognised.
65 66 67 68 69 70 |
# File 'lib/safe_memoize/stores/rails_cache.rb', line 65 def read(key) raw = @cache.read(cache_key(key)) return MISS unless raw.is_a?(Array) && raw.length == 2 && raw[0] == VALUE_TAG raw[1] end |
#write(key, value, expires_in: nil) ⇒ void
This method returns an undefined value.
77 78 79 80 |
# File 'lib/safe_memoize/stores/rails_cache.rb', line 77 def write(key, value, expires_in: nil) opts = expires_in ? {expires_in: expires_in} : {} @cache.write(cache_key(key), [VALUE_TAG, value], **opts) end |