Class: Qmore::Persistence::Redis
- Inherits:
-
Object
- Object
- Qmore::Persistence::Redis
- Defined in:
- lib/qmore/persistence.rb
Constant Summary collapse
- DYNAMIC_QUEUE_KEY =
"qmore:dynamic".freeze
- PRIORITY_KEY =
"qmore:priority".freeze
Instance Attribute Summary collapse
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
- #decode(data) ⇒ Object
- #encode(data) ⇒ Object
-
#initialize(redis) ⇒ Redis
constructor
A new instance of Redis.
-
#load ⇒ Qmore::Configuration
Returns a Qmore::Configuration from the underlying data storage mechanism.
- #read_dynamic_queues ⇒ Object
- #read_priority_buckets ⇒ Object
-
#write(configuration) ⇒ Object
Writes out the configuration to the underlying data storage mechanism.
- #write_dynamic_queues(dynamic_queues) ⇒ Object
- #write_priority_buckets(data) ⇒ Object
Constructor Details
#initialize(redis) ⇒ Redis
Returns a new instance of Redis.
46 47 48 |
# File 'lib/qmore/persistence.rb', line 46 def initialize(redis) @redis = redis end |
Instance Attribute Details
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
44 45 46 |
# File 'lib/qmore/persistence.rb', line 44 def redis @redis end |
Instance Method Details
#decode(data) ⇒ Object
50 51 52 |
# File 'lib/qmore/persistence.rb', line 50 def decode(data) MultiJson.load(data) if data end |
#encode(data) ⇒ Object
54 55 56 |
# File 'lib/qmore/persistence.rb', line 54 def encode(data) MultiJson.dump(data) end |
#load ⇒ Qmore::Configuration
Returns a Qmore::Configuration from the underlying data storage mechanism
60 61 62 63 64 65 |
# File 'lib/qmore/persistence.rb', line 60 def load configuration = Qmore::Configuration.new configuration.dynamic_queues = self.read_dynamic_queues configuration.priority_buckets = self.read_priority_buckets configuration end |
#read_dynamic_queues ⇒ Object
74 75 76 77 78 79 |
# File 'lib/qmore/persistence.rb', line 74 def read_dynamic_queues result = {} queues = redis.hgetall(DYNAMIC_QUEUE_KEY) queues.each {|k, v| result[k] = decode(v) } return result end |
#read_priority_buckets ⇒ Object
81 82 83 84 85 |
# File 'lib/qmore/persistence.rb', line 81 def read_priority_buckets priorities = Array(redis.lrange(PRIORITY_KEY, 0, -1)) priorities = priorities.collect {|p| decode(p) } return priorities end |
#write(configuration) ⇒ Object
Writes out the configuration to the underlying data storage mechanism. @param configuration to be persisted
69 70 71 72 |
# File 'lib/qmore/persistence.rb', line 69 def write(configuration) write_dynamic_queues(configuration.dynamic_queues) write_priority_buckets(configuration.priority_buckets) end |
#write_dynamic_queues(dynamic_queues) ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/qmore/persistence.rb', line 96 def write_dynamic_queues(dynamic_queues) redis.multi do redis.del(DYNAMIC_QUEUE_KEY) dynamic_queues.each do |k, v| redis.hset(DYNAMIC_QUEUE_KEY, k, encode(v)) end end end |
#write_priority_buckets(data) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/qmore/persistence.rb', line 87 def write_priority_buckets(data) redis.multi do redis.del(PRIORITY_KEY) Array(data).each do |v| redis.rpush(PRIORITY_KEY, encode(v)) end end end |