Created
May 9, 2011 08:11
-
-
Save sreeix/962221 to your computer and use it in GitHub Desktop.
Around wrapper to do simple memcached peruser
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
module CachingHelper | |
def self.included(claz) | |
claz.extend ClassMethods | |
end | |
module ClassMethods | |
def sf_cache(wrapped_method) | |
send(:around_filter, :cache_result) | |
end | |
end | |
def cache_result | |
key = "#{current_user.id}:#{request.url}" | |
if(page = Cache.get(key)) | |
Rails.logger.debug("CACHE HIT: for #{key}") | |
response.body = page | |
response.status = 200 | |
response.content_type = :xml | |
else | |
yield | |
if(request.get? && response.status == "200 OK") | |
Rails.logger.debug("CACHE MISS: setting into memcached #{response.body}" ) | |
Cache.set(key, response.body) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment