mirror of
https://github.com/valitydev/cache.git
synced 2024-11-06 01:45:19 +00:00
minor syntax updates
This commit is contained in:
parent
69e6a3cec6
commit
9a29b8ebf8
@ -28,14 +28,21 @@
|
||||
drop/1,
|
||||
i/1,
|
||||
heap/2,
|
||||
put/3, put/4, put_/3, put_/4,
|
||||
get/2, lookup/2,
|
||||
has/2, ttl/2,
|
||||
remove/2, remove_/2
|
||||
put/3,
|
||||
put/4,
|
||||
put_/3,
|
||||
put_/4,
|
||||
get/2,
|
||||
lookup/2,
|
||||
has/2,
|
||||
ttl/2,
|
||||
remove/2,
|
||||
remove_/2
|
||||
]).
|
||||
|
||||
-type(key() :: any()).
|
||||
-type(entity() :: any()).
|
||||
-type(ttl() :: integer()).
|
||||
|
||||
%%
|
||||
%% start new cache
|
||||
@ -70,16 +77,18 @@ heap(Cache, N) ->
|
||||
%% put entity to cache
|
||||
-spec(put/3 :: (atom(), key(), entity()) -> ok).
|
||||
-spec(put_/3 :: (atom(), key(), entity()) -> ok).
|
||||
-spec(put/4 :: (atom(), key(), entity(), integer()) -> ok).
|
||||
-spec(put_/4 :: (atom(), key(), entity(), integer()) -> ok).
|
||||
-spec(put/4 :: (atom(), key(), entity(), ttl()) -> ok).
|
||||
-spec(put_/4 :: (atom(), key(), entity(), ttl()) -> ok).
|
||||
|
||||
put(Cache, Key, Val) ->
|
||||
gen_server:call(Cache, {put, Key, Val}, ?DEF_IO_TIMEOUT).
|
||||
gen_server:call(Cache, {put, Key, Val}, ?DEF_CACHE_TIMEOUT).
|
||||
|
||||
put(Cache, Key, Val, TTL) ->
|
||||
gen_server:call(Cache, {put, Key, Val, TTL}, ?DEF_IO_TIMEOUT).
|
||||
gen_server:call(Cache, {put, Key, Val, TTL}, ?DEF_CACHE_TIMEOUT).
|
||||
|
||||
put_(Cache, Key, Val) ->
|
||||
gen_server:cast(Cache, {put, Key, Val}).
|
||||
|
||||
put_(Cache, Key, Val, TTL) ->
|
||||
gen_server:cast(Cache, {put, Key, Val, TTL}).
|
||||
|
||||
@ -88,28 +97,28 @@ put_(Cache, Key, Val, TTL) ->
|
||||
-spec(get/2 :: (atom(), key()) -> entity() | undefined).
|
||||
|
||||
get(Cache, Key) ->
|
||||
gen_server:call(Cache, {get, Key}, ?DEF_IO_TIMEOUT).
|
||||
gen_server:call(Cache, {get, Key}, ?DEF_CACHE_TIMEOUT).
|
||||
|
||||
%%
|
||||
%% lookup entity at cache do not prolong ttl
|
||||
-spec(lookup/2 :: (atom(), key()) -> entity() | undefined).
|
||||
|
||||
lookup(Cache, Key) ->
|
||||
gen_server:call(Cache, {lookup, Key}, ?DEF_IO_TIMEOUT).
|
||||
gen_server:call(Cache, {lookup, Key}, ?DEF_CACHE_TIMEOUT).
|
||||
|
||||
%%
|
||||
%% check entity at cache
|
||||
-spec(has/2 :: (atom(), key()) -> true | false).
|
||||
|
||||
has(Cache, Key) ->
|
||||
gen_server:call(Cache, {has, Key}, ?DEF_IO_TIMEOUT).
|
||||
gen_server:call(Cache, {has, Key}, ?DEF_CACHE_TIMEOUT).
|
||||
|
||||
%%
|
||||
%% check entity at cache and return estimated ttl
|
||||
-spec(ttl/2 :: (atom(), key()) -> true | false).
|
||||
-spec(ttl/2 :: (atom(), key()) -> ttl() | false).
|
||||
|
||||
ttl(Cache, Key) ->
|
||||
gen_server:call(Cache, {ttl, Key}, ?DEF_IO_TIMEOUT).
|
||||
gen_server:call(Cache, {ttl, Key}, ?DEF_CACHE_TIMEOUT).
|
||||
|
||||
%%
|
||||
%% remove entity from cache
|
||||
@ -117,7 +126,7 @@ ttl(Cache, Key) ->
|
||||
-spec(remove_/2 :: (atom(), key()) -> ok).
|
||||
|
||||
remove(Cache, Key) ->
|
||||
gen_server:call(Cache, {remove, Key}, ?DEF_IO_TIMEOUT).
|
||||
gen_server:call(Cache, {remove, Key}, ?DEF_CACHE_TIMEOUT).
|
||||
|
||||
remove_(Cache, Key) ->
|
||||
gen_server:cast(Cache, {remove, Key}).
|
||||
@ -126,5 +135,3 @@ remove_(Cache, Key) ->
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -7,7 +7,6 @@
|
||||
-define(DEBUG(Str, Args), ok).
|
||||
-endif.
|
||||
|
||||
|
||||
%% default cache eviction policy
|
||||
-define(DEF_CACHE_POLICY, lru).
|
||||
|
||||
@ -18,4 +17,5 @@
|
||||
%% default cache house keeping frequency
|
||||
-define(DEF_CACHE_QUOTA, 5).
|
||||
|
||||
-define(DEF_IO_TIMEOUT,60000).
|
||||
%% default cache i/o timeout
|
||||
-define(DEF_CACHE_TIMEOUT, 60000).
|
@ -257,7 +257,7 @@ cache_get(Key, #cache{policy=mru}=S) ->
|
||||
undefined ->
|
||||
cache_util:stats(S#cache.stats, {cache, S#cache.name, miss}),
|
||||
undefined;
|
||||
{Heap, Val} ->
|
||||
{_Heap, Val} ->
|
||||
?DEBUG("cache ~p: get ~p at cell ~p~n", [S#cache.name, Key, Heap#heap.id]),
|
||||
cache_util:stats(S#cache.stats, {cache, S#cache.name, hit}),
|
||||
Val
|
||||
@ -286,7 +286,7 @@ cache_lookup(Key, #cache{}=S) ->
|
||||
undefined ->
|
||||
cache_util:stats(S#cache.stats, {cache, S#cache.name, miss}),
|
||||
undefined;
|
||||
{Heap, Val} ->
|
||||
{_Heap, Val} ->
|
||||
?DEBUG("cache ~p: get ~p at cell ~p~n", [S#cache.name, Key, Heap#heap.id]),
|
||||
cache_util:stats(S#cache.stats, {cache, S#cache.name, hit}),
|
||||
Val
|
||||
@ -296,7 +296,7 @@ cache_has(Key, #cache{}=S) ->
|
||||
case heap_has(Key, S#cache.heap) of
|
||||
false ->
|
||||
false;
|
||||
Heap ->
|
||||
_Heap ->
|
||||
?DEBUG("cache ~p: has ~p at cell ~p~n", [S#cache.name, Key, Heap#heap.id]),
|
||||
true
|
||||
end.
|
||||
|
Loading…
Reference in New Issue
Block a user