Sha256: 4e5317c4d4dca32e743702233810f7c71a9a369dde661b3a85504f1e9498bbba

Contents?: true

Size: 693 Bytes

Versions: 2

Compression:

Stored size: 693 Bytes

Contents

require 'json'

class RedisQueue

  attr_accessor :key, :redis

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

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

  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

  def length
    @redis.llen(@key)
  end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gandalf-0.0.4 lib/redis_ext/redis_queue.rb
gandalf-0.0.3 lib/redis_ext/redis_queue.rb