Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/stash.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ defmodule Stash do
:ets.insert(cache, { key, value })
end

@spec getsert(atom, any, () -> any | any) :: any
deft getsert(cache, key, function) when is_function(function, 0) do
get(cache, key) || set(cache, key, function.())
end

deft getsert(cache, key, value) do
get(cache, key) || set(cache, key, value)
end

@doc """
Increments a key directly in the cache by `count`. If the key does not exist
it is set to `initial` before **then** being incremented.
Expand Down