Charon.Utils.PersistentTermCache (Charon v4.2.0)

View Source

Cache things using :persistent_term. Be careful when using this; :persistent_term is only suitable for very read-heavy storage, to the point the cached item should probably be write-once-read-often.

Summary

Functions

Get the item stored under key from :persistent_term. If it does not exist, create it using create/0 and cache it under key.

Functions

get_or_create(key, create)

(since 4.0.0)
@spec get_or_create(term(), (-> term())) :: term()

Get the item stored under key from :persistent_term. If it does not exist, create it using create/0 and cache it under key.

Doctests

iex> create = fn -> "I'm cached" end
iex> get_or_create(__MODULE__, create)
"I'm cached"
iex> create = fn -> "I'm never created" end
iex> get_or_create(__MODULE__, create)
"I'm cached"