From 367c8d64516f9a2baf8986b79e8cb7e1a0b2cb43 Mon Sep 17 00:00:00 2001 From: VeljkoMaksimovic Date: Tue, 11 Oct 2022 10:26:58 +0200 Subject: [PATCH 1/2] Pass key to fallback function --- lib/cacheman.ex | 2 +- test/cacheman_test.exs | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/cacheman.ex b/lib/cacheman.ex index 3495e8c..402ae36 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 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..ea45d5a 100644 --- a/test/cacheman_test.exs +++ b/test/cacheman_test.exs @@ -88,7 +88,11 @@ defmodule CachemanTest do assert {:ok, nil} = Cacheman.get(:good, key) # if we fetch on empty value, the fallback function is executed - assert {:ok, "hello"} = Cacheman.fetch(:good, key, fn -> {:ok, "hello"} end) + assert {:ok, "hello"} = + Cacheman.fetch(:good, key, fn passed_key -> + assert passed_key == key + {:ok, "hello"} + end) # the value of the fallback is saved into the cache assert {:ok, "hello"} = Cacheman.get(:good, key) @@ -182,11 +186,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 From 6c0737e44bc93447f223ad00bdd9ae9637f3ec82 Mon Sep 17 00:00:00 2001 From: VeljkoMaksimovic Date: Tue, 11 Oct 2022 13:27:21 +0200 Subject: [PATCH 2/2] Check fallback arity --- lib/cacheman.ex | 2 +- test/cacheman_test.exs | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/cacheman.ex b/lib/cacheman.ex index 402ae36..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.(key) 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 ea45d5a..5f7ff00 100644 --- a/test/cacheman_test.exs +++ b/test/cacheman_test.exs @@ -88,11 +88,7 @@ defmodule CachemanTest do assert {:ok, nil} = Cacheman.get(:good, key) # if we fetch on empty value, the fallback function is executed - assert {:ok, "hello"} = - Cacheman.fetch(:good, key, fn passed_key -> - assert passed_key == key - {:ok, "hello"} - end) + assert {:ok, "hello"} = Cacheman.fetch(:good, key, fn -> {:ok, "hello"} end) # the value of the fallback is saved into the cache assert {:ok, "hello"} = Cacheman.get(:good, key)