Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

cache results in ETS #1

Merged
merged 1 commit into from

2 participants

@d0rc

This will make it possible to call cfgsrv:get functions from frequent processes.

@chvanikoff chvanikoff merged commit 3543390 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 13, 2013
  1. @d0rc

    cache results in ETS

    d0rc authored
This page is out of date. Refresh to see the latest.
Showing with 10 additions and 2 deletions.
  1. +4 −1 src/cfgsrv.erl
  2. +6 −1 src/cfgsrv_srv.erl
View
5 src/cfgsrv.erl
@@ -34,7 +34,10 @@ get(Path, Key) ->
get(Path, Key, Default) ->
- gen_server:call(?SERVER, {get, Path, Key, Default}).
+ case ets:lookup(cfgsrv, {Path, Key, Default}) of
+ [] -> gen_server:call(?SERVER, {get, Path, Key, Default});
+ [{_, Val}] -> Val
+ end.
get_multiple(Path, Keys) ->
View
7 src/cfgsrv_srv.erl
@@ -42,11 +42,14 @@ start_link(Path) ->
%% ===================================================================
init(Path) ->
+ ets:new(cfgsrv, [set, named_table, {read_concurrency, true}]),
Configs = load_configs(Path),
{ok, #state{configs = Configs, path = Path}}.
handle_call({get, Path, Key, Default}, _From, State = #state{configs = Configs}) ->
+ Data = from_configs(Configs, Path, Key, Default),
+ ets:insert(cfgsrv, {{Path,Key,Default}, Data}),
{reply, from_configs(Configs, Path, Key, Default), State};
handle_call(test, _From, State) ->
@@ -99,7 +102,9 @@ load_configs(Path) ->
load_configs(filelib:wildcard(Path ++ "/*.config"), []).
-load_configs([], Acc) -> Acc;
+load_configs([], Acc) ->
+ ets:delete_all_objects(cfgsrv),
+ Acc;
load_configs([File | Files], Acc) ->
case file:consult(File) of
{ok, [Config]} -> load_configs(Files, [{list_to_atom(filename:basename(File, ".config")), Config} | Acc]);
Something went wrong with that request. Please try again.