NebulexEcto v0.1.0 NebulexEcto.Repo View Source
Wrapper/Facade on top of Nebulex.Cache
and Ecto.Repo
.
This module encapsulates the access to the Ecto repo and Nebulex cache,
providing a set of functions compliant with the Ecto.Repo
API.
For retrieve-like functions, the wrapper access the cache first, if the requested data is found, then it is returned right away, otherwise, the wrapper tries to retrieve the data from the repo (database), and if the data is found, then it is cached so the next time it can be retrieved directly from cache.
For write functions (insert, update, delete, …), the wrapper runs the
eviction logic, which can be delete the data from cache or just replace it;
depending on the :nbx_evict
option.
When used, NebulexEcto.Repo
expects the :repo
and :cache
as options.
For example:
defmodule MyApp.CacheableRepo do
use NebulexEcto.Repo,
cache: MyApp.Cache,
repo: MyApp.Repo
end
The cache and repo respectively:
defmodule MyApp.Cache do
use Nebulex.Cache,
otp_app: :my_app,
adapter: Nebulex.Adapters.Local
end
defmodule MyApp.Repo do
use Ecto.Repo,
otp_app: :my_app,
adapter: Ecto.Adapters.Postgres
end
And this is an example of how their configuration would looks like:
config :my_app, MyApp.Cache,
gc_interval: 3600
config :my_app, MyApp.Repo,
database: "ecto_simple",
username: "postgres",
password: "postgres",
hostname: "localhost"
Compile-time configuration options
:cache
- a compile-time option that specifies the Nebulex cache to be used by the wrapper.:repo
- a compile-time option that specifies the Ecto repo to be used by the wrapper.
To configure the cache and repo, see Nebulex
and Ecto
documentation
respectively.
Shared options
Almost all of the operations below accept the following options:
:nbx_key
- specifies the key to be used for cache access. By default is set to{Ecto.Schema.t, id :: term}
, assuming the schema has a fieldid
which is the primary key; if this is not your case, you must provide the:nbx_key
.:nbx_evict
- specifies the eviction strategy, if it is set to:delete
(the default), then the key is removed from cache, and if it is set to:replace
, then the key is replaced with the new value into the cache.
Link to this section Summary
Functions
Retrieves the compile time configuration
Link to this section Functions
Retrieves the compile time configuration.