Class: Switches::Backends::Redis

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

Constant Summary collapse

PREFIX =
"switches"
CHANNEL =
[PREFIX, "bus"].join(":")

Instance Method Summary collapse

Constructor Details

#initialize(uri, instance) ⇒ Redis

Returns a new instance of Redis.



9
10
11
12
# File 'lib/switches/backends/redis.rb', line 9

def initialize(uri, instance)
  @uri = uri
  @instance = instance
end

Instance Method Details

#clearObject



32
33
34
# File 'lib/switches/backends/redis.rb', line 32

def clear
  connection.flushdb
end

#get(item) ⇒ Object



18
19
20
21
22
# File 'lib/switches/backends/redis.rb', line 18

def get(item)
  if json = connection.get(item.key)
    JSONSerializer.deserialize(json)
  end
end

#listenObject



24
25
26
# File 'lib/switches/backends/redis.rb', line 24

def listen
  @thread ||= Thread.new { subscribe }
end

#notify(update) ⇒ Object



28
29
30
# File 'lib/switches/backends/redis.rb', line 28

def notify(update)
  connection.publish(CHANNEL, update.to_json)
end

#set(item) ⇒ Object



14
15
16
# File 'lib/switches/backends/redis.rb', line 14

def set(item)
  connection.set(item.key, item.to_json)
end

#stopObject



36
37
38
39
40
# File 'lib/switches/backends/redis.rb', line 36

def stop
  listen.kill
  connection.quit
  listener.quit
end