• Stars
    star
    136
  • Rank 258,457 (Top 6 %)
  • Language
    Erlang
  • License
    Apache License 2.0
  • Created over 11 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Erlang in-memory cache

Cache

Library implements segmented in-memory cache.

Build Status Coverage Status Hex.pm Hex Downloads

Inspiration

Cache uses N disposable ETS tables instead of single one. The cache applies eviction and quota policies at segment level. The oldest ETS table is destroyed and new one is created when quota or TTL criteria are exceeded. This approach outperforms the traditional timestamp indexing techniques.

The write operation always uses youngest segment. The read operation lookup key from youngest to oldest table until it is found same time key is moved to youngest segment to prolong TTL. If none of ETS table contains key then cache-miss occurs.

The downside is inability to assign precise TTL per single cache entry. TTL is always approximated to nearest segment. (e.g. cache with 60 sec TTL and 10 segments has 6 sec accuracy on TTL)

Key features

  • Key/value interface to read/write cached entities
  • Naive transform interface (accumulators, lists, binaries) to modify entities in-place
  • Check-and-store of put behavior
  • Supports asynchronous I/O to cache buckets
  • Sharding of cache bucket

Getting started

The latest version of the library is available at its master branch. All development, including new features and bug fixes, take place on the master branch using forking and pull requests as described in contribution guidelines.

The stable library release is available via hex packages, add the library as dependency to rebar.config

{deps, [
   cache
]}.

Usage

The library exposes public primary interface through exports of module cache.erl. An experimental features are available through interface extensions. Please note that further releases of library would promote experimental features to primary interface.

Build library and run the development console to evaluate key features

make && make run

spawn and configure

Use cache:start_link(...) to spawn an new cache instance. It supports a configuration using property lists:

  • type - a type of ETS table to used as segment, default is set. See ets:new/2 documentation for supported values.
  • n - number of cache segments, default is 10.
  • ttl - time to live of cached items in seconds, default is 600 seconds. It is recommended to use value multiple to n. The oldest cache segment is evicted every ttl / n seconds.
  • size - number of items to store in cache. It is recommended to use value multiple to n, each cache segment takes about size / n items. The size policy is applied only to youngest segment.
  • memory - rough number of bytes available for cache items. Each cache segment is allowed to take about memory / n bytes. Note: policy enforcement accounts Erlang word size.
  • policy - cache eviction policy, default is lru, supported values are Least Recently Used lru, Most Recently Used mru.
  • check - time in seconds to enforce cache policy. The default behavior enforces policy every ttl / n seconds. This timeout helps to optimize size/memory policy enforcement at high throughput system. The timeout is disabled by default.
  • stats - cache statistics handler either function/2 or {M, F} struct.
  • heir - the ownership of ETS segment is given away to the process during segment eviction. See ets:give_away/3 for details.

key/value interface

The library implements traditional key/value interface through put, get and remove functions. The function get prolongs ttl of the item, use lookup to keep ttl untouched.

application:start(cache).
{ok, _} = cache:start_link(my_cache, [{n, 10}, {ttl, 60}]).

ok  = cache:put(my_cache, <<"my key">>, <<"my value">>).
Val = cache:get(my_cache, <<"my key">>).

asynchronous i/o

The library provides synchronous and asynchronous implementation of same functions. The asynchronous variant of function is annotated with _ suffix. E.g. get(...) is a synchronous cache lookup operation (the process is blocked until cache returns); get_(...) is an asynchronous variant that delivers result of execution to mailbox.

application:start(cache).
{ok, _} = cache:start_link(my_cache, [{n, 10}, {ttl, 60}]).

Ref = cache:get_(my_cache, <<"my key">>).
receive {Ref, Val} -> Val end.

transform element

The library allows to read-and-modify (modify in-place) cached element. You can apply any function over cached elements and returns the result of the function. The apply acts a transformer with three possible outcomes:

  • undefined (e.g. fun(_) -> undefined end) - no action is taken, old cache value remains;
  • unchanged value (e.g. fun(X) -> X end) - no action is taken, old cache value remains;
  • new value (e.g. fun(X) -> <<"x", X/binary>> end) - the value in cache is replaced with the result of the function.
application:start(cache).
{ok, _} = cache:start_link(my_cache, [{n, 10}, {ttl, 60}]).

cache:put(my_cache, <<"my key">>, <<"x">>).
cache:apply(my_cache, <<"my key">>, fun(X) -> <<"x", X/binary>> end).
cache:get(my_cache, <<"my key">>).

The library implement helper functions to transform elements with append or prepend.

application:start(cache).
{ok, _} = cache:start_link(my_cache, [{n, 10}, {ttl, 60}]).

cache:put(my_cache, <<"my key">>, <<"b">>).
cache:append(my_cache, <<"my key">>, <<"c">>).
cache:prepend(my_cache, <<"my key">>, <<"a">>).
cache:get(my_cache, <<"my key">>).

