Class: Cache::Strategy::LRU

Inherits:
CapacityBounded show all
Defined in:
lib/cache/cache.rb

Overview

Implements the least recently used (LRU) strategy.

Defined Under Namespace

Classes: Item

Instance Attribute Summary

Attributes inherited from CapacityBounded

#capacity

Instance Method Summary collapse

Methods inherited from CapacityBounded

#dec, #empty?, #full?, #inc, #initialize

Constructor Details

This class inherits a constructor from Cache::Strategy::CapacityBounded

Instance Method Details

#access(item) ⇒ Object



84
85
86
# File 'lib/cache/cache.rb', line 84

def access(item)
  item.time = Time.now
end

#delete(item) ⇒ Object



88
89
90
# File 'lib/cache/cache.rb', line 88

def delete(item)
  dec(item)
end

#insert_or_extrude(item, enum) ⇒ Object

enum

a [key, item] enumerable



95
96
97
98
99
100
# File 'lib/cache/cache.rb', line 95

def insert_or_extrude(item, enum)
  # find least recently used key/item and yield
  yield enum.min_by {|key, it| it.time} while full?
  item.time = Time.now
  inc(item)
end

#item_classObject



82
# File 'lib/cache/cache.rb', line 82

def item_class() Item end