class Reliable::Redis

Public Class Methods

new() click to toggle source
# File lib/reliable/redis.rb, line 5
def initialize
  url = ENV.fetch("REDIS_URI") { ENV.fetch("REDIS_URL") }
  @connection = Redic.new(url)
  @mutex = Mutex.new
end

Public Instance Methods

brpoplpush(pop_key, push_key, timeout = POP_TIMEOUT) click to toggle source
# File lib/reliable/redis.rb, line 43
def brpoplpush(pop_key, push_key, timeout = POP_TIMEOUT)
  scommand "BRPOPLPUSH", pop_key, push_key, timeout
end
command(*args) click to toggle source
# File lib/reliable/redis.rb, line 15
def command(*args)
  @connection.call!(*args)
end
get(key) click to toggle source
# File lib/reliable/redis.rb, line 27
def get(key)
  scommand "GET", key
end
incr(key) click to toggle source
# File lib/reliable/redis.rb, line 35
def incr(key)
  scommand "INCR", key
end
llen(key) click to toggle source
# File lib/reliable/redis.rb, line 39
def llen(key)
  scommand "LLEN", key
end
lpop(key) click to toggle source
# File lib/reliable/redis.rb, line 55
def lpop(key)
  scommand "LPOP", key
end
lpop_and_del(list_key, key) click to toggle source
# File lib/reliable/redis.rb, line 103
def lpop_and_del(list_key, key)
  multi do
    command "LPOP", list_key
    command "DEL", key
  end
end
lpush(key, value) click to toggle source
# File lib/reliable/redis.rb, line 51
def lpush(key, value)
  scommand "LPUSH", key, value
end
multi() { || ... } click to toggle source
# File lib/reliable/redis.rb, line 83
def multi
  synchronize do
    begin
      command "MULTI"
      yield
      command "EXEC"
    rescue StandardError => e
      command "DISCARD"
      raise
    end
  end
end
pipeline() { |pipe| ... } click to toggle source
# File lib/reliable/redis.rb, line 74
def pipeline
  synchronize do
    @connection.reset
    pipe = Pipeline.new(@connection)
    yield(pipe)
    @connection.commit
  end
end
rpoplpush(pop_key, push_key) click to toggle source
# File lib/reliable/redis.rb, line 47
def rpoplpush(pop_key, push_key)
  scommand "RPOPLPUSH", pop_key, push_key
end
scan(pattern) click to toggle source
# File lib/reliable/redis.rb, line 110
def scan(pattern)
  keys = []
  cursor = "0"
  loop do
    cursor, list = scommand "SCAN", cursor, "MATCH", pattern
    keys << list
    break if cursor == "0"
  end
  keys.flatten.compact.uniq
end
scommand(*args) click to toggle source
# File lib/reliable/redis.rb, line 19
def scommand(*args)
  synchronize { command(*args) }
end
set(key, value) click to toggle source
# File lib/reliable/redis.rb, line 31
def set(key, value)
  scommand "SET", key, value
end
set_and_lpush(list_key, key, value) click to toggle source
# File lib/reliable/redis.rb, line 96
def set_and_lpush(list_key, key, value)
  multi do
    command "SET", key, value
    command "LPUSH", list_key, key
  end
end
synchronize() { || ... } click to toggle source
# File lib/reliable/redis.rb, line 11
def synchronize
  @mutex.synchronize { yield }
end
time() click to toggle source
# File lib/reliable/redis.rb, line 23
def time
  scommand("TIME").join(".")
end