ETScache is very(!) simple, self-contained memory cache, using only ETS tables in Erlang. You can create a cache with a maximum number of elements in it, and when this limit is exceed, the oldest element is eliminated.
It has the following functions
- new (max_size) -> etscache
- put_new (etscache, key, value) -> ok ; {error, "Already exists!"}
- update (etscache, key, value) -> ok
- get (etscache, key) -> value
It is design to perform rapidly: Get is constant O(1); Put_New is constant O(1); Update is linear with the number os elements in cache O(N).
Ricardo Gonçalves tome.wave@gmail.com
% Create a new cache
C = etscache:new(128), %start takes 1 argument, the maximum number of elememts in cache
% Add a key/value
ok = etscache:put_new(C, key1, "v1"),
% put_new only adds new values
{error,_} = etscache:put_new(C, key1, "cenas"),
% Update an existing value
etscache:update(C, key1, "v11"),
% Get the value for some key
case etscache:get(C, key1) of
not_found -> io:format("Value not Found!~n");
{ok, Value} -> io:format("Value found -> ~p~n", [Value])
end.
Function test gives and example of a possible run, and output the cache in the end, to check its state.
~$ cd "ETScacheFolder"
~$ erlc etscache.erl
~$ erl -noshell -s etscache test -s init stop
- Add proper testing framework. Look at http://etrepum.github.com/erl_testing_2011/
- Look at Judy C implemention http://judy.sourceforge.net/doc/index.html
- Benchmarks!!