Class: RedisQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_ext/redis_queue.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RedisQueue

Returns a new instance of RedisQueue.



7
8
9
10
# File 'lib/redis_ext/redis_queue.rb', line 7

def initialize(options = {})
  @key = options[:key]
  @redis = (options[:redis] || Redis.new(options))
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



5
6
7
# File 'lib/redis_ext/redis_queue.rb', line 5

def key
  @key
end

#redisObject

Returns the value of attribute redis.



5
6
7
# File 'lib/redis_ext/redis_queue.rb', line 5

def redis
  @redis
end

Instance Method Details

#lengthObject



28
29
30
# File 'lib/redis_ext/redis_queue.rb', line 28

def length
  @redis.llen(@key)
end

#popObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/redis_ext/redis_queue.rb', line 16

def pop
  begin
    value = JSON.parse(@redis.lpop(@key))
    new_hash = {}
    value.each{|k, v| new_hash[k.to_sym] = v}
    return new_hash 
  # Returns nil on any kind of exception
  rescue Exception
    return nil
  end
end

#pop_first(count) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/redis_ext/redis_queue.rb', line 32

def pop_first(count)
  list = []
  count.times do
    element = self.pop
    break unless element
    list << element
  end
  list
end

#push(value) ⇒ Object



12
13
14
# File 'lib/redis_ext/redis_queue.rb', line 12

def push(value)
  @redis.rpush(@key, value.to_json)
end