Class: GH::Cache::SimpleCache

Inherits:
Object
  • Object
show all
Defined in:
lib/gh/cache.rb

Overview

Internal: Simple in-memory cache basically implementing a copying GC.

Instance Method Summary collapse

Constructor Details

#initialize(size = 2048) ⇒ SimpleCache

Internal: Initializes a new SimpleCache.

size - Number of objects to hold in cache.



15
16
17
# File 'lib/gh/cache.rb', line 15

def initialize(size = 2048)
  @old, @new, @size, @mutex = {}, {}, size/2, Mutex.new
end

Instance Method Details

#clearObject

Internal: …



27
28
29
# File 'lib/gh/cache.rb', line 27

def clear
  @mutex.synchronize { @old, @new = {}, {} }
end

#fetch(key) ⇒ Object

Internal: Tries to fetch a value from the cache and if it doesn’t exist, generates it from the block given.



21
22
23
24
# File 'lib/gh/cache.rb', line 21

def fetch(key)
  @mutex.synchronize { @old, @new = @new, {} if @new.size > @size } if @new.size > @size
  @new[key] ||= @old[key] || yield
end