Skip to content

Commit

Permalink
feat: allow for different HTTP methods to be configured and fix a con…
Browse files Browse the repository at this point in the history
…cache store bug if options are configured without a name
  • Loading branch information
MikaAK committed Mar 20, 2024
1 parent b8858f5 commit f66c055
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
4 changes: 3 additions & 1 deletion lib/request_cache/con_cache_store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ cond do
@default_name RequestCache.Config.default_concache_opts()[:name]

def start_link(opts \\ []) do
opts = Keyword.merge(RequestCache.Config.default_concache_opts(), opts)
opts = RequestCache.Config.default_concache_opts()
|> Keyword.merge(opts)
|> Keyword.put_new(:global_ttl, :timer.hours(1))

ConCache.start_link(opts)
end
Expand Down
13 changes: 11 additions & 2 deletions lib/request_cache/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,22 @@ defmodule RequestCache.Config do
!!Application.get_env(@app, :enabled?, true)
end

def allowed_graphql_methods do
Application.get_env(@app, :allowed_graphql_methods, ["GET"])
end

def allowed_rest_methods do
Application.get_env(@app, :allowed_rest_methods, ["GET"])
end

def default_concache_opts do
Application.get_env(@app, :default_concache_opts) || [
name: :con_cache_request_cache_store,
opts = Application.get_env(@app, :default_concache_opts) || [
global_ttl: default_ttl(),
acquire_lock_timeout: :timer.seconds(1),
ttl_check_interval: :timer.seconds(1),
ets_options: [write_concurrency: true, read_concurrency: true]
]

Keyword.put_new(opts, :name, :con_cache_request_cache_store)
end
end
11 changes: 7 additions & 4 deletions lib/request_cache/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ defmodule RequestCache.Plug do

# This is compile time so we can check quicker
@graphql_paths RequestCache.Config.graphql_paths()
@allowed_graphql_methods RequestCache.Config.allowed_graphql_methods()
@allowed_rest_methods RequestCache.Config.allowed_rest_methods()
@request_cache_header "rc-cache-status"

def request_cache_header, do: @request_cache_header
Expand All @@ -26,6 +28,7 @@ defmodule RequestCache.Plug do
def call(conn, opts) do
if RequestCache.Config.enabled?() do
Util.verbose_log("[RequestCache.Plug] Hit request cache while enabled")

call_for_api_type(conn, opts)
else
Util.verbose_log("[RequestCache.Plug] Hit request cache while disabled")
Expand All @@ -36,18 +39,18 @@ defmodule RequestCache.Plug do

defp call_for_api_type(%Plug.Conn{
request_path: path,
method: "GET",
method: method,
query_string: query_string
} = conn, opts) when path in @graphql_paths do
} = conn, opts) when path in @graphql_paths and method in @allowed_graphql_methods do
Util.verbose_log("[RequestCache.Plug] GraphQL query detected")

maybe_return_cached_result(conn, opts, path, query_string)
end

defp call_for_api_type(%Plug.Conn{
request_path: path,
method: "GET"
} = conn, opts) when path not in @graphql_paths do
method: method
} = conn, opts) when path not in @graphql_paths and method in @allowed_rest_methods do
Util.verbose_log("[RequestCache.Plug] REST path detected")

cache_key = rest_cache_key(conn)
Expand Down
12 changes: 11 additions & 1 deletion lib/request_cache/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ defmodule RequestCache.Util do
@moduledoc false

@whitelisted_modules [DateTime, NaiveDateTime, Date, Time, File.Stat, MapSet, Regex, URI, Version]
@allowed_graphql_methods Enum.join(RequestCache.Config.allowed_graphql_methods(), ", ")
@allowed_rest_methods Enum.join(RequestCache.Config.allowed_rest_methods(), ", ")

def create_key(url_path, query_string) do
"#{url_path}:#{hash_string(query_string)}"
Expand All @@ -14,7 +16,15 @@ defmodule RequestCache.Util do
end

def log_cache_disabled_message do
Logger.warning("RequestCache requested but hasn't been enabled, ensure RequestCache.Plug is part of your endpoint.ex file")
if RequestCache.Config.verbose?() do
Logger.warning("""
RequestCache requested but hasn't been enabled, this can happen for one of the following reasons:
1) RequestCache.Plug is not currently part of your endpoint.ex file
2) The GraphQL HTTP method is not one of #{@allowed_graphql_methods}
2) The REST HTTP method is not one of #{@allowed_rest_methods}
""")
end
end

def verbose_log(message) do
Expand Down

0 comments on commit f66c055

Please sign in to comment.