class

LRUCache(K, V)

Inherits Reference < Object

LRU cache (Least Recently Used). LRUCache supports lifecycle, a global item size limit and an expiration time can be set for each item.

If a max_size is defined, the LRU cache can only contain max_size items. Beyond max_size, items are deleted from the oldest to the most recently used.

Constructors

new(*, max_size : Int32 | Nil = nil)

Creates a new LRUCache instance. If max_size is defined, the LRU cache can only contain max_size items. Beyond max_size, items are deleted from the oldest to the most recently used.

Source

Instance methods

[](key : K) : V

Same as get!(key).

Source
[]=(key : K, item : Tuple(V, Time | Nil)) : LRUCache

Same as set!(key, item)

Source
[]=(key : K, value : V) : LRUCache

Same as set(key, value).

Source
[]?(key : K) : V | Nil

Same as get(key).

Source
add!(key : K, item : Tuple(V, Time | Nil)) : LRUCache

Adds an item in the cache. If key exists, this methods raises a KeyError exception.

Source
add!(key : K, value : V, expire_at : Time | Nil = nil) : LRUCache

Adds a value in the cache. If key exists, this methods raises a KeyError exception.

Source
clear

Empties the cache.

Source
delete(key : K) : Tuple(V, Time | Nil) | Nil

Deletes an item from the cache. Returns the deleted item or nil.

Source
expire_at!(key : K) : Time | Nil

Returns the expiration time of a key.

Source
get(key : K) : V | Nil

Get a value by its key. Returns the item value or nil.

Source
get!(key : K) : V

If key does not exist, this methods raises a KeyError exception.

Source
has?(key : K) : Bool

Checks if the cache has an item by its key. This method checks the item expiration but does not consider the item to be used (the order in the LRU cache is inchanged).

Source
items

Returns the Hash containing all items. The Hash can be handled normally without affecting the behavior of the LRU cache.

Source
keys

Returns all keys.

Source
max_size

Returns the max items allowed in the cache.

Source
set(key : K, item : Tuple(V, Time | Nil)) : LRUCache

Sets an item in the cache.

Source
set(key : K, value : V, expire_at : Time | Nil = nil) : LRUCache

Sets a value in the cache.

Source
size

Returns the cache size (numbers of items in the cache).

Source
touch(key : K) : Bool

Given that the LRU cache purge the items least-recently-used, this method touches an item to consider it as recent use. This makes it possible to up an old element. If the item does not exist, nothing happens.

Returns true if the existing item has been touched, false if not (because does not exist).

Source