From ae5602b56c9307067d06759ffe2ae8cbf38c6c97 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Wed, 15 May 2024 10:59:36 +0200 Subject: [PATCH] Do not convert cache duration to hours Hours is not fine enough as granularity for good cache control. For this reason, the `Cache-Control` header operates with a granularity of seconds as well. Let's also work with seconds here. This is literally a breaking change, but it will only break caches. --- app/controllers/alchemy/json_api/nodes_controller.rb | 4 +++- app/controllers/alchemy/json_api/pages_controller.rb | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/controllers/alchemy/json_api/nodes_controller.rb b/app/controllers/alchemy/json_api/nodes_controller.rb index 0ce2e59..3829f21 100644 --- a/app/controllers/alchemy/json_api/nodes_controller.rb +++ b/app/controllers/alchemy/json_api/nodes_controller.rb @@ -3,6 +3,8 @@ module Alchemy module JsonApi class NodesController < JsonApi::BaseController + THREE_HOURS = 10800 + def index @nodes = node_scope.select(:id, :updated_at) if stale?(last_modified: @nodes.maximum(:updated_at), etag: @nodes) @@ -17,7 +19,7 @@ def index private def cache_duration - ENV.fetch("ALCHEMY_JSON_API_CACHE_DURATION", 3).to_i.hours + ENV.fetch("ALCHEMY_JSON_API_CACHE_DURATION", THREE_HOURS).to_i end def jsonapi_meta(nodes) diff --git a/app/controllers/alchemy/json_api/pages_controller.rb b/app/controllers/alchemy/json_api/pages_controller.rb index ffaafdb..fde1739 100644 --- a/app/controllers/alchemy/json_api/pages_controller.rb +++ b/app/controllers/alchemy/json_api/pages_controller.rb @@ -3,6 +3,8 @@ module Alchemy module JsonApi class PagesController < JsonApi::BaseController + THREE_HOURS = 10800 + before_action :load_page_for_cache_key, only: :show def index @@ -47,7 +49,7 @@ def render_pages_json(allowed) end def cache_duration - ENV.fetch("ALCHEMY_JSON_API_CACHE_DURATION", 3).to_i.hours + ENV.fetch("ALCHEMY_JSON_API_CACHE_DURATION", THREE_HOURS).to_i end def caching_options