Class: Switches::Backends::Redis
- Inherits:
-
Object
- Object
- Switches::Backends::Redis
- Defined in:
- lib/switches/backends/redis.rb
Constant Summary collapse
- PREFIX =
"switches"
- CHANNEL =
[PREFIX, "bus"].join(":")
Instance Method Summary collapse
- #clear ⇒ Object
- #get(item) ⇒ Object
-
#initialize(uri, instance) ⇒ Redis
constructor
A new instance of Redis.
- #listen ⇒ Object
- #notify(update) ⇒ Object
- #set(item) ⇒ Object
- #stop ⇒ Object
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
#clear ⇒ Object
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 |
#listen ⇒ Object
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 |
#stop ⇒ Object
36 37 38 39 40 |
# File 'lib/switches/backends/redis.rb', line 36 def stop listen.kill connection.quit listener.quit end |