Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myronmarston/d641725a48e0bf0dd5a8 to your computer and use it in GitHub Desktop.
Save myronmarston/d641725a48e0bf0dd5a8 to your computer and use it in GitHub Desktop.
From 25c336594e2b215429aa2d75e88d35632377ad81 Mon Sep 17 00:00:00 2001
From: Myron Marston <myron.marston@gmail.com>
Date: Thu, 3 Dec 2009 13:21:49 -0800
Subject: [PATCH] Override the caching of pages that have comments, so we don't accidentally serve stale pages to users.
---
config/environment.rb | 9 +++++++++
lib/site_controller_etag_extension.rb | 18 ++++++++++++++++++
2 files changed, 27 insertions(+), 0 deletions(-)
create mode 100644 lib/site_controller_etag_extension.rb
diff --git a/config/environment.rb b/config/environment.rb
index 2b5cb16..d706e09 100644
--- a/config/environment.rb
+++ b/config/environment.rb
@@ -17,6 +17,8 @@ Radiant::Initializer.run do |config|
# can be used as a placeholder for all extensions not explicitly named.
# config.extensions = [ :all ]
+ config.load_paths << File.join(Rails.root, 'lib')
+
# Your secret key for verifying cookie session data integrity.
# If you change this key, all old sessions will become invalid!
# Make sure the secret is at least 30 characters and all random,
@@ -76,6 +78,13 @@ Radiant::Initializer.run do |config|
end
end
+ # This is a bit of a hack, but I couldn't find any other way to make sure this module gets included
+ # once, and only once in dev mode. Radiant seems to have different auto-loading / class-caching behavior
+ # compared to a standard Rails app.
+ config.to_prepare do
+ SiteController.send(:include, SiteControllerEtagExtension) unless SiteController.included_modules.include?(SiteControllerEtagExtension)
+ end
+
config.gem 'will_paginate'
config.gem 'fastercsv'
config.gem 'sanitize'
diff --git a/lib/site_controller_etag_extension.rb b/lib/site_controller_etag_extension.rb
new file mode 100644
index 0000000..5604630
--- /dev/null
+++ b/lib/site_controller_etag_extension.rb
@@ -0,0 +1,18 @@
+module SiteControllerEtagExtension
+ def self.included(base)
+ base.alias_method_chain :set_cache_control, :etag
+ end
+
+ private
+
+ def set_cache_control_with_etag
+ if (request.head? || request.get?) && @page.cache? && @page.enable_comments?
+ expires_in nil, :private => true, "no-cache" => true
+
+ # Note: when a comment is added, :comment_count and :lock_version are updated. :updated_at is not.
+ headers['ETag'] = Digest::MD5.hexdigest("#{@page.updated_at.to_s} : #{@page.lock_version.to_s}")
+ else
+ set_cache_control_without_etag
+ end
+ end
+end
\ No newline at end of file
--
1.6.5+GitX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment