Class: Elephas::Backends::Hash
- Defined in:
- lib/elephas/backends/hash.rb
Overview
This is a simple backend, which uses an hash for storing the values.
Instance Attribute Summary collapse
-
#data ⇒ HashWithIndifferentAccess
The internal hash used by the backend.
Instance Method Summary collapse
-
#delete(key) ⇒ Boolean
Deletes a value from the cache.
-
#exists?(key) ⇒ Boolean
Checks if a key exists in the cache.
-
#initialize(data = nil) ⇒ Hash
constructor
Initialize the backend.
-
#read(key) ⇒ Entry|NilClass
Reads a value from the cache.
-
#write(key, value, options = {}) ⇒ Object
Writes a value to the cache.
Methods inherited from Base
Constructor Details
#initialize(data = nil) ⇒ Hash
Initialize the backend.
19 20 21 |
# File 'lib/elephas/backends/hash.rb', line 19 def initialize(data = nil) @data = data.ensure_hash({}).with_indifferent_access end |
Instance Attribute Details
#data ⇒ HashWithIndifferentAccess
Returns The internal hash used by the backend.
13 14 15 16 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/elephas/backends/hash.rb', line 13 class Hash < Base attr_accessor :data # Initialize the backend. # # @param data [Hash] The initial data stored. def initialize(data = nil) @data = data.ensure_hash({}).with_indifferent_access end # Reads a value from the cache. # # @param key [String] The key to lookup. # @return [Entry|NilClass] The read value or `nil`. def read(key) exists?(key) ? @data[key] : nil end # Writes a value to the cache. # # @param key [String] The key to associate the value with. # @param value [Object] The value to write. Setting a value to `nil` **doesn't** mean *deleting* the value. # @param options [Hash] A list of options for writing. # @see Elephas::Cache.setup_options # @return [Object] The value itself. def write(key, value, = {}) entry = ::Elephas::Entry.ensure(value, key, ) entry.refresh @data[key] = entry entry end # Deletes a value from the cache. # # @param key [String] The key to delete. # @return [Boolean] `true` if the key was in the cache, `false` otherwise. def delete(key) rv = @data.has_key?(key) @data.delete(key) rv end # Checks if a key exists in the cache. # # @param key [String] The key to lookup. # @return [Boolean] `true` if the key is in the cache, `false` otherwise. def exists?(key) @data.has_key?(key) && @data[key].valid?(self) end end |
Instance Method Details
#delete(key) ⇒ Boolean
Deletes a value from the cache.
49 50 51 52 53 |
# File 'lib/elephas/backends/hash.rb', line 49 def delete(key) rv = @data.has_key?(key) @data.delete(key) rv end |
#exists?(key) ⇒ Boolean
Checks if a key exists in the cache.
59 60 61 |
# File 'lib/elephas/backends/hash.rb', line 59 def exists?(key) @data.has_key?(key) && @data[key].valid?(self) end |
#read(key) ⇒ Entry|NilClass
Reads a value from the cache.
27 28 29 |
# File 'lib/elephas/backends/hash.rb', line 27 def read(key) exists?(key) ? @data[key] : nil end |