Created
August 13, 2010 15:44
-
-
Save gleicon/523086 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sinatra minimalist RestMQ | |
# no COMET, just /q/ routes and queue logic | |
# the core of RestMQ is how it uses Redis' data types | |
require 'rubygems' | |
require 'sinatra' | |
require 'redis' | |
QUEUESET = 'QUEUESET' # queue index | |
UUID_SUFFIX = ':UUID' # queue unique id | |
QUEUE_SUFFIX = ':queue' # suffix to identify each queue's LIST | |
reds = Redis.new | |
get '/q/*' do | |
queue = params['splat'].to_s | |
throw :halt, [404, 'Not found\n'] if queue == nil | |
queue = queue + QUEUE_SUFFIX | |
b = reds.rpop queue | |
throw :halt, [404, 'Not found (empty queue)\n'] if b == nil | |
v = reds.get b | |
"{'value':" + v + "'key':" + b + "}" | |
end | |
post '/q/*' do | |
queue = params['splat'].to_s | |
value = params['value'].to_s | |
throw :halt, [404, "Not found"] if queue == nil | |
q1 = queue + QUEUE_SUFFIX | |
uuid = reds.incr queue + UUID_SUFFIX | |
reds.sadd QUEUESET, q1 | |
lkey = queue + ':' + uuid.to_s | |
reds.set lkey, value | |
reds.lpush q1, lkey | |
'{ok, '+lkey+'}' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is a bit of duplication in your code. Check out the fix here at http://gist.github.com/616837