Class: MudisQL::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/mudis-ql/store.rb

Overview

Store wraps mudis operations for a specific namespace

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace = nil) ⇒ Store

Returns a new instance of Store.



10
11
12
# File 'lib/mudis-ql/store.rb', line 10

def initialize(namespace = nil)
  @namespace = namespace
end

Instance Attribute Details

#namespaceObject (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

#allArray<Hash>

Retrieve all records from the namespace

Returns:

  • (Array<Hash>)

    array of records with their keys



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

Parameters:

  • key (String)

    the key to delete



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

#keysArray<String>

Retrieve all keys from the namespace

Returns:

  • (Array<String>)

    array of keys



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

Parameters:

  • key (String)

    the key to read

Returns:

  • (Object, nil)

    the cached value or nil



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

Parameters:

  • key (String)

    the key to write

  • value (Object)

    the value to store

  • expires_in (Integer, nil) (defaults to: nil)

    optional TTL in seconds



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