Module: Rodbot::Db::Redis

Includes:
Memoize
Defined in:
lib/rodbot/db/redis.rb

Overview

Database adapter for Redis

All keys are implicitly nested inside the “rodbot:…” namespace to allow using one and the same Redis db for more than just Rodbot.

Examples:

Enable in config/rodbot.rb

db 'redis://localhost:6379/10'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Memoize

included

Class Method Details

.extendedObject



16
17
18
# File 'lib/rodbot/db/redis.rb', line 16

def self.extended(*)
  require 'redis'
end

Instance Method Details

#delete(*key) ⇒ Object



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

def delete(*key)
  get(*key).tap do
    db.del(skey(*key))
  end
end

#flushObject



45
46
47
48
# File 'lib/rodbot/db/redis.rb', line 45

def flush
  db.flushdb
  self
end

#get(*key) ⇒ Object



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

def get(*key)
  deserialize(db.get(skey(*key)))
end

#scan(*key) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/rodbot/db/redis.rb', line 36

def scan(*key)
  cursor, result = 0, []
  loop do
    cursor, keys = db.scan(cursor, match: skey(*key))
    result.append(*keys)
    break result if cursor == '0'
  end.map { _1[7..] }
end

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



20
21
22
23
24
# File 'lib/rodbot/db/redis.rb', line 20

def set(*key, expires_in: nil, &block)
  block.call((get(*key) unless block.arity.zero?)).tap do |value|
    db.set(skey(*key), serialize(value), ex: expires_in)
  end
end