ExJsonschema.Cache behaviour (ExJsonschema v0.1.17)
View SourceBehaviour for caching compiled JSON schemas.
This behaviour defines the interface for caching compiled schemas by their ID. Implementations can be stateless modules, GenServers, or any other approach.
Configuration
Configure which cache module to use:
config :ex_jsonschema, cache: MyApp.EtsCacheDefault
By default, uses ExJsonschema.Cache.Noop which disables caching.
Testing
Use ExJsonschema.Cache.Test for isolated test caches:
# Async tests (process-isolated)
setup do
test_cache = start_supervised!({Agent, fn -> %{} end})
cleanup = ExJsonschema.Cache.Test.setup_process_mode(test_cache)
on_exit(cleanup)
:ok
end
# Non-async tests (global, works with spawns)
setup do
test_cache = start_supervised!({Agent, fn -> %{} end})
cleanup = ExJsonschema.Cache.Test.setup_global_mode(test_cache)
on_exit(cleanup)
:ok
end
Summary
Callbacks
Clears all entries from the cache.
Removes a specific entry from the cache.
Retrieves a compiled schema from the cache.
Stores a compiled schema in the cache.
Callbacks
@callback clear() :: :ok
Clears all entries from the cache.
Returns :ok.
@callback delete(key :: binary()) :: :ok
Removes a specific entry from the cache.
Returns :ok regardless of whether the key existed.
Retrieves a compiled schema from the cache.
Returns {:ok, compiled_schema} if found, {:error, :not_found} otherwise.
Stores a compiled schema in the cache.
Returns :ok on success.