aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/controllers/application_controller.rb21
-rw-r--r--app/controllers/request_controller.rb9
-rw-r--r--app/helpers/application_helper.rb21
-rw-r--r--config/environment.rb3
-rw-r--r--todo.txt2
5 files changed, 43 insertions, 13 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 0f287f2e4..c0543ff9f 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -101,17 +101,32 @@ class ApplicationController < ActionController::Base
path = path.sub("/request/", "/request/" + first_three_digits + "/")
return path
end
+
def foi_fragment_cache_path(param)
path = foi_fragment_cache_part_path(param)
path = "/views" + path
- return File.join(self.cache_store.cache_path, path)
+ foi_cache_path = File.join(File.dirname(__FILE__), '../../cache')
+ return File.join(foi_cache_path, path)
end
def foi_fragment_cache_all_for_request(info_request)
+ # return stub path so admin can expire it
first_three_digits = info_request.id.to_s()[0..2]
path = "views/request/#{first_three_digits}/#{info_request.id}"
- return File.join(self.cache_store.cache_path, path)
+ foi_cache_path = File.join(File.dirname(__FILE__), '../../cache')
+ return File.join(foi_cache_path, path)
+ end
+ def foi_fragment_cache_exists?(key_path)
+ return File.exists?(key_path)
+ end
+ def foi_fragment_cache_read(key_path)
+ cached = File.read(key_path)
+ end
+ def foi_fragment_cache_write(key_path, content)
+ FileUtils.mkdir_p(File.dirname(key_path))
+ File.atomic_write(key_path) do |f|
+ f.write(content)
+ end
end
-
private
# Check the user is logged in
diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb
index 4588ef369..007cef5d1 100644
--- a/app/controllers/request_controller.rb
+++ b/app/controllers/request_controller.rb
@@ -563,8 +563,8 @@ class RequestController < ApplicationController
key = params.merge(:only_path => true)
key_path = foi_fragment_cache_path(key)
- if File.exists?(key_path)
- cached = File.read(key_path)
+ if foi_fragment_cache_exists?(key_path)
+ cached = foi_fragment_cache_read(key_path)
IncomingMessage # load global filename_to_mimetype XXX should move filename_to_mimetype to proper namespace
response.content_type = filename_to_mimetype(params[:file_name].join("/")) or 'application/octet-stream'
render_for_text(cached)
@@ -577,10 +577,7 @@ class RequestController < ApplicationController
# various fragment cache functions using Ruby Marshall to write the file
# which adds a header, so isnt compatible with images that have been
# extracted elsewhere from PDFs)
- FileUtils.mkdir_p(File.dirname(key_path))
- File.atomic_write(key_path) do |f|
- f.write(response.body)
- end
+ foi_fragment_cache_write(key_path, response.body)
end
def get_attachment
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 08908abee..f2a02962e 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -62,8 +62,25 @@ module ApplicationHelper
# Use our own algorithm for finding path of cache
def foi_cache(name = {}, options = nil, &block)
- name = @controller.foi_fragment_cache_part_path(name)
- @controller.fragment_for(output_buffer, name, options, &block)
+ if @controller.perform_caching
+ #name = @controller.foi_fragment_cache_part_path(name)
+ #@controller.fragment_for(output_buffer, name, options, &block)
+
+ key = name.merge(:only_path => true)
+ key_path = @controller.foi_fragment_cache_path(key)
+
+ if @controller.foi_fragment_cache_exists?(key_path)
+ cached = @controller.foi_fragment_cache_read(key_path)
+ output_buffer.concat(cached)
+ return
+ end
+
+ pos = output_buffer.length
+ content = block.call
+ @controller.foi_fragment_cache_write(key_path, output_buffer[pos..-1])
+ else
+ block.call
+ end
end
end
diff --git a/config/environment.rb b/config/environment.rb
index e99e43e7a..0ee753fc1 100644
--- a/config/environment.rb
+++ b/config/environment.rb
@@ -87,8 +87,7 @@ end
# Mime::Type.register "text/richtext", :rtf
# Mime::Type.register "application/x-mobile", :mobile
-# Include your application configuration below
-ActionController::Base.cache_store = :file_store, File.join(File.dirname(__FILE__), '../cache')
+# The Rails cache is set up by the Interlock plugin to use memcached
# Domain for URLs (so can work for scripts, not just web pages)
ActionMailer::Base.default_url_options[:host] = MySociety::Config.get("DOMAIN", 'localhost:3000')
diff --git a/todo.txt b/todo.txt
index 63ddcc4ae..45c1edfeb 100644
--- a/todo.txt
+++ b/todo.txt
@@ -56,6 +56,8 @@ Also freezes Abiword, but not catdoc
Performance
===========
+Doesn't need to have foi_fragment_cache_part_path in special foi_cache any more?
+
Varnish config
http://www.varnish-cache.org/wiki/VCLExampleCachingLoggedInUsers