Class: Muze::Core::BoundedCache
- Inherits:
-
Object
- Object
- Muze::Core::BoundedCache
- Defined in:
- lib/muze/core/cache.rb
Overview
Tiny bounded LRU cache for generated DSP lookup tables.
Instance Method Summary collapse
- #clear ⇒ Object
- #fetch(key) ⇒ Object
-
#initialize(max_size:) ⇒ BoundedCache
constructor
A new instance of BoundedCache.
- #size ⇒ Object
Constructor Details
#initialize(max_size:) ⇒ BoundedCache
Returns a new instance of BoundedCache.
7 8 9 10 11 12 13 |
# File 'lib/muze/core/cache.rb', line 7 def initialize(max_size:) raise Muze::ParameterError, "max_size must be positive" unless max_size.is_a?(Integer) && max_size.positive? @max_size = max_size @entries = {} @mutex = Mutex.new end |
Instance Method Details
#clear ⇒ Object
29 30 31 |
# File 'lib/muze/core/cache.rb', line 29 def clear @mutex.synchronize { @entries.clear } end |
#fetch(key) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/muze/core/cache.rb', line 15 def fetch(key) @mutex.synchronize do if @entries.key?(key) value = @entries.delete(key) @entries[key] = value return value end value = yield @entries.shift while @entries.size >= @max_size @entries[key] = value end end |
#size ⇒ Object
33 34 35 |
# File 'lib/muze/core/cache.rb', line 33 def size @mutex.synchronize { @entries.size } end |