class

LRUCache(T)

Inherits Reference < Object

A simple LRU Cache to store items of whatever type T

Constants

Log = ::Log.for(self)
VERSION = "1.0.5"

Constructors

new(max_size : Int32 | Int64, clean_interval : Time::Span | Nil = nil)

Creates a new LRUCache with the given max_size and clean_interval

Source

Instance methods

del(key : String) : Nil

Deletes a item from the LRU Cache.

expire_time argument is in seconds.

cache = LRUCache(String).new(5)

cache.set("key", "value", 5)
cache.del("key")

pp cache.get("key") # => nil
Source
get(key : String) : T | Nil

Gets the item associated with the key

cache = LRUCache(String).new(5)

cache.set("key", "value", 5)

pp cache.get("key") # => "value"
Source
items

Gets all the items that the LRU cache is holding.

cache = LRUCache(String).new(5)

cache.set("key", "value", 5)
cache.set("key2", "value2")

pp cache.items # => {"key" => LRUCache::Item(String)(@expires_at=1771043818, @value="value"), "key2" => LRUCache::Item(String)(@expires_at=nil, @value="value2")}
Source
max_size

Gets the max amount of items the LRU cache can hold.

Source
on_event

Define a callback for when a new event is received.

cache = LRUCache(String).new(5)

cache.on_event do |e|
  puts e.key
  puts e.event_type
end
Source
set(key : String, value : T, expire_time : Int | Nil = nil) : Nil

Sets a item of the desired Type T into the LRU Cache.

expire_time argument is in seconds.

cache = LRUCache(String).new(5)

cache.set("key", "value", 5)

pp cache.get("key") # => "value"
Source
size

Gets the current amount of items the LRU cache is holding.

cache = LRUCache(String).new(5)

cache.set("key", "value", 5)
cache.set("key2", "value2")

pp cache.size # => 2
Source

Nested types