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
Add new element to cache with defined key.
Complexity: O(log(N))Check if queue is empty.
Complexity: O(1)Check if cache has element with key.
Complexity: O(1)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)Pop element from head.
Complexity: O(log(N))Push element to queue tail.
Complexity: O(log(N))-spec size(lru()) -> non_neg_integer().
Take element from queue by key
Complexity: O(log(N))