Class: Roundtrip::Store::Redis
- Inherits:
-
Object
- Object
- Roundtrip::Store::Redis
- Defined in:
- lib/roundtrip/store/redis.rb
Instance Method Summary collapse
- #add(trip) ⇒ Object
- #add_checkpoint(trip, at) ⇒ Object
- #get(id) ⇒ Object
-
#initialize(opts = {:redis => {:host => 'localhost'}}) ⇒ Redis
constructor
A new instance of Redis.
- #pending_trips(prod, older_than = 0) ⇒ Object
- #prune_old_trips(prod, timespan) ⇒ Object
- #remove(trip) ⇒ Object
Constructor Details
Instance Method Details
#add(trip) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/roundtrip/store/redis.rb', line 19 def add(trip) @conn.multi do @conn.set(trip_key(trip.id), Marshal.dump(trip)) @conn.zadd(route_key(trip.route), trip.started_at.to_i, trip.id) end end |
#add_checkpoint(trip, at) ⇒ Object
26 27 28 29 30 |
# File 'lib/roundtrip/store/redis.rb', line 26 def add_checkpoint(trip, at) time = Time.now @conn.zadd(checkpoints_key(trip.id), time.to_f, at) [at, time] end |
#get(id) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/roundtrip/store/redis.rb', line 10 def get(id) blob = @conn.get(trip_key(id)) trip = Marshal.load(blob) if blob if trip trip.checkpoints = @conn.zrange(checkpoints_key(id), "0", "-1", :withscores => true).map{|pair| [pair[0], Time.at(pair[1])]} end trip end |
#pending_trips(prod, older_than = 0) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/roundtrip/store/redis.rb', line 41 def pending_trips(prod, older_than=0) start_stamp = Time.now.to_i - older_than ids = @conn.zrangebyscore(route_key(prod), 0, start_stamp.to_s) return [] if ids.empty? tripsdata = @conn.mget(ids.map{|k| trip_key(k) }) tripsdata.map { |blob| Marshal.load(blob) } end |
#prune_old_trips(prod, timespan) ⇒ Object
50 51 52 |
# File 'lib/roundtrip/store/redis.rb', line 50 def prune_old_trips(prod, timespan) end |
#remove(trip) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/roundtrip/store/redis.rb', line 32 def remove(trip) res = @conn.multi do @conn.del(trip_key(trip.id)) @conn.del(checkpoints_key(trip.id)) @conn.zrem(route_key(trip.route), trip.id) end res.count == 3 && res[0] == 1 && res[2] == true end |