Class: Cache::Strategy::CapacityBounded

Inherits:
Cache::Strategy show all
Defined in:
lib/cache/cache.rb

Overview

Abstract class for a capacity bounded strategy. Only up to capacity items are allowed to be stored in the cache at any time.

Direct Known Subclasses

LFU, LRU

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(capacity) ⇒ CapacityBounded

Returns a new instance of CapacityBounded.



31
32
33
34
# File 'lib/cache/cache.rb', line 31

def initialize(capacity)
  @capacity = capacity
  @n_items = 0  # number of items in cache
end

Instance Attribute Details

#capacityObject

Returns the value of attribute capacity.



29
30
31
# File 'lib/cache/cache.rb', line 29

def capacity
  @capacity
end

Instance Method Details

#dec(item) ⇒ Object



41
42
43
44
# File 'lib/cache/cache.rb', line 41

def dec(item)
  raise if empty?
  @n_items -= 1
end

#empty?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/cache/cache.rb', line 50

def empty?
  @n_items == 0
end

#full?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/cache/cache.rb', line 46

def full?
  @n_items >= @capacity 
end

#inc(item) ⇒ Object



36
37
38
39
# File 'lib/cache/cache.rb', line 36

def inc(item)
  raise if full?
  @n_items += 1
end