macula_cache (macula v0.14.3)

View Source

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

Summary

Functions

Clear all entries.

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

Get all keys in cache (most recent first).

Get max size.

Create new cache with max size.

Put entry in cache with current timestamp.

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

Remove entry from cache.

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.