Module: Rodbot::Db::Hash

Defined in:
lib/rodbot/db/hash.rb

Overview

Database adapter for Hash

This database is for development and testing only, it is not thread-safe and therefore should not be used in production.

Examples:

Enable in config/rodbot.rb

db 'hash'

Constant Summary collapse

PRUNE_THRESHOLD =
100

Instance Method Summary collapse

Instance Method Details

#delete(*key) ⇒ Object



31
32
33
34
# File 'lib/rodbot/db/hash.rb', line 31

def delete(*key)
  value, expires_at = db.delete(skey(*key))
  deserialize(value) if value && (!expires_at || epoch < expires_at)
end

#flushObject



41
42
43
44
# File 'lib/rodbot/db/hash.rb', line 41

def flush
  @db = {}
  self
end

#get(*key) ⇒ Object



26
27
28
29
# File 'lib/rodbot/db/hash.rb', line 26

def get(*key)
  value, expires_at = db[skey(*key)]
  deserialize(value) if value && (!expires_at || epoch < expires_at)
end

#scan(*key) ⇒ Object



36
37
38
39
# File 'lib/rodbot/db/hash.rb', line 36

def scan(*key)
  re = /\A#{skey(*key).sub(/\*\z/, '')}/
  db.keys.select { _1.match? re }
end

#set(*key, expires_in: nil, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/rodbot/db/hash.rb', line 16

def set(*key, expires_in: nil, &block)
  prune
  block.call((get(*key) unless block.arity.zero?)).tap do |value|
    db[key.join(':')] = [
      serialize(value),
      (epoch + expires_in if expires_in)
    ]
  end
end