Class: Cache::Strategy::LFU

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

Overview

Implements the least frequently used (LFU) 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



60
61
62
# File 'lib/cache/cache.rb', line 60

def access(item)
  item.freq += 1
end

#delete(item) ⇒ Object



64
65
66
# File 'lib/cache/cache.rb', line 64

def delete(item)
  dec(item)
end

#insert_or_extrude(item, enum) ⇒ Object

enum

a [key, item] enumerable



71
72
73
74
75
76
# File 'lib/cache/cache.rb', line 71

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

#item_classObject



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

def item_class() Item end