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

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

Functions

@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 ==.

Returns a specification to start this module under a supervisor.

See Supervisor.

@spec clear() :: :ok

Asynchronous clears all values in the cache.

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

Callback implementation for GenServer.init/1.

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

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

Link to this function

read_or_cache_default(key, default_fn)

View Source

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.

Link to this function

start_link(ttl_seconds \\ 21600)

View Source
@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.