lights FAQ Forum

Data Structures
lfrb WIP


Size-limited LRU cache in Lua

local lrucache = require'lrucache'

LRU cache limited by an abstract size. Values can have different sizes. When a new value is put in the cache and the cache is full, just enough old values are removed to make room for the new value and not exceed the cache max size.


lrucache([options]) -> cache create a new cache
cache.max_size <- size set the cache size limit
cache:clear() clear the cache
cache:free() destroy the cache
self:free_value(val) value destructor (to be overriden)
self:value_size(val) -> size get value size (to be overriden; returns 1)
self:free_size() -> size size left until max_size
self:get(key) -> val get a value from the cache by key
self:remove(key) -> val remove a value from the cache by key
self:remove_val(val) -> key remove a value from the cache
self:remove_last() -> val remove the last value from the cache
self:put(key, val) put a value in the cache, making room as needed

Last updated: 4 months ago | Edit on GitHub

Pkg type:Lua
Version: r1-2-g4fcba8e
Last commit:
License: Public Domain
Requires: dlist 
Required by: tr  ui