Module cuckoo_cache

High-performance concurrent in-memory cache with the ability to detect one-hit-wonders using a Cuckoo Filter.

Description

High-performance concurrent in-memory cache with the ability to detect one-hit-wonders using a Cuckoo Filter.

Data Types

cache_name()

cache_name() = term()

cuckoo_cache()

cuckoo_cache() = #cuckoo_cache{}

fallback_fun()

fallback_fun() = fun((key()) -> {ok, value()} | {error, term()})

fingerprint_size()

fingerprint_size() = 4 | 8 | 16 | 32 | 64

hash()

hash() = non_neg_integer()

hash_function()

hash_function() = fun((binary()) -> hash())

key()

key() = term()

option()

option() = {name, term()} | {ttl, timeout()} | {fingerprint_size, fingerprint_size()} | {bucket_size, pos_integer()} | {hash_function, hash_function()}

options()

options() = [option()]

value()

value() = term()

Function Index

capacity/1Returns the maximum capacity of the cuckoo filter in a cache.
delete/2Removes an entry from a cache.
fetch/3Equivalent to fetch(Cache, Key, Fallback, Cache#cuckoo_cache.ttl).
fetch/4Fetches an entry from a cache, generating a value on cache miss.
filter_size/1Returns number of items in the cuckoo filter of a cache.
get/2Retrieves an entry from a cache.
new/1Equivalent to new(Capacity, []).
new/2Creates a new cuckoo cache with the given capacity and options.
put/3Equivalent to put(Cache, Key, Value, Cache#cuckoo_cache.ttl).
put/4Places an entry in a cache.
size/1Returns number of items in the ets table of a cache.
whereis/1Retrieves a cuckoo_cache from persistent_term by its name.

Function Details

capacity/1

capacity(Cuckoo_cache::cuckoo_cache() | cache_name()) -> pos_integer()

Returns the maximum capacity of the cuckoo filter in a cache.

delete/2

delete(Cuckoo_cache::cuckoo_cache() | cache_name(), Key::key()) -> ok | {error, not_found}

Removes an entry from a cache.

This removes the entry from both the cuckoo filter and the ets table.

Returns ok if the deletion was successful, and returns {error, not_found} if the element could not be found in the cuckoo filter.

fetch/3

fetch(Cache::cuckoo_cache() | cache_name(), Key::key(), Fallback::fallback_fun()) -> value()

Equivalent to fetch(Cache, Key, Fallback, Cache#cuckoo_cache.ttl).

fetch/4

fetch(Cache::cuckoo_cache() | cache_name(), Key::key(), Fallback::fallback_fun(), TTL::timeout()) -> value()

Fetches an entry from a cache, generating a value on cache miss.

If the requested entry is found in the cache, cached value is returned. If the entry is not contained in the cache, the provided fallback function will be executed and its result is returned.

If the entry does not exist in the cuckoo filter, the returned value from fallback function will not be cached in the ets table, but the key will be added to the cuckoo filter.

If the entry exists in the cuckoo filter, the returned value from fallback function will be cached in the ets table.

When a new entry is added to the cuckoo filter, if cuckoo filter does not have enough space, a random element gets removed from both the filter and the ets table.

filter_size/1

filter_size(Cuckoo_cache::cuckoo_cache() | cache_name()) -> non_neg_integer()

Returns number of items in the cuckoo filter of a cache.

get/2

get(Cuckoo_cache::cuckoo_cache() | cache_name(), Key::key()) -> {ok, value()} | {error, not_found}

Retrieves an entry from a cache.

If entry exists in the cache {ok, Value} is returned, otherwise {error, not_found} is returned.

new/1

new(Capacity::pos_integer()) -> cuckoo_cache()

Equivalent to new(Capacity, []).

new/2

new(Capacity::pos_integer(), Opts::options()) -> cuckoo_cache()

Creates a new cuckoo cache with the given capacity and options.

A cuckoo cache has a built-in cuckoo filter to detect one-hit-wonders and an ets table to keep cached entries.

Given capacity is used as the capacity for the built-in cuckoo filter. Note that the actual capacity might be higher than the given capacity, because internally number of buckets in a cuckoo filter must be a power of 2.

The capacity of the ets table depends on the ratio of one-hit-wonders to the capacity of the filter, and is limited by the fingerprint key space. The size of ets table can not exceed the capacity of the cuckoo filter.

Possible options are:

put/3

put(Cache::cuckoo_cache() | cache_name(), Key::key(), Value::value()) -> ok

Equivalent to put(Cache, Key, Value, Cache#cuckoo_cache.ttl).

put/4

put(Cache::cuckoo_cache() | cache_name(), Key::key(), Value::value(), TTL::timeout()) -> ok

Places an entry in a cache.

This will add the entry to both the cuckoo filter and the ets table.

If there is not enough space in the filter a random element gets removed.

size/1

size(Cuckoo_cache::cuckoo_cache() | cache_name()) -> non_neg_integer()

Returns number of items in the ets table of a cache.

whereis/1

whereis(Name::cache_name()) -> cuckoo_cache()

Retrieves a cuckoo_cache from persistent_term by its name.


Generated by EDoc