tgrk / simple_cache

Simple Memory-based Erlang cache library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CircleCI Hex pm codecov.io

simple_cache

Simple Memory-based Erlang cache service using ETS.

This is a second fork of library originally written by Andrew Stanton.

This particular version has a few features:

  • provides async and sync acesss API (check for sync_ prefix).
  • API documentation
  • type specs
  • unit tests
  • ops helpers (check for ops_ prefix)
  • conditional sets (credit to Christian Lundgren)

Usage

Include it into rebar.config for rebar3:

{simple_cache, "1.1.3"}

Include it into rebar.config:

{simple_cache, "",
  {git, "git@github.com:tgrk/simple_cache.git", {tag, "1.1.2"}}}

Start OTP application:

_ = application:ensure_all_started(simple_cache).

Insert/update value (optional expiration in ms):

ok = simple_cache:set(<<"foo">>, <<"bar">>),
ok = simple_cache:set(<<"foo">>, <<"bar">>, 5000),
ok = simple_cache:sync_set(<<"foo">>, <<"bar">>),
ok = simple_cache:sync_set(<<"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 (optional default value):

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

Remove cached values all or by key:

ok = simple_cache:flush().
ok = simple_cache:sync_flush().
ok = simple_cache:flush(<<"foo">>).

Operations helpers:

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

For more information about usage refer to tests.

Credits

About

Simple Memory-based Erlang cache library

License:MIT License


Languages

Language:Erlang 100.0%