View Source EctoCache (EctoCache v1.0.0)
Implements an in-memory process to cache the results of database queries.
Summary
Functions
Gets the value of key
from the cache, if available. If the value has not
been cached yet or it has expired, executes the fun
function and cache
their result.
Returns a specification to start this module under a supervisor.
Deletes the cached key
value.
Same as EctoCache.delete/1
but designed to be used in a pipeline as a
result of a Ecto.Repo
write operation.
Starts the EctoCache
process.
Types
@opaque t()
Functions
@spec cache(atom(), (-> term()), {non_neg_integer() | :infinity}) :: term()
Gets the value of key
from the cache, if available. If the value has not
been cached yet or it has expired, executes the fun
function and cache
their result.
You can set lifetime
to the number of seconds you want the value to be
available or to infinity
so that it never expires. Every time the key
value is read, the availability time is reset again to lifetime
.
Example
iex> EctoCache.cache(:posts, &Repo.all(Post), 600)
[%Post{}, ...]
Returns a specification to start this module under a supervisor.
See Supervisor
.
@spec delete(atom()) :: :ok
Deletes the cached key
value.
Use this function when the cached values have changed and you want to disable the cache to avoid reading outdated data.
This function always returns :ok
regardless of whether or not a cached value
exists for the key
.
Examples
iex> EctoCache.delete(:posts)
:ok
@spec delete( {:ok, Ecto.Schema.t()}, atom() ) :: {:ok, Ecto.Schema.t()}
@spec delete( {:error, Ecto.Changeset.t()}, atom() ) :: {:error, Ecto.Changeset.t()}
Same as EctoCache.delete/1
but designed to be used in a pipeline as a
result of a Ecto.Repo
write operation.
This function integrates with Ecto.Repo.insert/2
, Ecto.Repo.update/2
and
Ecto.Repo.delete/2
, deleting the cache only if the operation was carried
out and returning the result of the database operation.
Examples
iex> Repo.insert(post) |> EctoCache.delete(:posts)
{:ok, %Post{}}
iex> Repo.update(post) |> EctoCache.delete(:posts)
{:ok, %Post{}}
iex> Repo.delete(post) |> EctoCache.delete(:posts)
{:ok, %Post{}}
Starts the EctoCache
process.
You should not use this function directly. Instead add EctoCache
to your
supervision tree as explained in the module documentation.