Skip to content

Commit 0d609f8

Browse files
Clarify docs and examples for fetch/4
1 parent 9c52af4 commit 0d609f8

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

lib/cachex.ex

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -562,39 +562,28 @@ defmodule Cachex do
562562
do: Router.route(cache, {:export, [options]})
563563

564564
@doc """
565-
Fetches an entry from a cache, generating a value on cache miss.
565+
Fetches an entry from a cache the same way as `get/3`, but calls `fallback`
566+
to lazily generate a value on cache miss. Conceptually similar to a
567+
read-through cache.
566568
567-
If the entry requested is found in the cache, this function will
568-
operate in the same way as `get/3`. If the entry is not contained
569-
in the cache, the provided fallback function will be executed.
569+
`fallback` should return a Tuple like `{:commit, value}` or
570+
`{:ignore, value}`. Passing `:ignore` instructs Cachex not to store the value.
571+
Any other value returned will assume `:commit`.
570572
571-
A fallback function is a function used to lazily generate a value
572-
to place inside a cache on miss. Consider it a way to achieve the
573-
ability to create a read-through cache.
573+
You may provide a third element in a `:commit` Tuple to passthrough options
574+
to `put/4`, for example `expire: ...`
574575
575-
A fallback function should return a Tuple consisting of a `:commit`
576-
or `:ignore` tag and a value. If the Tuple is tagged `:commit` the
577-
value will be placed into the cache and then returned. If tagged
578-
`:ignore` the value will be returned without being written to the
579-
cache. If you return a value which does not fit this structure, it
580-
will be assumed that you are committing the value.
576+
`fallback` optionally takes argument `key`.
581577
582-
As of Cachex v3.6, you can also provide a third element in a `:commit`
583-
Tuple, to allow passthrough of options from within your fallback. The
584-
options supported in this list match the options you can provide to a
585-
call of `Cachex.put/4`. An example is the `:expire` option to set an
586-
expiration from directly inside your fallback.
587-
588-
If a fallback function has an arity of 1, the requested entry key
589-
will be passed through to allow for contextual computation. If a
590-
function has an arity of 0, it will be executed without arguments.
578+
Multiple calls to `fetch/4` with the same key while the `fallback` is running
579+
will wait on the lazy evaluation to complete. Multiple instances of
580+
`fallback` will never run concurrently for a given key.
591581
592582
## Examples
593583
594-
iex> Cachex.put(:my_cache, "key", "value")
595-
iex> Cachex.fetch(:my_cache, "key", fn(key) ->
596-
...> { :commit, String.reverse(key) }
597-
...> end)
584+
iex> Cachex.fetch(:my_cache, "key", fn -> "value" end)
585+
{ :commit, "value" }
586+
iex> Cachex.fetch(:my_cache, "key", fn -> "value" end)
598587
{ :ok, "value" }
599588
600589
iex> Cachex.fetch(:my_cache, "missing_key", fn(key) ->
@@ -607,10 +596,10 @@ defmodule Cachex do
607596
...> end)
608597
{ :commit, "yek_gnissim" }
609598
610-
iex> Cachex.fetch(:my_cache, "missing_key_expires", fn(key) ->
599+
iex> Cachex.fetch(:my_cache, "with_options", fn(key) ->
611600
...> { :commit, String.reverse(key), expire: :timer.seconds(60) }
612601
...> end)
613-
{ :commit, "seripxe_yek_gnissim" }
602+
{ :commit, "snoitpo_htiw" }
614603
615604
"""
616605
@spec fetch(Cachex.t(), any, function(), Keyword.t()) ::

0 commit comments

Comments
 (0)