Skip to content

Instantly share code, notes, and snippets.

@alexch
Created November 19, 2009 16:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexch/238847 to your computer and use it in GitHub Desktop.
Save alexch/238847 to your computer and use it in GitHub Desktop.
# a Rack middleware component that enables ActiveRecord query caching
# To use, put "use QueryCaching" in your Sinatra app.
class QueryCaching
def initialize(app)
@app = app
end
def call(env)
if is_static_file?(env)
@app.call(env)
else
response = nil
ActiveRecord::Base.cache do
response = @app.call(env)
end
response
end
end
def is_static_file?(env)
# if the path end with a dot-extension (e.g. 'foo.jpg') then we assume
# it's a static file and don't enable the query cache. (This will only
# work for some application URL schemes, naturally.)
env['PATH_INFO'] =~ /\/[^\/]*\.[^\/\.]+$/
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment