Packages
charon
4.0.0
4.3.0
4.3.0-beta1
4.2.0
4.2.0-beta3
4.2.0-beta2
4.2.0-beta1
4.1.0
4.0.1
4.0.0
4.0.0-beta3
4.0.0-beta2
4.0.0-beta1
3.4.1
3.4.0
3.3.0
3.2.0
3.1.1
3.1.0
3.0.3
3.0.2
3.0.1
3.0.0
3.0.0-beta1
2.8.0
2.8.0-beta1
2.7.3
2.7.2
2.7.1
2.7.0
2.6.1
2.6.0
2.5.0
2.4.0
2.3.0
2.2.0
2.2.0-beta
2.1.3
2.1.2
2.1.2-beta
2.1.1
2.1.0
2.0.0
1.3.4
1.3.3
1.3.2-beta
1.3.1-beta
1.3.0-beta
1.2.0-beta
1.1.0-beta
1.0.1-beta
1.0.0-beta1
0.0.4-alpha
0.0.3-alpha
0.0.2-alpha
0.0.1-alpha
Authentication & sessions for API's.
Current section
Files
Jump to
Current section
Files
lib/charon/utils/persistent_term_cache.ex
defmodule Charon.Utils.PersistentTermCache do
@moduledoc since: "4.0.0"
@moduledoc """
Cache things using `m::persistent_term`. Be careful when using this; `m::persistent_term` is only suitable for very read-heavy storage, to the point the cached item should probably be write-once-read-often.
"""
@doc """
Get the item stored under `key` from `m::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"
"""
@spec get_or_create(term(), (-> term())) :: term()
def get_or_create(key, create) do
:persistent_term.get(key, nil) || create.() |> tap(&:persistent_term.put(key, &1))
end
end