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.



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

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

Instance Attribute Details

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

#redisObject

Returns the value of attribute redis.



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

def redis
  @redis
end

Instance Method Details

#lengthObject



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

def length
  @redis.llen(@key)
end

#popObject



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

def pop
  begin
    value = JSON.parse(@redis.lpop(@key))
    new_hash = {}
    value.each{|k, v| new_hash[k.to_sym] = v}
    return new_hash 
  rescue Exception => ex
    puts ex
    return nil
  end
end

#pop_first(length) ⇒ Object



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

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

#push(value) ⇒ Object



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

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