class Kramdown::Utils::LRUCache
A simple least recently used (LRU) cache.
The cache relies on the fact that Ruby’s Hash class maintains insertion order. So deleting and re-inserting a key-value pair on access moves the key to the last position. When an entry is added and the cache is full, the first entry is removed.
Public Class Methods
Source
# File lib/kramdown/utils/lru_cache.rb, line 20 def initialize(size) @size = size @cache = {} end
Creates a new LRUCache
that can hold size
entries.
Public Instance Methods
Source
# File lib/kramdown/utils/lru_cache.rb, line 26 def [](key) (val = @cache.delete(key)).nil? ? nil : @cache[key] = val end
Returns the stored value for key
or nil
if no value was stored under the key.
Source
# File lib/kramdown/utils/lru_cache.rb, line 31 def []=(key, value) @cache.delete(key) @cache[key] = value @cache.shift if @cache.length > @size end
Stores the value
under the key
.