class GH::Cache::SimpleCache

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

Public Class Methods

new(size = 2048) click to toggle source

Internal: Initializes a new SimpleCache.

size - Number of objects to hold in cache.

# File lib/gh/cache.rb, line 15
def initialize(size = 2048)
  @old, @new, @size, @mutex = {}, {}, size/2, Mutex.new
end

Public Instance Methods

clear() click to toggle source

Internal: …

# File lib/gh/cache.rb, line 27
def clear
  @mutex.synchronize { @old, @new = {}, {} }
end
fetch(key) { || ... } click to toggle source

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

# 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