Skip to content

Commit

Permalink
Commiting the rest interface for TB's infinispan before adding multip…
Browse files Browse the repository at this point in the history
…le bins.
  • Loading branch information
bertrama committed Mar 2, 2014
1 parent 2ac27eb commit 239ea5a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 33 deletions.
4 changes: 2 additions & 2 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
require 'restmc'
#$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
require './lib/restmc'

run RESTmc::Application
83 changes: 52 additions & 31 deletions lib/restmc.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
require 'rubygems'
require 'sinatra/base'
require 'memcached'
require 'yaml'
require 'torquebox/cache'

module RESTmc

def self.config
begin
@config ||= YAML.load_file(File.join(File.dirname(__FILE__), '..', 'config.yml'))[RESTmc::Application.environment.to_s]
rescue
@config = { 'servers' => ['127.0.0.1:11211'] }
end
end
def reload_fn cache, key, value
# (val.to_i + value).to_s
val = cache.get(key)
cache.put(key, val.to_i + value)
cache.get(key).to_s
end

module RESTmc
class Application < Sinatra::Base
mime_type :text, 'text/plain'
set :reload_templates, false # we have no templates
Expand All @@ -21,57 +19,85 @@ class Application < Sinatra::Base
DEFAULT_TTL = 0 # never expire; use @mc.options[:default_ttl] for the client default of 1 week

def initialize
@mc = Memcached.new RESTmc.config['servers']
@cache = TorqueBox::Infinispan::Cache.new
end

before do
content_type :text
end

put '/' do
load 'lib/restmc.rb'
"reload"
end

get '/' do
@cache.keys.join("\n")
end

get '/*' do
begin
@mc.get splat_to_key(params[:splat]), should_marshal?
rescue Memcached::NotFound
@cache.get(splat_to_key(params[:splat])).to_s
rescue Exception => e
status 404
''
end
end

put '/+/*' do
begin
key = splat_to_key(params[:splat])
data = request.body.read.to_i
by = (data > 0 ? data : 1)
@mc.incr splat_to_key(params[:splat]), by
rescue Memcached::NotFound
@mc.set splat_to_key(params[:splat]), by, get_ttl, false
data = 1 if data < 1
@cache.increment(key, data).to_s
rescue TypeError
@cache.put(key, @cache.get(key).to_i + data).to_s
rescue Exception => e
@cache.put(key, data, get_ttl).to_s
end
end

put '/-/*' do
begin
key = splat_to_key(params[:splat])
data = request.body.read.to_i
by = (data > 0 ? data : 1)
@mc.decr splat_to_key(params[:splat]), by
rescue Memcached::NotFound
@mc.set splat_to_key(params[:splat]), 0, get_ttl, false
data = 1 if data < 1
@cache.decrement(key, data).to_s
rescue TypeError
@cache.put(key, @cache.get(key).to_i + data).to_s
rescue Exception => e
@cache.put(key, 1 - data, get_ttl).to_s
end
end

put '/*' do
@mc.set splat_to_key(params[:splat]), request.body.read, get_ttl, should_marshal?
@cache.put(splat_to_key(params[:splat]), request.body.read, get_ttl).to_s
end

post '/*' do
begin
@mc.add splat_to_key(params[:splat]), request.body.read, get_ttl, should_marshal?
rescue Memcached::NotStored
data = request.body.read
data = data.to_i if data.match(/^\d+$/)
@cache.put_if_absent(splat_to_key(params[:splat]), data, get_ttl).to_s
rescue Exception => e
status 409
''
end
end

delete '/' do
begin
@cache.clear
'Cache Cleared'
rescue Exception => e
status 400
end
end

delete '/*' do
begin
@mc.delete splat_to_key(params[:splat])
rescue Memcached::NotFound
@cache.remove(splat_to_key(params[:splat])).to_s
rescue Exception => e
status 404
end
end
Expand All @@ -92,10 +118,5 @@ def get_ttl
end
ttl
end

def should_marshal?
ENABLE_MARSHAL
end

end
end

0 comments on commit 239ea5a

Please sign in to comment.