class Switches::Backends::Redis

Constants

CHANNEL
PREFIX

Public Class Methods

new(uri, instance) click to toggle source
# File lib/switches/backends/redis.rb, line 9
def initialize(uri, instance)
  @uri = uri
  @instance = instance
end

Public Instance Methods

clear() click to toggle source
# File lib/switches/backends/redis.rb, line 32
def clear
  connection.flushdb
end
get(item) click to toggle source
# File lib/switches/backends/redis.rb, line 18
def get(item)
  if json = connection.get(item.key)
    JSONSerializer.deserialize(json)
  end
end
listen() click to toggle source
# File lib/switches/backends/redis.rb, line 24
def listen
  @thread ||= Thread.new { subscribe }
end
notify(update) click to toggle source
# File lib/switches/backends/redis.rb, line 28
def notify(update)
  connection.publish(CHANNEL, update.to_json)
end
set(item) click to toggle source
# File lib/switches/backends/redis.rb, line 14
def set(item)
  connection.set(item.key, item.to_json)
end
stop() click to toggle source
# File lib/switches/backends/redis.rb, line 36
def stop
  listen.kill
  connection.quit
  listener.quit
end

Private Instance Methods

connect() click to toggle source
# File lib/switches/backends/redis.rb, line 52
def connect
  ::Redis.new(url: @uri)
end
connection() click to toggle source
# File lib/switches/backends/redis.rb, line 48
def connection
  @connection ||= connect
end
listener() click to toggle source
# File lib/switches/backends/redis.rb, line 44
def listener
  @listener ||= connect
end
subscribe() click to toggle source
# File lib/switches/backends/redis.rb, line 56
def subscribe
  listener.subscribe(CHANNEL) do |on|
    on.message do |_, message|
      update = Update.load(message)
      @instance.notified(update)
    end
  end
end