View Source LastCrusader.Cache.MemoryTokenStore (Last Crusader v0.11.0)

Simple ETS-based cache.

https://gist.github.com/raorao/a4bb34726af2e3fa071adfa504505e1d

Summary

Functions

Asynchronous call to cache a value at the provided key. Any key that can be used with ETS can be used, and will be evaluated using ==.

Returns a specification to start this module under a supervisor.

Asynchronous clears all values in the cache.

Callback implementation for GenServer.init/1.

Sychronously reads the cache for the provided key. If no value is found, returns :not_found .

Sychronously reads the cache for the provided key. If no value is found, invokes default_fn and caches the result. Note: in order to prevent congestion of the RequestCache process, default_fn is invoked in the context of the caller process.

Starts a RequestCache process linked to the current process. See GenServer.start_link/2 for details.

Types

cache_key()

@type cache_key() :: any()

cache_value()

@type cache_value() :: any()

t()

@type t() :: %{ttl: integer(), invalidators: %{}}

Functions

cache(key, val)

@spec cache(cache_key(), cache_value()) :: :ok

Asynchronous call to cache a value at the provided key. Any key that can be used with ETS can be used, and will be evaluated using ==.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clear()

@spec clear() :: :ok

Asynchronous clears all values in the cache.

init(ttl_seconds)

@spec init(integer()) :: {:ok, t()}

Callback implementation for GenServer.init/1.

read(key)

@spec read(cache_key()) :: cache_value() | :not_found

Sychronously reads the cache for the provided key. If no value is found, returns :not_found .

read_or_cache_default(key, default_fn)

Sychronously reads the cache for the provided key. If no value is found, invokes default_fn and caches the result. Note: in order to prevent congestion of the RequestCache process, default_fn is invoked in the context of the caller process.

start_link(ttl_seconds \\ 21600)

@spec start_link(integer()) :: GenServer.on_start()

Starts a RequestCache process linked to the current process. See GenServer.start_link/2 for details.

By default, every item in the cache lives for 6 hours.