Skip to content

Commit

Permalink
Give the packet cache a configurable TTL.
Browse files Browse the repository at this point in the history
  • Loading branch information
aeden committed Aug 20, 2012
1 parent 49ba860 commit af80a99
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ebin/erldns.app
@@ -1,6 +1,6 @@
{application,erldns,
[{description,"Erlang Authoritative DNS Server"},
{vsn,"63d849e"},
{vsn,"49ba860"},
{modules,[erldns,erldns_app,erldns_fake_responder,
erldns_mysql_responder,erldns_packet_cache,
erldns_records,erldns_server,erldns_sup]},
Expand Down
10 changes: 6 additions & 4 deletions src/erldns_packet_cache.erl
Expand Up @@ -16,7 +16,7 @@

-define(SERVER, ?MODULE).

-record(state, {}).
-record(state, {ttl}).

%% Public API
start_link() ->
Expand All @@ -28,9 +28,11 @@ put(Question, Answer) ->
gen_server:call(?SERVER, {set_packet, [Question, Answer]}).

%% Gen server hooks
init(_Args) ->
init([]) ->
init([20]);
init([TTL]) ->
ets:new(packet_cache, [set, named_table]),
{ok, #state{}}.
{ok, #state{ttl = TTL}}.
handle_call({get_packet, Question}, _From, State) ->
case ets:lookup(packet_cache, Question) of
[{Question, {Answer, ExpiresAt}}] ->
Expand All @@ -47,7 +49,7 @@ handle_call({get_packet, Question}, _From, State) ->
end;
handle_call({set_packet, [Question, Answer]}, _From, State) ->
{_,T,_} = erlang:now(),
ets:insert(packet_cache, {Question, {Answer, T + 60}}),
ets:insert(packet_cache, {Question, {Answer, T + State#state.ttl}}),
{reply, ok, State}.
handle_cast(_Message, State) ->
{noreply, State}.
Expand Down

0 comments on commit af80a99

Please sign in to comment.