Skip to content

Instantly share code, notes, and snippets.

@humanzz
Created July 18, 2010 10:40
Show Gist options
  • Save humanzz/480306 to your computer and use it in GitHub Desktop.
Save humanzz/480306 to your computer and use it in GitHub Desktop.
Modified version of caches_page to allow specifiying an :expire_after such that the page will be expired via a background job
class ActionController::Base
# Modified version of caches_page to allow specifiying an :expire_after
# such that the page will be expired via a background job
# Example:
# class PostsController < ApplicationController
# time_caches_page :index, :expire_after => 5.minutes
# .
# .
# end
def self.time_caches_page(*actions)
return unless perform_caching
options = actions.extract_options!
expire_after = options.delete(:expire_after)
raise ArgumentError, "no :expire_after provided" if expire_after.blank?
after_filter({:only => actions}.merge(options)) do |c|
c.cache_page
# Relies on the presence of DelayedJob gem presence (http://rubygems.org/gems/delayed_job)
actions.each {|a| send_at(Time.now.utc + expire_after, :expire_page, c.request.path)}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment