View Source Finitomata.Cache (Finitomata v0.26.4)
The self-curing cache based on Finitomata
implementation.
This implementation should not be chosen for typical caching scenarios,
use cachex
and/or con_cache
instead.
The use-case for this implementation would be somewhat like a self-updated local replica
of the remote data. Unlike typical cache implementations, this one might keep the cached
values up-to-date, configured by ttl:
argument. Bsaed on processes (backed by Finitomata
,)
this implementation updates itself periodically, making the value retrieval almost instant.
Consider a remote service supplying currency exchange rates by polling. One might instruct
Finitomata.Cache
to retrieve values periodically (say, once per a minute,) and then
the consumers of this cache would be able to retrieve the up-to-date values locally without
a penalty of getting a value after a long period (cache miss.)
First of all, the Finitomata.Cache
implementation should be added to a supervision tree
{Finitomata.Cache, [
[id: MyCache, ttl: 60_000, live?: true, type: Infinitomata, getter: &MyMod.getter/1]]}
Once the supervisor is started, the values might be retrieven as
Finitomata.Cache.get(MyCache, :my_key_1, live?: false) # use default getter
Finitomata.Cache.get(MyCache, :my_key, getter: fn _ -> ExtService.get(:my_key) end)
Summary
Functions
Erases the cache assotiated with key
.
Retrieves the value either cached or via getter/1
anonymous function and caches it.
Supervision tree embedder.
Functions
@spec child_spec( id: term(), type: term(), ttl: pos_integer(), live?: boolean(), getter: (term() -> term()) | term() ) :: Supervisor.child_spec()
@spec erase(id :: Finitomata.id(), key :: any()) :: :ok
Erases the cache assotiated with key
.
@spec get( id :: Finitomata.id(), key :: key, opts :: [ getter: (key -> value), live?: boolean(), reset: boolean(), ttl: pos_integer() ] ) :: {DateTime.t(), value} | {:instant, value} | :error when key: any(), value: any()
Retrieves the value either cached or via getter/1
anonymous function and caches it.
Spawns the respective Finitomata instance if needed.
@spec start_link( id: term(), type: term(), ttl: pos_integer(), live?: boolean(), getter: (term() -> term()) | term() ) :: Supervisor.on_start()
Supervision tree embedder.
Options to Finitomata.Cache.start_link/1
:id
(term/0
) - Required. The uniqueID
of this Finitomata “branch,” whennil
theFinitomata.Cache
value would be used:type
- The actualFinitomata.Supervisor
implementation (typically,Finitomata
orInfinitomata
) The default value isInfinitomata
.:ttl
(pos_integer/0
) - Required. The default time-to-live value in seconds, after which the value would be either revalidated or discarded:live?
(boolean/0
) - Whentrue
, the value will be automatically renewed upon expiration (and discarded otherwise) The default value isfalse
.:getter
- The shared for all instances getter returning a value based on the name of the instance, used as a key The default value isnil
.