Skip to content

outlyerapp/ets_cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 

Repository files navigation

ETS cache

ets_cache is a very(!) simple, self-contained memory cache, using only ETS (Erlang Term Storage). You create a cache that has limit of maximum number of elements. When this limit is exceed, the oldest element is removed.

It has the following functions:

new (max_size) -> ets_cache()
destroy (ets_cache) -> true

put (ets_cache, key, value) -> true
put (ets_cache, key, value, timestamp) -> true

get (ets_cache, key) -> {ok, value} | not_found
get (ets_cache, key, timeout) -> {ok, value} | not_found | expired

It is design to perform rapidly:

  • get is constant O(1);
  • put is logarithmic O(log(N));

PS: For performance, a good rule is to use binaries as much as possible for representing strings and large blocks of untyped memory.

Author

Ricardo Tomé Gonçalves tome.wave@gmail.com

Contributions

Ali Yakamercan

How To Use

% Creates a new cache with a maximum of 2 elements in cache
C = ets_cache:new(2),

% Add key/values
ets_cache:put(C, key1, v1),
ets_cache:put(C, key2, v2),
ets_cache:put(C, key3, v3),
ets_cache:put(C, key3, v33), 

% Get values
not_found = ets_cache:get(C, key1),
{ok, v2}  = ets_cache:get(C, key2),
{ok, v33} = ets_cache:get(C, key3),
expired   = ets_cache:get(C, key3, 0),

Test It

$ erlc -DTEST ets_cache.erl; erl -eval "eunit:test(ets_cache)"

TODO

About

Simple ETS Cache implemented as gen_server

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages