From ad049bcc4b8f6a30f244a0410e6cdd28eab003c5 Mon Sep 17 00:00:00 2001 From: Jacques Distler Date: Thu, 18 Dec 2008 09:21:26 -0600 Subject: [PATCH] Drop hostname from cache key By default, Rails will cache example.com/mywiki/show/SomePage and www.example.com/mywiki/show/SomePage In Instiki, this just leads to stale cached pages and frustration. Fix that behaviour. --- config/environment.rb | 1 + lib/caching_stuff.rb | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 lib/caching_stuff.rb diff --git a/config/environment.rb b/config/environment.rb index 05bd8afc..4a3c41e0 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -58,3 +58,4 @@ end require_dependency 'instiki_errors' #require 'jcode' +require 'caching_stuff' diff --git a/lib/caching_stuff.rb b/lib/caching_stuff.rb new file mode 100644 index 00000000..54634a47 --- /dev/null +++ b/lib/caching_stuff.rb @@ -0,0 +1,20 @@ +# based on http://pastie.org/49840 +# override the fragment_cache_key method in actionpack/lib/action_controller/caching/fragments.rb +# to remove hostname in fragment caching +# so instead of caching different fragments for "example.com/posts/list" and +# "www.example.com/posts/list", instead it will just use "posts/list" +module CachingStuff + module ActionController + module Caching + module Fragments + def fragment_cache_key(key) + ActiveSupport::Cache.expand_cache_key(key.is_a?(Hash) ? url_for(key.merge(:host=>"")).split(":///").last : key.split('/')[1..-1].join('/'), :views) + end + end + end + end +end + +ActionController::Base.class_eval do + include CachingStuff::ActionController::Caching::Fragments +end