Class: Avocado::Redis::Strategy
- Inherits:
-
Object
- Object
- Avocado::Redis::Strategy
- Defined in:
- lib/avocado/redis/strategy.rb
Instance Attribute Summary collapse
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
-
#initialize(url = ENV['REDIS_URL']) ⇒ Strategy
constructor
A new instance of Strategy.
- #purge_old(upload_id) ⇒ Object
- #read ⇒ Object
- #write(data, upload_id) ⇒ Object
Constructor Details
#initialize(url = ENV['REDIS_URL']) ⇒ Strategy
Returns a new instance of Strategy.
6 7 8 |
# File 'lib/avocado/redis/strategy.rb', line 6 def initialize(url = ENV['REDIS_URL']) @redis = ::Redis.new url: url end |
Instance Attribute Details
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
4 5 6 |
# File 'lib/avocado/redis/strategy.rb', line 4 def redis @redis end |
Instance Method Details
#purge_old(upload_id) ⇒ Object
21 22 23 24 |
# File 'lib/avocado/redis/strategy.rb', line 21 def purge_old(upload_id) old_keys = keys.reject { |key| key.end_with? upload_id } redis.del(*old_keys) if old_keys.any? end |
#read ⇒ Object
10 11 12 13 14 |
# File 'lib/avocado/redis/strategy.rb', line 10 def read redis.mget(*keys).map do |value| JSON.parse value end end |
#write(data, upload_id) ⇒ Object
16 17 18 19 |
# File 'lib/avocado/redis/strategy.rb', line 16 def write(data, upload_id) key = "avocado-#{Time.current.to_s(:nsec)}-#{upload_id}" redis[key] = data.to_json end |