LRUCache

A cache utilizing a simple LRU (Least Recently Used) policy. The items managed by this cache must respond to the key method. Attempts to optimize reads rather than inserts!

LRU semantics are enforced by inserting the items in a queue. The lru item is always at the tail. Two special sentinels (head, tail) are used to simplify (?) the code.

Methods
[] []= clear delete first last lru new
Classes and Modules
Module LRUCache::Item
Class LRUCache::Sentinel
Attributes
[R] head the head sentinel and the tail sentinel, tail.prev points to the lru item.
[RW] max_items the maximum number of items in the cache.
[R] tail the head sentinel and the tail sentinel, tail.prev points to the lru item.
Public Class methods
new(max_items)
# File lib/more/facets/lrucache.rb, line 59
  def initialize(max_items)
    @max_items = max_items
    lru_clear()
  end
Public Instance methods
[](key)

Lookup an item in the cache.

# File lib/more/facets/lrucache.rb, line 66
  def [](key)
    if item = super
      return lru_touch(item)
    end
  end
[]=(key, item)

The inserted item is considered mru!

# File lib/more/facets/lrucache.rb, line 74
  def []=(key, item)
    item = super
    item.lru_key = key
    lru_insert(item)
  end
clear()

Clear the cache.

# File lib/more/facets/lrucache.rb, line 90
  def clear
    super
    lru_clear()
  end
delete(key)

Delete an item from the cache.

# File lib/more/facets/lrucache.rb, line 82
  def delete(key)
    if item = super
      lru_delete(item)
    end
  end
first()

The first (mru) element in the cache.

# File lib/more/facets/lrucache.rb, line 97
  def first
    @head.lru_next
  end
last()

The last (lru) element in the cache.

This method is also aliased as lru
# File lib/more/facets/lrucache.rb, line 103
  def last
    @tail.lru_prev
  end
lru()

Alias for last