Mentat v0.1.0 Mentat View Source
Provides a basic cache with ttls.
Usage
A cache must be given a name when its started.
Mentat.start_link(name: :my_cache)
After its been started you can store and retrieve values:
Mentat.put(:my_cache, user_id, user)
user = Mentat.get(:my_cache, user_id)
TTLs
Both put
and fetch
operations allow you to specify the key's TTL. If no
TTL is provided then the TTL is set to :infinity
. TTL times are always
in milliseconds.
Mentat.put(:my_cache, :key, "value", [ttl: 5_000])
Mentat.fetch(:my_cache, :key, [ttl: 5_000], fn key ->
{:commit, "value"}
end)
## Telemetry
Mentat publishes multiple telemetry events
Link to this section Summary
Functions
Returns a specification to start this module under a supervisor.
Fetches a value or executes the fallback function. The function can return
either {:commit, term()}
or {:ignore, term()}
. If {:commit, term()}
is
returned, the value will be stored in the cache before its returned. See the
"TTLs" section for a list of options.
Retrieves a value from a the cache. Returns nil
if the key is not found.
Callback implementation for Supervisor.init/1
.
Returns a list of all keys.
Removes all keys from the cache.
Puts a new key into the cache. See the "TTLs" section for a list of options.
Starts a new cache.
Link to this section Functions
Returns a specification to start this module under a supervisor.
See Supervisor
.
Fetches a value or executes the fallback function. The function can return
either {:commit, term()}
or {:ignore, term()}
. If {:commit, term()}
is
returned, the value will be stored in the cache before its returned. See the
"TTLs" section for a list of options.
Example
Mentat.fetch(:cache, user_id, fn user_id ->
case get_user(user_id) do
{:ok, user} ->
{:commit, user}
error ->
{:ignore, error}
end
end)
Retrieves a value from a the cache. Returns nil
if the key is not found.
Callback implementation for Supervisor.init/1
.
Returns a list of all keys.
Removes all keys from the cache.
Puts a new key into the cache. See the "TTLs" section for a list of options.
Starts a new cache.
Options:
:name
- The name of the cache
:cleanup_interval
- How often the janitor process will remove old keys (defaults to 5_000).