Skip to content

Commit

Permalink
Add cache key to response header (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaAK authored Aug 22, 2024
1 parent 526fc8b commit 411befb
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/request_cache/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 411befb

Please sign in to comment.