Giter Site home page Giter Site logo

etscache's Introduction

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 [email protected]

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.