accumulator

application:start(cache).
{ok, _} = cache:start_link(my_cache, [{n, 10}, {ttl, 60}]).

cache:acc(my_cache, <<"my key">>, 1).
cache:acc(my_cache, <<"my key">>, 1).
cache:acc(my_cache, <<"my key">>, 1).

check-and-store

The library implements the check-and-store semantic for put operations:

  • add store key/val only if cache does not already hold data for this key
  • replace store key/val only if cache does hold data for this key

configuration via Erlang sys.config

The cache instances are configurable via sys.config. These cache instances are supervised by application supervisor.

{cache, [
   {my_cache, [{n, 10}, {ttl, 60}]}
]}

distributed environment

The cache application uses standard Erlang distribution model. Please node that Erlang distribution uses single tcp/ip connection for message passing between nodes. Therefore, frequent read/write of large entries might impact on overall Erlang performance.

The global cache instance is visible to all Erlang nodes in the cluster.

%% at [email protected]
{ok, _} = cache:start_link({global, my_cache}, [{n, 10}, {ttl, 60}]).
Val = cache:get({global, my_cache}, <<"my key">>).

%% at [email protected]
ok  = cache:put({global, my_cache}, <<"my key">>, <<"my value">>).
Val = cache:get({global, my_cache}, <<"my key">>).

The local cache instance is accessible for any Erlang nodes in the cluster.

%% [email protected]
{ok, _} = cache:start_link(my_cache, [{n, 10}, {ttl, 60}]).
Val = cache:get(my_cache, <<"my key">>).

%% [email protected]
ok  = cache:put({my_cache, '[email protected]'}, <<"my key">>, <<"my value">>).
Val = cache:get({my_cache, '[email protected]'}, <<"my key">>).

sharding

Module cache_shards provides simple sharding on top of cache. It uses simple hash(Key) rem NumShards approach, and keeps NumShards in application environment. This feature is still experimental, its interface is a subject to change in further releases.

{ok, _} = cache_shards:start_link(my_cache, 8, [{n, 10}, {ttl, 60}]).
ok = cache_shards:put(my_cache, key1, "Hello").
{ok,"Hello"} = cache_shards:get(my_cache, key1).

sharded_cache uses only small subset of cache API. But you can get shard name for your key and then use cache directly.

{ok, Shard} = cache_shards:get_shard(my_cache, key1)
{ok, my_cache_2}
cache:lookup(Shard, key1).
"Hello"

How to Contribute

The library is Apache 2.0 licensed and accepts contributions via GitHub pull requests.

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

The development requires Erlang/OTP version 19.0 or later and essential build tools.

commit message

The commit message helps us to write a good release note, speed-up review process. The message should address two question what changed and why. The project follows the template defined by chapter Contributing to a Project of Git book.

Short (50 chars or less) summary of changes

More detailed explanatory text, if necessary. Wrap it to about 72 characters or so. In some contexts, the first line is treated as the subject of an email and the rest of the text as the body. The blank line separating the summary from the body is critical (unless you omit the body entirely); tools like rebase can get confused if you run the two together.

Further paragraphs come after blank lines.

Bullet points are okay, too

Typically a hyphen or asterisk is used for the bullet, preceded by a single space, with blank lines in between, but conventions vary here

Bugs

If you detect a bug, please bring it to our attention via GitHub issues. Please make your report detailed and accurate so that we can identify and replicate the issues you experience:

  • specify the configuration of your environment, including which operating system you're using and the versions of your runtime environments
  • attach logs, screen shots and/or exceptions if possible
  • briefly summarize the steps you took to resolve or reproduce the problem

Changelog

  • 2.3.0 - sharding of cache bucket (single node only)
  • 2.0.0 - various changes on asynchronous api, not compatible with version 1.x
  • 1.0.1 - production release

Contributors

License

Copyright 2014 Dmitry Kolesnikov

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

More Repositories

1

datalog

simplified query engine based on logic programming paradigm
Erlang
134
star
2

datum

pure functional and generic programming for Erlang
Erlang
119
star
3

aws-cdk-pure

Purely Functional and high-order cloud components with AWS CDK
TypeScript
89
star
4

esq

simple persistent queues for Erlang
Erlang
54
star
5

typhoon

distributed system stress and load testing tool
Erlang
43
star
6

golem

pure functional and generic programming for Go
Go
40
star
7

serverless

Serverless Erlang runtime for AWS Lambda Service
Erlang
29
star
8

csv

csv parser, optimized for performance
Makefile
22
star
9

dynamo

Generic Golang Key/Value trait for AWS storage services
Go
17
star
10

pts

The library provides hashtable-like interface to manipulate data distributed across Erlang processes.
Erlang
15
star
11

hash

collection of hash functions for Erlang applications
Erlang
12
star
12

feta

fogfish erlang toolkit archive
Erlang
10
star
13

guid

K-ordered unique identifiers in lock-free and decentralised manner for Golang applications
Go
10
star
14

makefile

Erlang workflow
Makefile
10
star
15

oauth2

oauth2 authorization server
Erlang
9
star
16

node-lambda-typescript-template

The bare minimum for a TypeScript app running on Amazon Lambda.
TypeScript
8
star
17

ek

Erlang clustering
Erlang
8
star
18

nebula

Erlang nodes discovery agent
Erlang
8
star
19

ddb

AWS Generic Storage Drivers
Erlang
8
star
20

relog

Redis datalog support
Erlang
8
star
21

m_http

A class of Erlang monads which can do http requests
Erlang
7
star
22

hyperion

Erlang Vanilla Node
Erlang
6
star
23

uid

erlang fault tolerant service to generate unique identities
Erlang
6
star
24

svg

Erlang
5
star
25

gurl

แต๐Ÿ†„๐Ÿ†๐Ÿ…ป is a combinator library for network I/O
Go
5
star
26

blueprint-serverless-golang

AWS CDK template for serverless Golang
Go
5
star
27

semantic

Semantic Web ToolKit for Erlang applications
Erlang
4
star
28

socat

Command line utility to cat files via network socket
Erlang
4
star
29

ring

consistent hashing data structure
Go
4
star
30

schemaorg

Go types of schema.org ontology
Go
4
star
31

elata

Erlang LATency Agent: software solution to gather telemetry of software components (written on erlang)
Shell
4
star
32

certbot-on-aws

Serverless integration with https://letsencrypt.org for microservices hosted on AWS
Makefile
4
star
33

swirl

swirl is the Erlang port of whiskers.js template library.
Erlang
4
star
34

streamfs

Append-only file system access with stream abstraction
Erlang
3
star
35

esh

Erlang to Shell binding
Erlang
3
star
36

crdts

Convergent replicated data type for Erlang
Erlang
3
star
37

curie

The type CURIE (compact URI) for Golang
Go
3
star
38

erlang-in-docker

Erlang/OTP container
Dockerfile
3
star
39

elasticnt

N-triple intake to Elastic Search
Erlang
3
star
40

chronolog

time series database
Erlang
3
star
41

ecsd

AWS ECS microservice supervisor
Erlang
3
star
42

permit

a high-level api for security tokens management.
Erlang
3
star
43

stdio

Creating streams and performing input and output operations on them
Erlang
3
star
44

s3am

Stream I/O to aws s3 buckets
Erlang
3
star
45

gouldian

Go combinator library for building HTTP serverless applications
Go
3
star
46

word2vec

Golang "native" implementation of word2vec algorithm (word2vec++ port)
C++
3
star
47

datomic-aws

AWS Appliance to managed Datomic solutions
Shell
2
star
48

serverless-runtime

Experimental runtime for serverless Erlang application (Use fogfish/serverless) instead
Erlang
2
star
49

swarm

Go channels for distributed queueing and event-driven systems
Go
2
star
50

hnsw

Hierarchical Navigable Small World Graphs
Go
2
star
51

code-build-bot

Serverless CI/CD with AWS CodeBuild
TypeScript
2
star
52

aae

active anti-entropy library
Erlang
2
star
53

schemacli

schema.org ontology command-line
Go
2
star
54

faults

Type safe constructs to annotate Golang errors with the context
Go
2
star
55

d3

direct distributed dets interface
Erlang
2
star
56

dive

ephemeral and persistent b-tree interface
Erlang
1
star
57

esio

HTTP client to Elastic Search for Erlang application.
Erlang
1
star
58

homebrew-qemu-9pfs

Homebrew Tap install QEMU with 9P filesystem
Ruby
1
star
59

geojson

GeoJSON / RFC7946 codec for Golang
Go
1
star
60

kv8.rel

simple erlang benchmark utility
Shell
1
star
61

cryptex

Semi-automatic cipher for Algebraic Data Types in Golang
Go
1
star
62

elasticlog

Elastic Search datalog support
Erlang
1
star
63

pq

Erlang process queues (pool of workers)
Erlang
1
star
64

znap

Build and replay snapshots from your asynchronous event stream(s)
Scala
1
star
65

runscript

tiny wrapper to run script / shell command as root
C
1
star
66

skiplist

Golang SkipList data structure
Go
1
star
67

ambit

Erlang
1
star
68

erlcc

Erlang Code Compile: wrapper of native compile module
Makefile
1
star
69

tf-workspace

A personal workspace for TensorFlow.
Jupyter Notebook
1
star
70

it

Human-friendly unit tests assertions for Go
Go
1
star
71

clue

system and application status repository for Erlang
Erlang
1
star
72

clot

cloud toolkit for Erlang applications
Erlang
1
star
73

hornlog

define and evaluate horn clauses
Makefile
1
star
74

hexer

Hexastore: Sextuple Indexing for Semantic Web Data Management
Go
1
star
75

pns

process namespace
Erlang
1
star