Fiat.CacheServer (fiat v1.0.0) View Source

Fiat is a module to handle basic caching needs. Behind the scenes it leverages an ets table to store objects and a GenServer to maintain the state of the current keys.

Usage

Add Fiat.CacheServer to your application's supervision tree. Because Fiat.CacheServer is registered with its module name, it can be accessed without providing a pid to access the process.

For example:

children = [
  ...
  Fiat.CacheServer
]
...

Link to this section Summary

Functions

Caches an object using a cache_key.

Returns a specification to start this module under a supervisor.

Clears stale items from the cache.

Fetches the cached object for a particular key.

Fetches the cached object for a particular key. If the cache_key is not present in the cache, it executes the provided query_fn paramter, stores the result in the cache and returns it.

Removes the given key from the cache if it exists.

Stops the GenServer.

Link to this section Functions

Link to this function

cache_object(cache_key, object, expires_in \\ 300)

View Source

Specs

cache_object(term(), term(), integer()) :: true

Caches an object using a cache_key.

Examples

iex> Fiat.CacheServer.cache_object("data", {"code", 2})
true

iex> Fiat.CacheServer.cache_object("data", {"code", 2}, 10)
true

Returns a specification to start this module under a supervisor.

See Supervisor.

Clears stale items from the cache.

Examples

iex> Fiat.CacheServer.clear_stale_objects
[]

Specs

fetch_object(term()) :: term() | nil

Fetches the cached object for a particular key.

Returns object if it exists in the cache, otherwise returns nil.

Examples

iex> Fiat.CacheServer.cache_object("data", {"code", 2})
iex> Fiat.CacheServer.fetch_object("data")
{"code", 2}

iex> Fiat.CacheServer.fetch_object("data_old")
nil
Link to this function

fetch_object(cache_key, query_fn, expires_in \\ 300)

View Source

Specs

fetch_object(term(), (() -> term()), integer()) :: term()

Fetches the cached object for a particular key. If the cache_key is not present in the cache, it executes the provided query_fn paramter, stores the result in the cache and returns it.

Returns either the cached object or the result of the query_fn parameter.

Examples

iex> Fiat.CacheServer.cache_object("data", :data)
iex> Fiat.CacheServer.fetch_object("data", fn -> :ok end)
:data

iex> Fiat.CacheServer.fetch_object("data", fn -> :ok end)
:ok

Specs

remove_key(term()) :: true

Removes the given key from the cache if it exists.

Returns true even if key does not exist.

Examples

iex> Fiat.CacheServer.cache_object("data", {"code", 2})
iex> Fiat.CacheServer.remove_key("data")
true

iex> Fiat.CacheServer.fetch_object("data")
nil

Specs

stop() :: :ok

Stops the GenServer.

Examples

iex> Fiat.CacheServer.stop()
:ok