From 411befb0a7a7afbbdebdf0802dd7a7807282b4bf Mon Sep 17 00:00:00 2001 From: Mika Kalathil Date: Thu, 22 Aug 2024 15:04:41 -0700 Subject: [PATCH] Add cache key to response header (#31) --- lib/request_cache/plug.ex | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/request_cache/plug.ex b/lib/request_cache/plug.ex index 17381cb..3c9173b 100644 --- a/lib/request_cache/plug.ex +++ b/lib/request_cache/plug.ex @@ -18,6 +18,7 @@ defmodule RequestCache.Plug do @request_cache_header "rc-cache-status" def request_cache_header, do: @request_cache_header + def request_cache_key_header, do: @request_cache_key_header @impl Plug def init(opts), do: opts @@ -65,7 +66,7 @@ defmodule RequestCache.Plug do Metrics.inc_rest_cache_hit(%{cache_key: cache_key}) Util.verbose_log("[RequestCache.Plug] Returning cached result for #{cache_key}") - halt_and_return_result(conn, cached_result) + halt_and_return_result(conn, cache_key, cached_result) {:error, e} -> log_error(e, conn, opts) @@ -91,7 +92,7 @@ defmodule RequestCache.Plug do Metrics.inc_graphql_cache_hit(event_metadata(conn, cache_key, opts)) Util.verbose_log("[RequestCache.Plug] Returning cached result for #{cache_key}") - halt_and_return_result(conn, cached_result) + halt_and_return_result(conn, cache_key, cached_result) {:error, e} -> log_error(e, conn, opts) @@ -100,10 +101,11 @@ defmodule RequestCache.Plug do end end - defp halt_and_return_result(conn, result) do + defp halt_and_return_result(conn, cache_key, result) do conn |> Plug.Conn.halt() |> Plug.Conn.put_resp_header(@request_cache_header, "HIT") + |> Plug.Conn.put_resp_header(@request_cache_key_header, cache_key) |> maybe_put_content_type(result) |> Plug.Conn.send_resp(200, result) end