Patches: Browse | Submit New | Admin

[#17713] [PATCH] Read cached files based on page_cache_directory setting

Date:
2008-02-02 01:24
Priority:
3
Submitted By:
Phil Misiowiec (alertphil)
Assigned To:
Evan Weaver (evanweaver)
Category:
mongrel
State:
Open
Summary:
[PATCH] Read cached files based on page_cache_directory setting

Detailed description
Problem:

A page cache directory specified in the Rails config file (e.g. environment.rb) is not read by Mongrel, e.g.

config.action_controller.page_cache_directory = RAILS_ROOT + "/tmp/cache"

Thus, when testing page caching on a local machine, the log shows completed Mongrel requests even for cached pages.
In a prod environment, this issue is largely moot since you'd likely be using an HTTP proxy front-end bypassing Mongrel
entirely for cached output. Whether or not your local dev environment uses a Mongrel cluster and an HTTP proxy is besides
the point, as this one is more of an annoyance that that affects your ability to test caching if you're keeping things
simple (due to system resource constraints, laziness, whatever), as found in numerous blog posts and forum entries.

Solution:

The following patch should do the trick. I've tested it against 3 scenarios:

-Caching disabled (in dev)
-Caching enabled but config.action_controller.page_cache_directory not set (thus defaulting to RAILS_ROOT/public)
-Caching enabled with config.action_controller.page_cache_directory set to a path outside of public

Important Note: you'll run into a problem with Mongrel v1.1.3 unless you patch up handlers.rb. See
http://rubyforge.org/tracker/index.php?func=detail&aid=17712&group_id=1306&atid=5145 for details.

Original file: http://mongrel.rubyforge.org/svn/tags/rel_1-1-2/lib/mongrel/rails.rb

+++ rails.rb
@@ -53,17 +53,21 @@
         path_info = request.params[Mongrel::Const::PATH_INFO]
         rest_operator = request.params[Mongrel::Const::REQUEST_URI][/^#{Regexp.escape path_info}(;[^\?]+)/, 1].to_s
         path_info.chomp!("/")
+        path_info = "/index" if path_info.empty?
+
+        cache_dir = ActionController::Base.page_cache_directory
+        cache_dir.chomp!("/")
         
-        page_cached = path_info + rest_operator + ActionController::Base.page_cache_extension
+        page_cached = cache_dir + path_info + rest_operator + ActionController::Base.page_cache_extension
         get_or_head = @@file_only_methods.include? request.params[Mongrel::Const::REQUEST_METHOD]
 
         if get_or_head and @files.can_serve(path_info)
           # File exists as-is so serve it up
           @files.process(request,response)
-        elsif get_or_head and @files.can_serve(page_cached)
+        elsif get_or_head and (files = Mongrel::DirHandler.new(nil,false)).can_serve(page_cached);
           # Possible cached page, serve it up
           request.params[Mongrel::Const::PATH_INFO] = page_cached
-          @files.process(request,response)
+          files.process(request,response)
         else
           begin
             cgi = Mongrel::CGIWrapper.new(request, response)

Add A Comment: Notepad

Please login


Followup

Message
Date: 2008-02-02 18:19
Sender: Phil Misiowiec

Actually, please use the following code, as it also resolves
an issue when
caching is disabled after being enabled (and still reading the
cached files):

+++ rails.rb
@@ -53,17 +53,22 @@
         path_info = request.params[Mongrel::Const::PATH_INFO]
         rest_operator = 
request.params[Mongrel::Const::REQUEST_URI][/^#{Regexp.escape 
path_info}(;[^\?]+)/, 1].to_s
         path_info.chomp!("/")
+        path_info = "/index" if path_info.empty?
+
+        do_caching = ActionController::Base.perform_caching
+        cache_dir = ActionController::Base.page_cache_directory
+        cache_dir.chomp!("/")
         
-        page_cached = path_info + rest_operator + 
ActionController::Base.page_cache_extension
+        page_cached = cache_dir + path_info + rest_operator + 
ActionController::Base.page_cache_extension
         get_or_head = @@file_only_methods.include? 
request.params[Mongrel::Const::REQUEST_METHOD]
 
         if get_or_head and @files.can_serve(path_info)
           # File exists as-is so serve it up
           @files.process(request,response)
-        elsif get_or_head and @files.can_serve(page_cached)
+        elsif do_caching and get_or_head and (files = 
Mongrel::DirHandler.new(nil,false)).can_serve(page_cached);
           # Possible cached page, serve it up
           request.params[Mongrel::Const::PATH_INFO]
= page_cached
-          @files.process(request,response)
+          files.process(request,response)
         else
           begin
             cgi = Mongrel::CGIWrapper.new(request, response)

Attached Files:

Name Description Download
No Files Currently Attached

Changes:

No Changes Have Been Made to This Item