Project

memq

0.0
No commit activity in last 3 years
No release in over 3 years
Simple and lightweight memcached based queue solution
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0

Runtime

>= 0
 Project Readme

Memq

Memq is a lightweight simple queue solution, based on Memcached and Dalli. It's a port of this https://github.com/abhinavsingh/memq.git PHP MEMQ class, with some minor changes.

Installation

Add this line to your application's Gemfile:

gem 'memq'

And then execute:

$ bundle

Or install it yourself as:

$ gem install memq

Memq will attempt to install latest version of dalli gem automaticly.

Usage

require 'memq'

mem = Memq::Queue.new

Above code will initialize Queue object and attempt to create a default queue with a key "default".

If you wish, you can pass some dalli options, and a queue key name:

mem = Memq::Queue.new "mail", "localhost:11211", {:compress => true}

Enqueue something. The method will return queue position..

mem.enqueue "Hello world!" # => 0 
mem.enqueue "Aloha world!" # => 1
mem.total                  # => 2

Call mem.total to see queue length

Dequeue.

mem.dequeue                #  => {"mail_0"=>"Hello world!"} 

The keys are "queuename_position"

Enqueue some more

mem.enqueue [1,2,3,4,5]                            # => 2 
mem.enqueue({:hello => 'word', :aloha => 'world'}) # => 3
mem.total                                          # => 3

Dequeue all in a row

mem.dequeue mem.total
# => {"mail_1"=>"Aloha world!", "mail_2"=>[1, 2, 3, 4, 5], "mail_3"=>{:hello=>"word", :aloha=>"world"}}
mem.total    # => 0

Please keep in mind that dequeing currently doesn't actually delete your queued items. It only ajusts queue head and tail pointers. So, you can access an already dequeued item directly:

mem.client.get "mail_0"   # => "Hello world!"

mem.client is just a Dalli client instance, so it can do everything Dalli client can.

By default the queue is named "default". You can create and manipulate other queues by single object instance:

mem.use "process"  # => ["process", 0, 0]

will create "process" queue if it doesn't exist and switch using it. The method returns an array with queue name, head and tail pointer values. If called without arguments, it will return this array for current queue.

Also you can reset your queue, check if queue exists and if queue is empty:

mem.rst       # => true
mem.exists?   # => true
mem.empty?    # => true

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request