From 43be333408c56ef84329860855a9e7c8a49a2f7e Mon Sep 17 00:00:00 2001 From: Kirilll Zaborsky Date: Fri, 8 Aug 2014 16:21:33 +0400 Subject: [PATCH] Simple set/get API added --- src/simple_cache.erl | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/simple_cache.erl b/src/simple_cache.erl index e545379..09c0ed7 100644 --- a/src/simple_cache.erl +++ b/src/simple_cache.erl @@ -32,6 +32,7 @@ %%% Public API. -export([init/1]). -export([get/4]). +-export([get/2, set/4]). -export([flush/1, flush/2]). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -62,16 +63,29 @@ flush(CacheName) -> %% on a miss. -spec get(atom(), infinity|pos_integer(), term(), function()) -> term(). get(CacheName, LifeTime, Key, FunResult) -> + case get(CacheName, Key) of + {ok, V} -> V; + {error, not_found} -> + V = FunResult(), + set(CacheName, LifeTime, Key, V), + V + end. + +-spec get(atom(), term()) -> {ok, term()} | {error, not_found}. +get(CacheName, Key) -> RealName = ?NAME(CacheName), case ets:lookup(RealName, Key) of [] -> - % Not found, create it. - V = FunResult(), - ets:insert(RealName, {Key, V}), - erlang:send_after( - LifeTime, simple_cache_expirer, {expire, CacheName, Key} - ), - V; - [{Key, R}] -> R % Found, return the value. + {error, not_found}; + [{Key, R}] -> + {ok, R} % Found, return the value. end. +-spec set(atom(), infinity|pos_integer(), term(), term()) -> ok. +set(CacheName, LifeTime, Key, Value) -> + RealName = ?NAME(CacheName), + ets:insert(RealName, {Key, Value}), + erlang:send_after( + LifeTime, simple_cache_expirer, {expire, CacheName, Key} + ), + ok.