Permalink
Browse files

Refactoring page caching a bit, support cache-busting

  • Loading branch information...
1 parent 19fd148 commit a861780e8de854816f904b3f7824256e24f06282 @aceofspades committed Jul 30, 2012
Showing with 29 additions and 9 deletions.
  1. +29 −9 app/controllers/caloris/pages_controller.rb
@@ -20,9 +20,11 @@ def index
def show
authorize!(:show, @page)
::Caloris.logger.info "Caloris: show page \"#{@page.name}\""
+ load_page_cacheability
+ return if bust_cache?
using_view_paths do
respond_to do |format|
- format.html { render inline: maybe_cached_render(@page) }
+ format.html { render inline: maybe_cached_render }
# TODO require metadata to enable xml?
#format.xml { render @page.pathname }
end
@@ -152,25 +154,43 @@ def load_revision
@revision = @revisions.find{|r| r.name == @session[:ref] || @session[:branch]}
end
- def maybe_cached_render(page)
- block = proc do |page|
- render_to_string inline: page.render, layout: false
+ def load_page_cacheability
+ @cache_page = @page.cache_enable ? @page.site.cache_enable(self, @page) : false
+ @cache_page = false if request.query_string =~ /_cb=\d+/
+ end
+
+ def bust_cache?
+ return false unless @cache_page == :bust
+ # Bust the cache all the way to the browser
+ query = [request.query_string, "_cb=#{Time.now.to_i}"].map(&:presence).compact.join('&')
+ dest =
+ ["#{request.ssl? ? 'https' : 'http'}://#{request.host_with_port}#{request.path}", query].
+ map(&:presence).compact.join('?')
+ flash.keep
+ redirect_to dest
+ true
+ end
+
+ def maybe_cached_render
+ block = proc do
+ render_to_string inline: @page.render, layout: false
end
- if page.cache_enable && page.site.cache_enable(self, page)
+
+ if @cache_page
key = ActionCachePath.new(self, params).path
if params[:expire_cache]
::Caloris.logger.info "Caloris: expiring page \"#{@page.name}\" due to expire_cache parameter"
expires_now
Rails.cache.delete(key)
- block.call(page)
+ block.call(@page)
else
expires_in *@page.expires_in
- cache_params = {expires_in: page.cache_expiration, race_condition_ttl: page.cache_expiration + 5}
- Rails.cache.fetch(key, cache_params) { block.call(page) }
+ cache_params = {expires_in: @page.cache_expiration, race_condition_ttl: @page.cache_expiration + 5}
+ Rails.cache.fetch(key, cache_params) { block.call(@page) }
end
else
- block.call(page)
+ block.call(@page)
end
end

0 comments on commit a861780

Please sign in to comment.