Permalink
Browse files

initial support for memcached

  • Loading branch information...
1 parent 0dae2c4 commit 4f952c88d5680c3f57e519c20ff6d06792d3999b makuchaku committed Nov 21, 2011
Showing with 24 additions and 1 deletion.
  1. +2 −0 README
  2. +22 −1 bridge.rb
View
2 README
@@ -19,6 +19,8 @@ u => uri
m => HTTP method (GET, POST, PUT, DELETE, HEAD, etc) default GET
h => request headers hash in json, url encoded
b => request body
+c => if "true", server side caching of result will happen for #{cn} minutes
+cn => number of minutes (1, 2, 3, ...) for which server side caching will occur
Callback argument looks like this:
{ status: 200, headers: { header: hash }, body: 'body text' }
View
@@ -4,6 +4,17 @@
require 'net/http'
require 'net/https'
require 'json'
+require 'sinatra/memcache' # sinatra support => https://github.com/gioext/sinatra-memcache
+require "pp"
+
+
+# Memcached config
+set :cache_server, "localhost:11211"
+set :cache_namespace, "bridge-memcache"
+set :cache_enable, true
+set :cache_logging, true
+set :cache_default_expiry, 60*1 # seconds, default => 1 min
+set :cache_default_compress, true
get '/' do
@@ -37,7 +48,17 @@
http.use_ssl = true if url.scheme == "https"
response = http.start do |http|
- http.request(request)
+ if params['c'] == "true"
+ pp params
+ cache params['u'], :expiry => (60 * (params['cn']||1.to_i)) do
+ puts "========> cached for #{Time.now}"
+ http.request(request) # uncached request
+ end
+ else
+ puts "========> NOT caching #{Time.now}"
+ http.request(request) # uncached request
+ end
+
end
arg = "{ status: #{response.code}, headers: [#{response.each_name { }.to_json}], body: \"#{URI.escape(response.body)}\" }"

0 comments on commit 4f952c8

Please sign in to comment.