Skip to content

Commit

Permalink
Merge pull request #941 from alphagov/cache-travel-advice-atom-feeds
Browse files Browse the repository at this point in the history
Cache travel advice atom feeds for 5 minutes.
  • Loading branch information
steventux authored Jun 20, 2018
2 parents c5967c8 + ec5ee34 commit 4cfe647
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
5 changes: 2 additions & 3 deletions app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ def set_access_control_allow_origin_header
end

def set_expiry
max_age = @content_item.content_item.cache_control.max_age
cache_private = @content_item.content_item.cache_control.private?
expires_in(max_age, public: !cache_private)
expires_in(@content_item.cache_control_max_age(request.format),
public: @content_item.cache_control_public?)
end

def with_locale
Expand Down
10 changes: 10 additions & 0 deletions app/presenters/content_item_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ def canonical_url
end
end

# The default behaviour to is honour the max_age
# from the content-store response.
def cache_control_max_age(_format)
content_item.cache_control.max_age
end

def cache_control_public?
!content_item.cache_control.private?
end

private

def display_date(timestamp, format = "%-d %B %Y")
Expand Down
7 changes: 7 additions & 0 deletions app/presenters/travel_advice_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class TravelAdvicePresenter < ContentItemPresenter
include ContentItem::Parts
include ActionView::Helpers::TextHelper

ATOM_CACHE_CONTROL_MAX_AGE = 300

def page_title
if is_summary?
super
Expand Down Expand Up @@ -90,6 +92,11 @@ def atom_public_updated_at
DateTime.parse(content_item["public_updated_at"])
end

def cache_control_max_age(format)
return ATOM_CACHE_CONTROL_MAX_AGE if format == "atom"
content_item.cache_control.max_age
end

private

# Treat summary as the first part
Expand Down
8 changes: 8 additions & 0 deletions test/controllers/content_items_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ class ContentItemsControllerTest < ActionController::TestCase
assert_equal "max-age=20, public", @response.headers['Cache-Control']
end

test "sets a longer cache-control header for travel advice atom feeds" do
content_item = content_store_has_schema_example('travel_advice', 'full-country')
get :show, params: { path: path_for(content_item), format: 'atom' }

assert_response :success
assert_equal "max-age=300, public", @response.headers['Cache-Control']
end

test "honours cache-control private items" do
content_item = content_store_has_schema_example('coming_soon', 'coming_soon')
content_store_has_item(content_item['base_path'], content_item, private: true)
Expand Down

0 comments on commit 4cfe647

Please sign in to comment.