Class: Zm::Support::Cache::Store
- Inherits:
-
Object
- Object
- Zm::Support::Cache::Store
- Defined in:
- lib/zm/support/cache/store.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
writeonly
Sets the attribute logger.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #cleanup(_options = nil) ⇒ Object
- #clear(_options = nil) ⇒ Object
- #delete(name, options = nil) ⇒ Object
- #exist?(name, options = nil) ⇒ Boolean
- #fetch(name, options = nil) ⇒ Object
-
#initialize(options = {}) ⇒ Store
constructor
A new instance of Store.
- #read(name, options = nil) ⇒ Object
- #write(name, value, options = nil) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Store
Returns a new instance of Store.
10 11 12 13 14 15 |
# File 'lib/zm/support/cache/store.rb', line 10 def initialize( = {}) = @coder = Cache::Entry.factory @digest = OpenSSL::Digest.new('sha256') @logger = nil end |
Instance Attribute Details
#logger=(value) ⇒ Object (writeonly)
Sets the attribute logger
8 9 10 |
# File 'lib/zm/support/cache/store.rb', line 8 def logger=(value) @logger = value end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/zm/support/cache/store.rb', line 7 def end |
Instance Method Details
#cleanup(_options = nil) ⇒ Object
91 92 93 |
# File 'lib/zm/support/cache/store.rb', line 91 def cleanup( = nil) raise NotImplementedError end |
#clear(_options = nil) ⇒ Object
87 88 89 |
# File 'lib/zm/support/cache/store.rb', line 87 def clear( = nil) raise NotImplementedError end |
#delete(name, options = nil) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/zm/support/cache/store.rb', line 71 def delete(name, = nil) = () key = normalize_key(name, ) delete_entry(key, **) end |
#exist?(name, options = nil) ⇒ Boolean
78 79 80 81 82 83 84 85 |
# File 'lib/zm/support/cache/store.rb', line 78 def exist?(name, = nil) = () key = normalize_key(name, ) version = normalize_version() entry = read_entry(key, **) (entry && !entry.expired? && !entry.mismatched?(version)) || false end |
#fetch(name, options = nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/zm/support/cache/store.rb', line 17 def fetch(name, = nil, &) if block_given? = () key = normalize_key(name, ) entry = nil unless [:force] cached_entry = read_entry(key, **) entry = handle_expired_entry(cached_entry, key, ) entry = nil if entry&.mismatched?(normalize_version()) end if entry @logger&.info 'Load from cache' entry.value else save_block_result_to_cache(key, , &) end elsif && [:force] raise ArgumentError, 'Missing block: Calling `Cache#fetch` with `force: true` requires a block.' else read(name, ) end end |
#read(name, options = nil) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/zm/support/cache/store.rb', line 44 def read(name, = nil) = () key = normalize_key(name, ) version = normalize_version() entry = read_entry(key, **) return unless entry if entry.expired? || entry.mismatched?(version) delete_entry(key, **) nil else @logger&.info 'Load from cache' entry.value end end |
#write(name, value, options = nil) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/zm/support/cache/store.rb', line 62 def write(name, value, = nil) = () key = normalize_key(name, ) version = normalize_version() entry = Entry.new(value, **, version: version) write_entry(key, entry, **) end |