-
-
Save tedb/339365 to your computer and use it in GitHub Desktop.
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
# Forked from user dirk; changed to drop the should_render boolean arg and | |
# replace it with a separate method, cache_and_render | |
class ApplicationController < ActionController::Base | |
# Other random stuff. | |
protected | |
def cache_and_render(key, opts = {}) | |
cached = cache(key, opts) | |
render :text => cached | |
return cached | |
end | |
def cache(key, opts = {}) | |
cache = read_fragment(key, opts) | |
if not cache | |
cache = yield | |
write_fragment(key, cache, opts) | |
end | |
return cache | |
end | |
end | |
class Controller < ApplicationController | |
def action | |
# Filtering or other random stuff. | |
cache('key', :expires_in => 5.minutes) do | |
"This will be cached and rendered." | |
end | |
# This will be cached as well, but stored in cached_data instead of being rendered. | |
cached_data = cache('other_key', {}, false) { User.first.email } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment