class LRUCache(T)
- LRUCache(T)
- Reference
- Object
Overview
A simple LRU Cache to store items of whatever type T
Defined in:
lru.crConstant Summary
-
Log =
::Log.for(self) -
VERSION =
"1.0.0"
Constructors
Instance Method Summary
-
#del(key : String) : Nil
Deletes a item from the LRU Cache.
-
#get(key : String) : T | Nil
Gets the item associated with the
key -
#items : Hash(String, Item(T))
Gets all the items that the LRU cache is holding.
-
#max_size : Int64 | Int32
Gets the max amount of items the LRU cache can hold.
-
#set(key : String, value : T, expire_time : Int64 | Nil = nil) : Nil
Sets a item of the desired Type
Tinto the LRU Cache. -
#size : Int64
Gets the current amount of items the LRU cache is holding.
Constructor Detail
def self.new(max_size : Int32 | Int64, clean_interval : Time::Span | Nil = 1.seconds)
#
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
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"
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")}
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"
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