aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/application_controller.rb41
-rw-r--r--app/controllers/general_controller.rb5
-rw-r--r--app/controllers/help_controller.rb2
-rw-r--r--app/controllers/public_body_controller.rb2
-rw-r--r--app/controllers/request_controller.rb6
-rw-r--r--app/controllers/track_controller.rb2
-rw-r--r--app/controllers/user_controller.rb1
7 files changed, 49 insertions, 10 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 405327952..5f18be2e5 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -14,10 +14,32 @@ class ApplicationController < ActionController::Base
layout "default"
include FastGettext::Translation # make functions like _, n_, N_ etc available)
before_filter :set_gettext_locale
-
+ before_filter :set_vary_header
# scrub sensitive parameters from the logs
filter_parameter_logging :password
+ def set_vary_header
+ response.headers['Vary'] = 'Cookie'
+ end
+
+ helper_method :anonymous_cache, :short_cache, :medium_cache, :long_cache
+ def anonymous_cache(time)
+ if session[:user_id].nil?
+ expires_in time, :public => true
+ end
+ end
+
+ def short_cache
+ anonymous_cache(60.seconds)
+ end
+
+ def medium_cache
+ anonymous_cache(60.minutes)
+ end
+
+ def long_cache
+ anonymous_cache(24.hours)
+ end
def set_gettext_locale
requested_locale = params[:locale] || session[:locale] || cookies[:locale] || request.env['HTTP_ACCEPT_LANGUAGE']
@@ -46,12 +68,17 @@ class ApplicationController < ActionController::Base
# egrep "CONSUME MEMORY: [0-9]{7} KB" production.log
around_filter :record_memory
def record_memory
- File.read("/proc/#{Process.pid}/status").match(/VmRSS:\s+(\d+)/)
- rss_before_action = $1.to_i
- yield
- File.read("/proc/#{Process.pid}/status").match(/VmRSS:\s+(\d+)/)
- rss_after_action = $1.to_i
- logger.info("PID: #{Process.pid}\tCONSUME MEMORY: #{rss_after_action - rss_before_action} KB\tNow: #{rss_after_action} KB\t#{request.url}")
+ record_memory = MySociety::Config.get('DEBUG_RECORD_MEMORY', false)
+ if record_memory
+ File.read("/proc/#{Process.pid}/status").match(/VmRSS:\s+(\d+)/)
+ rss_before_action = $1.to_i
+ yield
+ File.read("/proc/#{Process.pid}/status").match(/VmRSS:\s+(\d+)/)
+ rss_after_action = $1.to_i
+ logger.info("PID: #{Process.pid}\tCONSUME MEMORY: #{rss_after_action - rss_before_action} KB\tNow: #{rss_after_action} KB\t#{request.url}")
+ else
+ yield
+ end
end
# Set cookie expiry according to "remember me" checkbox, as per "An easier
diff --git a/app/controllers/general_controller.rb b/app/controllers/general_controller.rb
index 6e5c8c3fd..ffc97237a 100644
--- a/app/controllers/general_controller.rb
+++ b/app/controllers/general_controller.rb
@@ -20,9 +20,8 @@ class GeneralController < ApplicationController
# New, improved front page!
def frontpage
-
+ medium_cache
behavior_cache do
-
# get some example searches and public bodies to display
# either from config, or based on a (slow!) query if not set
body_short_names = MySociety::Config.get('FRONTPAGE_PUBLICBODY_EXAMPLES', '').split(/\s*;\s*/).map{|s| "'%s'" % s.gsub(/'/, "''") }.join(", ")
@@ -66,6 +65,7 @@ class GeneralController < ApplicationController
# Display WhatDoTheyKnow category from mySociety blog
def blog
+ medium_cache
@feed_autodetect = []
feed_url = MySociety::Config.get('BLOG_FEED', '')
if not feed_url.empty?
@@ -176,6 +176,7 @@ class GeneralController < ApplicationController
end
def custom_css
+ long_cache
@locale = self.locale_from_params()
render(:layout => false, :content_type => 'text/css')
end
diff --git a/app/controllers/help_controller.rb b/app/controllers/help_controller.rb
index ab1ef5c5f..c6d246b4c 100644
--- a/app/controllers/help_controller.rb
+++ b/app/controllers/help_controller.rb
@@ -10,6 +10,8 @@ class HelpController < ApplicationController
# we don't even have a control subroutine for most help pages, just see their templates
+ before_filter :long_cache
+
def unhappy
@info_request = nil
if params[:url_title]
diff --git a/app/controllers/public_body_controller.rb b/app/controllers/public_body_controller.rb
index c74959b17..05acf4868 100644
--- a/app/controllers/public_body_controller.rb
+++ b/app/controllers/public_body_controller.rb
@@ -11,6 +11,7 @@ require 'csv'
class PublicBodyController < ApplicationController
# XXX tidy this up with better error messages, and a more standard infrastructure for the redirect to canonical URL
def show
+ long_cache
if MySociety::Format.simplify_url_part(params[:url_name], 'body') != params[:url_name]
redirect_to :url_name => MySociety::Format.simplify_url_part(params[:url_name], 'body'), :status => :moved_permanently
return
@@ -80,6 +81,7 @@ class PublicBodyController < ApplicationController
end
def list
+ long_cache
# XXX move some of these tag SQL queries into has_tag_string.rb
@tag = params[:tag]
@locale = self.locale_from_params()
diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb
index 81dffaa80..472f18f6e 100644
--- a/app/controllers/request_controller.rb
+++ b/app/controllers/request_controller.rb
@@ -19,10 +19,11 @@ class RequestController < ApplicationController
include RequestControllerCustomStates
@@custom_states_loaded = true
end
- rescue MissingSourceFile
+ rescue MissingSourceFile, NameError
end
def show
+ medium_cache
@locale = self.locale_from_params()
PublicBody.with_locale(@locale) do
@@ -95,6 +96,7 @@ class RequestController < ApplicationController
# Extra info about a request, such as event history
def details
+ long_cache
@info_request = InfoRequest.find_by_url_title(params[:url_title])
if !@info_request.user_can_view?(authenticated_user)
render :template => 'request/hidden', :status => 410 # gone
@@ -106,6 +108,7 @@ class RequestController < ApplicationController
# Requests similar to this one
def similar
+ short_cache
@per_page = 25
@page = (params[:page] || "1").to_i
@info_request = InfoRequest.find_by_url_title(params[:url_title])
@@ -124,6 +127,7 @@ class RequestController < ApplicationController
end
def list
+ medium_cache
@view = params[:view]
if @view.nil?
diff --git a/app/controllers/track_controller.rb b/app/controllers/track_controller.rb
index 10b3418bd..e06701a5f 100644
--- a/app/controllers/track_controller.rb
+++ b/app/controllers/track_controller.rb
@@ -11,6 +11,8 @@ class TrackController < ApplicationController
protect_from_forgery # See ActionController::RequestForgeryProtection for details
+ before_filter :medium_cache
+
# Track all updates to a particular request
def track_request
@info_request = InfoRequest.find_by_url_title(params[:url_title])
diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb
index 2e3f6c9e0..d3c42c7f1 100644
--- a/app/controllers/user_controller.rb
+++ b/app/controllers/user_controller.rb
@@ -16,6 +16,7 @@ class UserController < ApplicationController
# Show page about a user
def show
+ long_cache
if MySociety::Format.simplify_url_part(params[:url_name], 'user', 32) != params[:url_name]
redirect_to :url_name => MySociety::Format.simplify_url_part(params[:url_name], 'user', 32), :status => :moved_permanently
return