Class: Reliable::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/reliable/redis.rb

Defined Under Namespace

Classes: Pipeline

Instance Method Summary collapse

Constructor Details

#initializeRedis

Returns a new instance of Redis.



5
6
7
8
9
# 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

Instance Method Details

#brpoplpush(pop_key, push_key, timeout = POP_TIMEOUT) ⇒ Object



43
44
45
# 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) ⇒ Object



15
16
17
# File 'lib/reliable/redis.rb', line 15

def command(*args)
  @connection.call!(*args)
end

#get(key) ⇒ Object



27
28
29
# File 'lib/reliable/redis.rb', line 27

def get(key)
  scommand "GET", key
end

#incr(key) ⇒ Object



35
36
37
# File 'lib/reliable/redis.rb', line 35

def incr(key)
  scommand "INCR", key
end

#llen(key) ⇒ Object



39
40
41
# File 'lib/reliable/redis.rb', line 39

def llen(key)
  scommand "LLEN", key
end

#lpop(key) ⇒ Object



55
56
57
# File 'lib/reliable/redis.rb', line 55

def lpop(key)
  scommand "LPOP", key
end

#lpop_and_del(list_key, key) ⇒ Object



103
104
105
106
107
108
# 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) ⇒ Object



51
52
53
# File 'lib/reliable/redis.rb', line 51

def lpush(key, value)
  scommand "LPUSH", key, value
end

#multiObject



83
84
85
86
87
88
89
90
91
92
93
94
# 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

#pipelineObject



74
75
76
77
78
79
80
81
# 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) ⇒ Object



47
48
49
# File 'lib/reliable/redis.rb', line 47

def rpoplpush(pop_key, push_key)
  scommand "RPOPLPUSH", pop_key, push_key
end

#scan(pattern) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# 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) ⇒ Object



19
20
21
# File 'lib/reliable/redis.rb', line 19

def scommand(*args)
  synchronize { command(*args) }
end

#set(key, value) ⇒ Object



31
32
33
# File 'lib/reliable/redis.rb', line 31

def set(key, value)
  scommand "SET", key, value
end

#set_and_lpush(list_key, key, value) ⇒ Object



96
97
98
99
100
101
# 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

#synchronizeObject



11
12
13
# File 'lib/reliable/redis.rb', line 11

def synchronize
  @mutex.synchronize { yield }
end

#timeObject



23
24
25
# File 'lib/reliable/redis.rb', line 23

def time
  scommand("TIME").join(".")
end