Skip to content

Commit ec5ee34

Browse files
Steve Laingsteventux
Steve Laing
authored andcommitted
Cache travel advice atom feeds for 5 minutes.
1 parent dd59c2a commit ec5ee34

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

app/controllers/content_items_controller.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,8 @@ def set_access_control_allow_origin_header
9292
end
9393

9494
def set_expiry
95-
max_age = @content_item.content_item.cache_control.max_age
96-
cache_private = @content_item.content_item.cache_control.private?
97-
expires_in(max_age, public: !cache_private)
95+
expires_in(@content_item.cache_control_max_age(request.format),
96+
public: @content_item.cache_control_public?)
9897
end
9998

10099
def with_locale

app/presenters/content_item_presenter.rb

+10
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ def canonical_url
6161
end
6262
end
6363

64+
# The default behaviour to is honour the max_age
65+
# from the content-store response.
66+
def cache_control_max_age(_format)
67+
content_item.cache_control.max_age
68+
end
69+
70+
def cache_control_public?
71+
!content_item.cache_control.private?
72+
end
73+
6474
private
6575

6676
def display_date(timestamp, format = "%-d %B %Y")

app/presenters/travel_advice_presenter.rb

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ class TravelAdvicePresenter < ContentItemPresenter
22
include ContentItem::Parts
33
include ActionView::Helpers::TextHelper
44

5+
ATOM_CACHE_CONTROL_MAX_AGE = 300
6+
57
def page_title
68
if is_summary?
79
super
@@ -90,6 +92,11 @@ def atom_public_updated_at
9092
DateTime.parse(content_item["public_updated_at"])
9193
end
9294

95+
def cache_control_max_age(format)
96+
return ATOM_CACHE_CONTROL_MAX_AGE if format == "atom"
97+
content_item.cache_control.max_age
98+
end
99+
93100
private
94101

95102
# Treat summary as the first part

test/controllers/content_items_controller_test.rb

+8
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ class ContentItemsControllerTest < ActionController::TestCase
136136
assert_equal "max-age=20, public", @response.headers['Cache-Control']
137137
end
138138

139+
test "sets a longer cache-control header for travel advice atom feeds" do
140+
content_item = content_store_has_schema_example('travel_advice', 'full-country')
141+
get :show, params: { path: path_for(content_item), format: 'atom' }
142+
143+
assert_response :success
144+
assert_equal "max-age=300, public", @response.headers['Cache-Control']
145+
end
146+
139147
test "honours cache-control private items" do
140148
content_item = content_store_has_schema_example('coming_soon', 'coming_soon')
141149
content_store_has_item(content_item['base_path'], content_item, private: true)

0 commit comments

Comments
 (0)