Skip to content

Commit

Permalink
Do not convert cache duration to hours
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mamhoff committed May 15, 2024
1 parent 7b1d334 commit ae5602b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion app/controllers/alchemy/json_api/nodes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/alchemy/json_api/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ae5602b

Please sign in to comment.