Class: MudisQL::Store
- Inherits:
-
Object
- Object
- MudisQL::Store
- Defined in:
- lib/mudis-ql/store.rb
Overview
Store wraps mudis operations for a specific namespace
Instance Attribute Summary collapse
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
Instance Method Summary collapse
-
#all ⇒ Array<Hash>
Retrieve all records from the namespace.
-
#delete(key) ⇒ Object
Delete a key from the cache.
-
#initialize(namespace = nil) ⇒ Store
constructor
A new instance of Store.
-
#keys ⇒ Array<String>
Retrieve all keys from the namespace.
-
#read(key) ⇒ Object?
Read a value from the cache.
-
#write(key, value, expires_in: nil) ⇒ Object
Write a value to the cache.
Constructor Details
#initialize(namespace = nil) ⇒ Store
10 11 12 |
# File 'lib/mudis-ql/store.rb', line 10 def initialize(namespace = nil) @namespace = namespace end |
Instance Attribute Details
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
8 9 10 |
# File 'lib/mudis-ql/store.rb', line 8 def namespace @namespace end |
Instance Method Details
#all ⇒ Array<Hash>
Retrieve all records from the namespace
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mudis-ql/store.rb', line 66 def all keys.map do |key| value = read(key) next unless value if value.is_a?(Hash) value.merge("_key" => key) else { "_key" => key, "value" => value } end end.compact end |
#delete(key) ⇒ Object
Delete a key from the cache
55 56 57 58 59 60 61 |
# File 'lib/mudis-ql/store.rb', line 55 def delete(key) if namespace Mudis.delete(key, namespace: namespace) else Mudis.delete(key) end end |
#keys ⇒ Array<String>
Retrieve all keys from the namespace
17 18 19 20 21 22 23 24 25 |
# File 'lib/mudis-ql/store.rb', line 17 def keys if namespace Mudis.keys(namespace: namespace) || [] else # When no namespace, mudis uses default internal namespace # We need to call without the namespace parameter [] # mudis doesn't support listing keys without namespace end end |
#read(key) ⇒ Object?
Read a value from the cache
31 32 33 34 35 36 37 |
# File 'lib/mudis-ql/store.rb', line 31 def read(key) if namespace Mudis.read(key, namespace: namespace) else Mudis.read(key) end end |
#write(key, value, expires_in: nil) ⇒ Object
Write a value to the cache
44 45 46 47 48 49 50 |
# File 'lib/mudis-ql/store.rb', line 44 def write(key, value, expires_in: nil) if namespace Mudis.write(key, value, expires_in: expires_in, namespace: namespace) else Mudis.write(key, value, expires_in: expires_in) end end |