Current section

Files

Jump to
elixir_cache lib cache sandbox.ex
Raw

lib/cache/sandbox.ex

defmodule Cache.Sandbox do
@moduledoc """
This module is the adapter used by the SandboxRegistry to mock out all the other adapters
therefore it must implement all features shared across all adapters. It uses a basic `Agent`
and shouldn't be used in production. It's good for dev & test to avoid needing dependencies
"""
use Agent
@behaviour Cache
@impl Cache
def opts_definition, do: []
def start_link(opts \\ []) do
with {:error, {:already_started, pid}} <- Agent.start_link(fn -> %{} end, opts) do
{:ok, pid}
end
end
@impl Cache
def child_spec({cache_name, opts}) do
%{
id: "#{cache_name}_anana_cache_agent",
start: {Cache.Agent, :start_link, [Keyword.put(opts, :name, cache_name)]}
}
end
@impl Cache
def get(cache_name, key, _opts \\ []) do
Agent.get(cache_name, fn state ->
{:ok, Map.get(state, key)}
end)
end
@impl Cache
def put(cache_name, key, _ttl \\ nil, value, _opts \\ []) do
Agent.update(cache_name, fn state ->
Map.put(state, key, value)
end)
end
@impl Cache
def delete(cache_name, key, _opts \\ []) do
Agent.update(cache_name, fn state ->
Map.delete(state, key)
end)
end
def hash_delete(cache_name, key, hash_key, _opts) do
Agent.update(cache_name, fn state ->
Map.update(state, key, %{}, &Map.delete(&1, hash_key))
end)
end
def hash_get(cache_name, key, hash_key, _opts) do
Agent.get(cache_name, fn state ->
{:ok, state[key][hash_key]}
end)
end
def hash_get_all(cache_name, key, _opts) do
Agent.get(cache_name, fn state ->
{:ok, state[key]}
end)
end
def hash_values(cache_name, key, _opts) do
Agent.get(cache_name, fn state ->
{:ok, Map.values(state[key] || %{})}
end)
end
def hash_set(cache_name, key, hash_key, value, _opts) do
Agent.update(cache_name, fn state ->
Map.update(state, key, %{hash_key => value}, &Map.put(&1, hash_key, value))
end)
end
def hash_set_many(cache_name, key, hash_key_value_tuples, _opts) do
Agent.update(cache_name, fn state ->
Map.put(state, key, Map.new(hash_key_value_tuples))
end)
end
end