Created
November 19, 2009 16:11
-
-
Save alexch/238847 to your computer and use it in GitHub Desktop.
This file contains 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
# 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