diff --git a/lib/request_cache/con_cache_store.ex b/lib/request_cache/con_cache_store.ex index 0f6fe5b..1df255c 100644 --- a/lib/request_cache/con_cache_store.ex +++ b/lib/request_cache/con_cache_store.ex @@ -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 diff --git a/lib/request_cache/config.ex b/lib/request_cache/config.ex index 1a38a15..82e2b47 100644 --- a/lib/request_cache/config.ex +++ b/lib/request_cache/config.ex @@ -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 diff --git a/lib/request_cache/plug.ex b/lib/request_cache/plug.ex index 17381cb..5047cda 100644 --- a/lib/request_cache/plug.ex +++ b/lib/request_cache/plug.ex @@ -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 @@ -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") @@ -36,9 +39,9 @@ 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) @@ -46,8 +49,8 @@ defmodule RequestCache.Plug do 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) diff --git a/lib/request_cache/util.ex b/lib/request_cache/util.ex index 0b667e5..ecabb19 100644 --- a/lib/request_cache/util.ex +++ b/lib/request_cache/util.ex @@ -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)}" @@ -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