Class: GH::Cache::SimpleCache
- Inherits:
-
Object
- Object
- GH::Cache::SimpleCache
- Defined in:
- lib/gh/cache.rb
Overview
Internal: Simple in-memory cache basically implementing a copying GC.
Instance Method Summary collapse
-
#fetch(key) ⇒ Object
Internal: Tries to fetch a value from the cache and if it doesn’t exist, generates it from the block given.
-
#initialize(size = 2048) ⇒ SimpleCache
constructor
Internal: Initializes a new SimpleCache.
Constructor Details
#initialize(size = 2048) ⇒ SimpleCache
Internal: Initializes a new SimpleCache.
size - Number of objects to hold in cache.
16 17 18 |
# File 'lib/gh/cache.rb', line 16 def initialize(size = 2048) @old, @new, @size, @mutex = {}, {}, size/2, Mutex.new end |
Instance Method Details
#fetch(key) ⇒ Object
Internal: Tries to fetch a value from the cache and if it doesn’t exist, generates it from the block given.
22 23 24 25 |
# File 'lib/gh/cache.rb', line 22 def fetch(key) @mutex.lock { @old, @new = @new, {} if @new.size > @size } if @new.size > @size @new[key] ||= @old[key] || yield end |