Current section
Files
Jump to
Current section
Files
lib/helper/cache.ex
# Copyright (c) 2021 Anand Panchapakesan
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
defmodule Helper.Cache do
@moduledoc false
@cache_env [
adapter: Nebulex.Adapters.Partitioned,
stats: true,
keyslot: PartitionedCache.JumpingHashSlot,
primary: [
gc_interval: :timer.seconds(7_200),
backend: :shards
]
]
@spec __using__(Keyword.t()) :: Macro.output() | no_return()
defmacro __using__(otp_app: app, cache: cache) do
import Helper, only: [merge: 2]
import Helper.Ecto.Util, only: [ast2mod!: 1]
unless is_atom(app),
do: raise(CompileError, description: "#{inspect(app)}: not a valid application")
cache = ast2mod!(cache)
unless is_atom(cache),
do: raise(CompileError, description: "#{inspect(cache)}: not a valid module name")
if Kernel.function_exported?(cache, :__info__, 1),
do: raise(ArgumentError, "#{inspect(cache)}: cache cannot be an existing module")
{adapter, config} =
@cache_env
|> merge(Application.get_env(app, cache, []))
|> Keyword.pop!(:adapter)
Application.put_env(app, cache, config)
body =
quote generated: true, context: cache,
bind_quoted: [app: app, adapter: adapter] do
use Nebulex.Cache,
otp_app: app,
adapter: adapter
end
{:module, _, _, _} = Module.create(cache, body, Macro.Env.location(__CALLER__))
quote do: :ok
end
end