ResourceCache behaviour (Resource Cache v0.0.4) View Source

Fast caching with clear syntax.

Quick Setup

def deps do
  [
    {:resource_cache, "~> 0.1"}
  ]
end

Define a cache by setting a resource (in this case Ecto schema) and source. (The Ecto repo to query.)

defmodule MyApp.Categories do
  use ResourceCache
  resource MyApp.Category
  source :ecto, repo: MyApp.Repo
end

Now the cache can be used for fast listing:

iex> MyApp.Categories.list()
[%MyApp.Category{}, ...]

Indices can be added to do quick lookups by value:

defmodule MyApp.Categories do
  use ResourceCache
  resource MyApp.Category
  source :ecto, repo: MyApp.Repo

  index :slug, primary: true
end

Now get_by_slug/1 can be used. In addition to the standard get_by_slug, get/1 is also available since it was defined as primary index.

iex> MyApp.Categories.get("electronics")
%MyApp.Category{}
iex> MyApp.Categories.get_by_slug("electronics")
%MyApp.Category{}
iex> MyApp.Categories.get("fake")
nil
iex> MyApp.Categories.get_by_slug("fake")
nil

There is no limit to the amount of indices that can be added.

It is possible to pass an optional type for each index, to generate cleaner specs for the function arguments.

index :slug, primary: true, type: String.t

Link to this section Summary

Link to this section Types

Specs

hook() ::
  (cache :: module() -> :ok | :unhook)
  | (cache :: module(), changes :: ResourceCache.Changeset.t() -> nil)

Specs

resource() :: %{optional(atom()) => term()}

Link to this section Functions

Link to this macro

filter(field_or_opts \\ [], filter)

View Source (macro)

Specs

filter(atom() | Keyword.t(), term()) :: term()
Link to this macro

filter(field, opts, filter)

View Source (macro)

Specs

filter(atom(), Keyword.t(), term()) :: term()
Link to this macro

index(field, opts \\ [])

View Source (macro)

Specs

index(atom(), Keyword.t()) :: term()
Link to this macro

on_configure(callback, opts \\ [])

View Source (macro)

Specs

on_configure(hook(), Keyword.t()) :: term()
Link to this macro

on_update(callback, opts \\ [])

View Source (macro)

Specs

on_update(hook(), Keyword.t()) :: term()
Link to this macro

optimize(field \\ nil, opts \\ [], optimizer)

View Source (macro)
Link to this macro

reject(field_or_opts \\ [], rejecter)

View Source (macro)

Specs

reject(atom() | Keyword.t(), term()) :: term()
Link to this macro

reject(field, opts, rejecter)

View Source (macro)

Specs

reject(atom(), Keyword.t(), term()) :: term()
Link to this macro

resource(resource, opts \\ [], convert \\ [])

View Source (macro)

Specs

resource(module(), Keyword.t(), term()) :: term()
Link to this macro

source(source, opts \\ [])

View Source (macro)

Specs

source(module(), Keyword.t()) :: term()
Link to this macro

type(type, opts \\ [])

View Source (macro)

Specs

type(atom(), Keyword.t()) :: term()

Link to this section Callbacks

Specs

__config__() :: ResourceCache.Config.t()

Specs

__config__(atom()) :: any()

Specs

__process__([term()]) :: [term()]

Specs

__type__() :: :cache | :source | :bridge