Packages
Plug to cache requests declaratively for either GraphQL or Phoenix, this plug is intended to short circuit all json/decoding or parsing a server would normally do
Current section
Files
Jump to
Current section
Files
lib/request_cache/util.ex
defmodule RequestCache.Util do
require Logger
@moduledoc false
def parse_gql_name(query_string) do
case Regex.run(~r/^(?:query) ([^\({]+(?=\(|{))/, query_string, capture: :all_but_first) do
[query_name] -> String.trim(query_name)
_ -> nil
end
end
def merge_default_opts(opts) do
Keyword.merge([
ttl: RequestCache.Config.default_ttl(),
cache: RequestCache.Config.request_cache_module()
], opts)
end
def create_key(url_path, query_string) do
"#{url_path}:#{hash_string(query_string)}"
end
defp hash_string(query_string) do
:md5 |> :crypto.hash(query_string) |> Base.encode16(padding: false)
end
def log_cache_disabled_message do
Logger.debug("RequestCache requested but hasn't been enabled, ensure query has a name and the RequestCache.Plug is part of your Endpoint")
end
def verbose_log(message) do
if RequestCache.Config.verbose?() do
Logger.debug(message)
end
end
end