Skip to content

Simple Memory-based Erlang cache service

License

Notifications You must be signed in to change notification settings

GameAnalytics/simple_cache

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simple_cache

Simple Memory-based Erlang cache service using ETS. It provides async and sync acesss API (check for sync_ prefix).

Usage

Include it into rebar.config:

{simple_cache, "",
  {git, "git@github.com:GameAnalytics/simple_cache.git", {branch, "master"}}}

Start OTP application:

ok = application:start(simple_cache).

Single cache

If you plan to use a single instance of simple_cache,

Insert/update value:

ok = simple_cache:set(<<"foo">>, <<"bar">>),       % Returns immediately
ok = simple_cache:sync_set(<<"foo">>, <<"bar">>),  % Blocks until cache is set

Insert/update value with expiration in ms:

ok = simple_cache:setex(<<"foo">>, <<"bar">>, 5000),
ok = simple_cache:sync_setex(<<"foo">>, <<"bar">>, infinity),

Insert/update value based on predicate result:

PredFun = fun (<<"bar">>) -> false end,
{ok, false} = simple_cache:cond_set(<<"foo">>, <<"baz">>, PredFun, infinity).

Get value by key:

{ok, <<"bar">>} = simple_cache:get(<<"foo">>),

Get value by key with default value given:

{ok, <<"bar">>} = simple_cache:get_def(<<"foo">>, <<"default">>),

Remove cached values all or by key:

ok = simple_cache:clear_all().
ok = simple_cache:sync_clear_all().
ok = simple_cache:clear(<<"foo">>).
ok = simple_cache:sync_clear(<<"foo">>).

Operations helpers:

simple_cache:ops_info().
simple_cache:ops_list().

For more information about usage refer to tests.

Multiple caches

If you plan to use a many instances of simple_cache,

Insert/update value:

Name = cache_name, % Has to be an atom!
ok = simple_cache:set(Name, <<"foo">>, <<"bar">>),
ok = simple_cache:sync_set(Name, <<"foo">>, <<"bar">>),

Insert/update value with expiration in ms:

ok = simple_cache:set(Name, <<"foo">>, <<"bar">>, 5000),
ok = simple_cache:sync_set(Name, <<"foo">>, <<"bar">>, infinity),

Insert/update value based on predicate result:

PredFun = fun (<<"bar">>) -> false end,
{ok, false} = simple_cache:cond_set(Name, <<"foo">>, <<"baz">>, PredFun, infinity).

Get value by key (optional default value):

{ok, <<"bar">>} = simple_cache:lookup(Name, <<"foo">>),
{ok, <<"bar">>} = simple_cache:lookup(Name, <<"foo">>, <<"default">>),

Remove cached values all or by key:

ok = simple_cache:clear(Name).
ok = simple_cache:sync_clear(Name).
ok = simple_cache:clear(Name, <<"foo">>).

Operations helpers:

simple_cache:ops_info(Name).
simple_cache:ops_list(Name).
simple_cache:list().          % List of all registered caches

For more information about usage refer to tests.

Credits

About

Simple Memory-based Erlang cache service

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Erlang 86.9%
  • Makefile 13.1%