Giter Site home page Giter Site logo

boshra-pardaz / cache_tab Goto Github PK

View Code? Open in Web Editor NEW

This project forked from processone/cache_tab

0.0 2.0 0.0 87 KB

In-memory cache Erlang / Elixir library

Home Page: http://www.ejabberd.im

License: Apache License 2.0

Makefile 0.20% C 6.05% Erlang 93.75%

cache_tab's Introduction

In-memory cache application for Erlang / Elixir apps

Build Status Coverage Status Hex version

cache_tab application is intended to proxy back-end operations for Key-Value insert, lookup and delete and maintain a cache of those Key-Values in-memory, to save back-end operations.

Operations are intended to be atomic between back-end and cache tables.

The lifetime of the cache object and the max size of the cache can be defined as table parameters to limit the size of the in-memory tables.

Building

cache_tab application can be build as follow:

make

It is a rebar-compatible OTP application. Alternatively, you can build it with rebar:

rebar get-deps compile

Usage

You can start the application with the command:

$ erl -pa ebin -pa deps/*/ebin
Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Eshell V7.1  (abort with ^G)
1> application:start(cache_tab).

Then, you can create a table for a specific type of data to cache. You can create several tables to separate your cached data. The following command will create a table named tab_name:

cache_tab:new(tab_name, [{life_time, Seconds}, {max_size, N}]).

The optional life_time option is used to define cache entry expiration. The optional max_size option is used to limit number of entries to add in cache.

You can insert data into the cache with:

cache_tab:insert(tab_name, <<"key">>, <<"value">>, fun() -> do_something() end).

Cache inserts are designed to be able to act as a proxy to backend insert. That's the purpose of the fun called as last parameter (do_something). Return of the fun is ignored, but if it crashes, cache will not be stored. If value did not change compared to previous insert, the fun will not be called, saving possibly unneeded write operations

You can also lookup data from the cache:

cache_tab:lookup(tab_name, <<"key">>, fun() -> read_value_if_not_cached() end).

The fun in last parameter is used to read and cache the value from your back-end if it is not already in cache.

It is expected to return either {ok, Value} or error.

You can delete entries from back-end and cache with command:

cache_tab:delete(tab_name, <<"key">>, fun() -> do_something() end),

Info command will report about the hits / misses to help you tune your cache size / lifetime parameters:

cache_tab:info(tab_name, Info).

Info parameter can be: size, ratio or all.

Development

Test

Unit test

You can run eunit test with the command:

$ make test

cache_tab's People

Contributors

mremond avatar zinid avatar prefiks avatar alexeyshch avatar badlop avatar weiss avatar jamielinux avatar

Watchers

James Cloos avatar Ali Ghasemi avatar

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.