Class: Cache::Strategy::CapacityBounded
- Inherits:
-
Cache::Strategy
- Object
- Cache::Strategy
- Cache::Strategy::CapacityBounded
- 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.
Instance Attribute Summary collapse
-
#capacity ⇒ Object
Returns the value of attribute capacity.
Instance Method Summary collapse
- #dec(item) ⇒ Object
- #empty? ⇒ Boolean
- #full? ⇒ Boolean
- #inc(item) ⇒ Object
-
#initialize(capacity) ⇒ CapacityBounded
constructor
A new instance of CapacityBounded.
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
#capacity ⇒ Object
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
50 51 52 |
# File 'lib/cache/cache.rb', line 50 def empty? @n_items == 0 end |
#full? ⇒ 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 |