Sha256: 696e44e5de67768432a9f58e3d5094c25e5f45e81825595af809af5a93e1c8ca

Contents?: true

Size: 738 Bytes

Versions: 9

Compression:

Stored size: 738 Bytes

Contents

require 'thread'
require 'singleton'

##
# A generic, thread-safe in-memory cache. It's used for caching
# RDoc::TemplatePage objects when generating RDoc output.

class RDoc::Cache

  include Singleton

  ##
  # Creates a new, empty cache

  def initialize
    @contents = {}
    @lock = Mutex.new
  end

  ##
  # Checks whether there's a value in the cache with key +key+. If so, then
  # that value will be returned. Otherwise, the given block will be run, and
  # its return value will be put into the cache, and returned.

  def cache(key)
    @lock.synchronize do
      @contents[key] ||= yield
    end
  end

  ##
  # Clears the contents of the cache

  def clear
    @lock.synchronize do
      @contents.clear
    end
  end

end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
voloko-sdoc-0.1.3 rdoc/lib/rdoc/cache.rb
voloko-sdoc-0.1.4 rdoc/lib/rdoc/cache.rb
voloko-sdoc-0.1.5 rdoc/lib/rdoc/cache.rb
voloko-sdoc-0.1.6 rdoc/lib/rdoc/cache.rb
voloko-sdoc-0.1.7 rdoc/lib/rdoc/cache.rb
rdoc-2.4.1 lib/rdoc/cache.rb
rdoc-2.4.2 lib/rdoc/cache.rb
rdoc-2.4.3 lib/rdoc/cache.rb
rdoc-2.4.0 lib/rdoc/cache.rb