Created
January 11, 2009 09:03
-
-
Save giom/45673 to your computer and use it in GitHub Desktop.
Quick braindump on how would one implement a mint store in rack:cache, I haven't tested this, so it probably doesn't work :-)
This file contains hidden or 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
# NB: I just wrote this to see how one would go about writing a mint store in rack cache and | |
# how hard it would be. I haven't tested it or even runned it so it most probably doesn't work (I'll | |
# play with it when I have more time) | |
module Rack::Cache | |
class MetaStore | |
# Stores request/response pairs in memcached. Keys are not stored | |
# directly since memcached has a 250-byte limit on key names. Instead, | |
# the SHA1 hexdigest of the key is used. | |
# Uses the mint store algorithm | |
class MintStore < Memcache | |
attr_reader :options | |
def initialize(server="localhost:11211", options={}) | |
@options = {:refresh_delay => 30} | |
@options.merge!(options.only(:refresh_delay)) | |
super(server, options) | |
end | |
def lookup(request, entity_store) | |
response = super | |
if response && response.freshness_information? && response.stale? | |
response.max_age += options[:refresh_delay] | |
self.store(request, response, entity_store) | |
nil | |
else | |
response | |
end | |
end | |
extend Rack::Utils | |
# Create Mintstorn store for the given URI. The URI must specify | |
# a host and may specify a port, namespace, and options: | |
# | |
# memcached://example.com:11211/namespace?opt1=val1&opt2=val2 | |
# | |
# Query parameter names and values are documented with the memcached | |
# library: http://tinyurl.com/4upqnd | |
def self.resolve(uri) | |
server = "#{uri.host}:#{uri.port || '11211'}" | |
options = parse_query(uri.query) | |
options.keys.each do |key| | |
value = | |
case value = options.delete(key) | |
when 'true' ; true | |
when 'false' ; false | |
else value.to_sym | |
end | |
options[k.to_sym] = value | |
end | |
options[:namespace] = uri.path.sub(/^\//, '') | |
new server, options | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment