Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'rubygems'
require 'sinatra'
require 'appengine-apis/memcache'
before do
@memcache = AppEngine::Memcache.new
end
get '/test/:key' do
@key = params[:key]
val = @memcache[@key]
now = Time.new.strftime("%H:%M")
if val
erb "hit! on #{now}<br>val: #{val}"
else
ttl = 60*3 #3minutes
@memcache.set(@key, now, ttl)
erb "miss! on #{now}<br>setting val: #{now}"
end
end
get '/reset/:key' do
@key = params[:key]
if @key == "all"
result = @memcache.flush_all
else
result = @memcache.delete(@key)
end
erb result.to_s
end
get '/stats' do
stats = @memcache.stats
erb "items: #{stats[:items]}<br>oldest_item_age: #{stats[:oldest_item_age]}"
end
__END__
@@layout
<%= yield %>
<p style="text-align:center;">
<% if @key && @key != "all" %>
<a href="/test/<%= @key %>">test <%= @key %> again</a><br>
<a href="/reset/<%= @key %>">delete <%= @key %></a><br>
<% end %>
<a href="/reset/all">reset all keys</a><br>
<a href="/stats">stats</a><br>
</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.