Class: Roundtrip::Store::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/roundtrip/store/redis.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {:redis => {:host => 'localhost'}}) ⇒ Redis

Returns a new instance of Redis.



6
7
8
# File 'lib/roundtrip/store/redis.rb', line 6

def initialize(opts={:redis => {:host => 'localhost'}})
  @conn = opts[:redis][:connection]  || Redis.new(opts[:redis])
end

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