-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathsimple_cache.erl
More file actions
91 lines (83 loc) · 3.01 KB
/
simple_cache.erl
File metadata and controls
91 lines (83 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
%%% @doc Main module for simple_cache.
%%%
%%% Copyright 2013 Marcelo Gornstein <marcelog@@gmail.com>
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%% http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%% @end
%%% @copyright Marcelo Gornstein <marcelog@gmail.com>
%%% @author Marcelo Gornstein <marcelog@gmail.com>
%%%
-module(simple_cache).
-author('marcelog@gmail.com').
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Types.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-define(ETS_TID, atom_to_list(?MODULE)).
-define(NAME(N), list_to_atom(?ETS_TID ++ "_" ++ atom_to_list(N))).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Exports.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Public API.
-export([init/1]).
-export([get/4]).
-export([get/2, set/4]).
-export([flush/1, flush/2]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Public API.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% @doc Initializes a cache.
-spec init(atom()) -> ok.
init(CacheName) ->
RealName = ?NAME(CacheName),
RealName = ets:new(RealName, [
named_table, {read_concurrency, true}, public, {write_concurrency, true}
]),
ok.
%% @doc Deletes the keys that match the given ets:matchspec() from the cache.
-spec flush(atom(), term()) -> true.
flush(CacheName, Key) ->
RealName = ?NAME(CacheName),
ets:delete(RealName, Key).
%% @doc Deletes all keys in the given cache.
-spec flush(atom()) -> true.
flush(CacheName) ->
RealName = ?NAME(CacheName),
true = ets:delete_all_objects(RealName).
%% @doc Tries to lookup Key in the cache, and execute the given FunResult
%% 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
[] ->
{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.