Search documentation of macula

# macula_cache (macula v0.44.2)

View Source

Generic LRU cache implementation. Provides least-recently-used eviction with configurable max size.

# Summary

## Types

cache/0
entry/0
key/0
timestamp/0
value/0

## Functions

clear(Cache)

Clear all entries.

get(Cache, Key)

Get entry from cache. Returns {ok, Value, UpdatedCache} or not_found. The updated cache has the entry moved to front (LRU).

keys(_)

Get all keys in cache (most recent first).

max_size(_)

Get max size.

new(MaxSize)

Create new cache with max size.

put(Cache, Key, Value)

Put entry in cache with current timestamp.

put(Cache, Key, Value, Timestamp)

Put entry in cache with custom timestamp (for testing).

remove(Cache, Key)

Remove entry from cache.

size(_)

Get number of entries.

# Types

# cache/0

    -type cache() :: #{entries := [entry()], max_size := pos_integer()}.

# entry/0

    -type entry() :: #{key := key(), value := value(), timestamp := timestamp()}.

# key/0

    -type key() :: term().

# timestamp/0

    -type timestamp() :: integer().

# value/0

    -type value() :: term().

# Functions

# clear(Cache)

    -spec clear(cache()) -> cache().

Clear all entries.

# get(Cache, Key)

    -spec get(cache(), key()) -> {ok, value(), cache()} | not_found.

Get entry from cache. Returns {ok, Value, UpdatedCache} or not_found. The updated cache has the entry moved to front (LRU).

# keys(_)

    -spec keys(cache()) -> [key()].

Get all keys in cache (most recent first).

# max_size(_)

    -spec max_size(cache()) -> pos_integer().

Get max size.

# new(MaxSize)

    -spec new(pos_integer()) -> cache().

Create new cache with max size.

# put(Cache, Key, Value)

    -spec put(cache(), key(), value()) -> cache().

Put entry in cache with current timestamp.

# put(Cache, Key, Value, Timestamp)

    -spec put(cache(), key(), value(), timestamp()) -> cache().

Put entry in cache with custom timestamp (for testing).

# remove(Cache, Key)

    -spec remove(cache(), key()) -> cache().

Remove entry from cache.

# size(_)

    -spec size(cache()) -> non_neg_integer().

Get number of entries.