Skip to content

Commit

Permalink
added cache expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
micheljansen committed Dec 29, 2009
1 parent 646e937 commit d214497
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion afcal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
alarm = params[:alarm].to_i if params[:alarm]

#fetch events
@all_events = Sinatra::Cache.cache("#{params[:postalcode]}/#{params[:housenumber]}/#{time}") do
@all_events = Sinatra::Cache.cache("#{params[:postalcode]}/#{params[:housenumber]}/#{time}", :expire => 60*60*24) do
TwenteMilieuData.new(params[:postalcode], params[:housenumber], time).all_events
end

Expand Down
7 changes: 4 additions & 3 deletions lib/cache.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
# source: http://railsillustrated.com/blazing-fast-sinatra-with-memcached.html
# based on: http://railsillustrated.com/blazing-fast-sinatra-with-memcached.html
require 'memcache'
require 'digest/md5'

module Sinatra
class Cache
def self.cache(key, &block)
def self.cache(key, options={}, &block)
unless CONFIG['memcached']
raise "Configure CONFIG['memcached'] to be a string like 'localhost:11211' "
end
begin
options[:expire] ||= 0
key = Digest::MD5.hexdigest(key)
@@connection ||= MemCache.new(CONFIG['memcached'], :namespace => 'Sinatra/')
result = @@connection.get(key)
return result if result
result = yield
@@connection.set(key, result)
@@connection.set(key, result, options[:expire])
result
rescue
yield
Expand Down

0 comments on commit d214497

Please sign in to comment.