diff --git a/lib/cacheman.ex b/lib/cacheman.ex index 3495e8c..d08817e 100644 --- a/lib/cacheman.ex +++ b/lib/cacheman.ex @@ -190,7 +190,7 @@ defmodule Cacheman do def fetch(name, key, put_opts, fallback) do case get(name, key) do {:ok, nil} -> - case fallback.() do + case if is_function(fallback, 0), do: fallback.(), else: fallback.(key) do {:ok, value} -> put(name, key, value, put_opts) {:ok, value} diff --git a/test/cacheman_test.exs b/test/cacheman_test.exs index 4016bca..5f7ff00 100644 --- a/test/cacheman_test.exs +++ b/test/cacheman_test.exs @@ -182,11 +182,17 @@ defmodule CachemanTest do key = "test-#{:rand.uniform(10_000)}" assert {:ok, nil} = Cacheman.get(:broken, key) - assert {:ok, "hello"} = Cacheman.fetch(:broken, key, fn -> {:ok, "hello"} end) + + assert {:ok, "hello"} = + Cacheman.fetch(:broken, key, fn passed_key -> + assert passed_key == key + {:ok, "hello"} + end) + assert {:ok, nil} = Cacheman.get(:broken, key) assert {:ok, "this-is-not-used"} = - Cacheman.fetch(:broken, key, fn -> {:ok, "this-is-not-used"} end) + Cacheman.fetch(:broken, key, fn _ -> {:ok, "this-is-not-used"} end) end test "TTL for keys" do