|
| 1 | +defmodule NebulexCachexAdapter do |
| 2 | + @moduledoc """ |
| 3 | + Nebulex adapter for Cachex. |
| 4 | + """ |
| 5 | + |
| 6 | + # Provide Cache Implementation |
| 7 | + @behaviour Nebulex.Adapter |
| 8 | + |
| 9 | + import Nebulex.Helpers |
| 10 | + |
| 11 | + @compile {:inline, to_ttl: 1} |
| 12 | + |
| 13 | + ## Adapter |
| 14 | + |
| 15 | + @impl true |
| 16 | + defmacro __before_compile__(_), do: :ok |
| 17 | + |
| 18 | + @impl true |
| 19 | + def init(opts) do |
| 20 | + name = |
| 21 | + normalize_module_name([ |
| 22 | + opts[:name] || Keyword.fetch!(opts, :cache), |
| 23 | + Cachex |
| 24 | + ]) |
| 25 | + |
| 26 | + child_spec = |
| 27 | + opts |
| 28 | + |> Keyword.put(:name, name) |
| 29 | + |> Cachex.child_spec() |
| 30 | + |
| 31 | + {:ok, child_spec, %{name: name}} |
| 32 | + end |
| 33 | + |
| 34 | + @impl true |
| 35 | + def get(%{name: name}, key, _opts) do |
| 36 | + Cachex.get!(name, key) |
| 37 | + end |
| 38 | + |
| 39 | + @impl true |
| 40 | + def get_all(%{name: name}, keys, _opts) do |
| 41 | + Enum.reduce(keys, %{}, fn key, acc -> |
| 42 | + if value = Cachex.get!(name, key) do |
| 43 | + Map.put(acc, key, value) |
| 44 | + else |
| 45 | + acc |
| 46 | + end |
| 47 | + end) |
| 48 | + end |
| 49 | + |
| 50 | + @impl true |
| 51 | + def put(%{name: name}, key, value, ttl, :put, _opts) do |
| 52 | + Cachex.put!(name, key, value, ttl: to_ttl(ttl)) |
| 53 | + end |
| 54 | + |
| 55 | + def put(%{name: name}, key, value, ttl, :replace, _opts) do |
| 56 | + Cachex.update!(name, key, value, ttl: to_ttl(ttl)) |
| 57 | + end |
| 58 | + |
| 59 | + def put(%{name: name}, key, value, ttl, :put_new, _opts) do |
| 60 | + if Cachex.get!(name, key) do |
| 61 | + false |
| 62 | + else |
| 63 | + Cachex.put!(name, key, value, ttl: to_ttl(ttl)) |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + @impl true |
| 68 | + def put_all(adapter_meta, entries, ttl, on_write, opts) when is_map(entries) do |
| 69 | + put_all(adapter_meta, :maps.to_list(entries), ttl, on_write, opts) |
| 70 | + end |
| 71 | + |
| 72 | + def put_all(%{name: name}, entries, ttl, :put, _opts) when is_list(entries) do |
| 73 | + Cachex.put_many!(name, entries, ttl: to_ttl(ttl)) |
| 74 | + end |
| 75 | + |
| 76 | + def put_all(%{name: name}, entries, ttl, :put_new, _opts) when is_list(entries) do |
| 77 | + {keys, _} = Enum.unzip(entries) |
| 78 | + |
| 79 | + Cachex.transaction!(name, keys, fn worker -> |
| 80 | + if Enum.any?(keys, &(worker |> Cachex.exists?(&1) |> elem(1))) do |
| 81 | + false |
| 82 | + else |
| 83 | + Cachex.put_many!(worker, entries, ttl: to_ttl(ttl)) |
| 84 | + end |
| 85 | + end) |
| 86 | + end |
| 87 | + |
| 88 | + @impl true |
| 89 | + def delete(%{name: name}, key, _opts) do |
| 90 | + true = Cachex.del!(name, key) |
| 91 | + :ok |
| 92 | + end |
| 93 | + |
| 94 | + @impl true |
| 95 | + def take(%{name: name}, key, _opts) do |
| 96 | + Cachex.take!(name, key) |
| 97 | + end |
| 98 | + |
| 99 | + @impl true |
| 100 | + def has_key?(%{name: name}, key) do |
| 101 | + {:ok, bool} = Cachex.exists?(name, key) |
| 102 | + bool |
| 103 | + end |
| 104 | + |
| 105 | + @impl true |
| 106 | + def ttl(%{name: name}, key) do |
| 107 | + cond do |
| 108 | + ttl = Cachex.ttl!(name, key) -> |
| 109 | + ttl |
| 110 | + |
| 111 | + Cachex.get!(name, key) -> |
| 112 | + :infinity |
| 113 | + |
| 114 | + true -> |
| 115 | + nil |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + @impl true |
| 120 | + def expire(%{name: name}, key, ttl) do |
| 121 | + Cachex.expire!(name, key, to_ttl(ttl)) |
| 122 | + end |
| 123 | + |
| 124 | + @impl true |
| 125 | + def touch(%{name: name}, key) do |
| 126 | + Cachex.touch!(name, key) |
| 127 | + end |
| 128 | + |
| 129 | + @impl true |
| 130 | + def incr(%{name: name}, key, incr, :infinity, opts) do |
| 131 | + Cachex.incr!(name, key, incr, initial: opts[:default] || 0) |
| 132 | + end |
| 133 | + |
| 134 | + def incr(%{name: name}, key, incr, ttl, opts) do |
| 135 | + Cachex.transaction!(name, [key], fn worker -> |
| 136 | + counter = Cachex.incr!(worker, key, incr, initial: opts[:default] || 0) |
| 137 | + if ttl = to_ttl(ttl), do: Cachex.expire!(worker, key, ttl) |
| 138 | + counter |
| 139 | + end) |
| 140 | + end |
| 141 | + |
| 142 | + @impl true |
| 143 | + def size(%{name: name}) do |
| 144 | + Cachex.size!(name) |
| 145 | + end |
| 146 | + |
| 147 | + @impl true |
| 148 | + def flush(%{name: name}) do |
| 149 | + size = Cachex.size!(name) |
| 150 | + true = Cachex.reset!(name) |
| 151 | + size |
| 152 | + end |
| 153 | + |
| 154 | + ## Private Functions |
| 155 | + |
| 156 | + defp to_ttl(:infinity), do: nil |
| 157 | + defp to_ttl(ttl), do: ttl |
| 158 | +end |
0 commit comments