View Source filezcache_lru (filezcache v2.1.1)

Erlang LRU cache/Queue with deletion by key.

Motivation: For many purposes we need FIFO of elements with fast-enough deletion from the middle. This FIFO also can be used as LRU cache (Least Recently Used Cache).

Summary

Functions

Add new element to cache with defined key.

Check if queue is empty.

Check if cache has element with key.

Lookup and update cache (LRU function).

Create new unlimited cache.

Create new limited cache with defined max size

Pop element from head.

Push element to queue tail.

Get number of elements in the LRU cache. Complexity: O(1)

Take element from queue by key

Types

-type key() :: any().
-type lru() ::
    #lru{max_size :: non_neg_integer() | unlimited,
         current_size :: non_neg_integer(),
         lookup_cache :: #{key() => {non_neg_integer(), value()}},
         usage :: gb_trees:tree(non_neg_integer(), key()),
         last :: term()}.
-type value() :: any().

Functions

-spec add(key(), value(), lru()) -> lru().

Add new element to cache with defined key.

Complexity: O(log(N))
-spec empty(lru()) -> boolean().

Check if queue is empty.

Complexity: O(1)
-spec has_key(key(), lru()) -> boolean().

Check if cache has element with key.

Complexity: O(1)
Link to this function

lookup_and_update(Key, Lru)

View Source
-spec lookup_and_update(key(), lru()) -> {ok, value(), lru()} | error.

Lookup and update cache (LRU function).

Complexity: O(log(N))
-spec new() -> lru().

Create new unlimited cache.

Complexity: O(1)
-spec new(MaxSize :: pos_integer()) -> lru().

Create new limited cache with defined max size

Complexity: O(1)
-spec pop(lru()) -> {key(), value(), lru()}.

Pop element from head.

Complexity: O(log(N))
-spec push(key(), value(), lru()) -> lru().

Push element to queue tail.

Complexity: O(log(N))
-spec size(lru()) -> non_neg_integer().
Get number of elements in the LRU cache. Complexity: O(1)
-spec take(key(), lru()) -> {value(), lru()} | error.

Take element from queue by key

Complexity: O(log(N))