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
new(size)
click to toggle source
Creates a new LRUCache
that can hold size
entries.
# File lib/kramdown/utils/lru_cache.rb 19 def initialize(size) 20 @size = size 21 @cache = {} 22 end
Public Instance Methods
[](key)
click to toggle source
Returns the stored value for key
or nil
if no value was stored under the key.
# File lib/kramdown/utils/lru_cache.rb 25 def [](key) 26 (val = @cache.delete(key)).nil? ? nil : @cache[key] = val 27 end
[]=(key, value)
click to toggle source
Stores the value
under the key
.
# File lib/kramdown/utils/lru_cache.rb 30 def []=(key, value) 31 @cache.delete(key) 32 @cache[key] = value 33 @cache.shift if @cache.length > @size 34 end