class LRUCache(T)

Overview

A simple LRU Cache to store items of whatever type T

Defined in:

lru.cr

Constant Summary

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

Constructors

Instance Method Summary

Constructor Detail

def self.new(max_size : Int32 | Int64, clean_interval : Time::Span | Nil = 1.seconds) #

Creates a new LRUCache with the given #max_size and clean_interval


[View source]

Instance Method Detail

def 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

[View source]
def 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"

[View source]
def items : Hash(String, Item(T)) #

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")}

[View source]
def max_size : Int64 | Int32 #

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


[View source]
def set(key : String, value : T, expire_time : Int64 | 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"

[View source]
def size : Int64 #

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

[View source]