class RDF::Util::Cache::ObjectSpaceCache

This implementation relies on ‘ObjectSpace#_id2ref` and performs optimally on Ruby >= 2.x; however, it does not work on JRuby by default since much `ObjectSpace` functionality on that platform is disabled unless the `-X+O` startup option is given.

@see ruby-doc.org/core-2.2.2/ObjectSpace.html @see ruby-doc.org/stdlib-2.2.0/libdoc/weakref/rdoc/WeakRef.html

Public Instance Methods

[](key) click to toggle source

@param [Object] key @return [Object]

# File lib/rdf/util/cache.rb, line 73
def [](key)
  if value_id = @cache[key]
    ObjectSpace._id2ref(value_id) rescue nil
  end
end
[]=(key, value) click to toggle source

@param [Object] key @param [Object] value @return [Object]

# File lib/rdf/util/cache.rb, line 83
def []=(key, value)
  if capacity?
    id = value.__id__
    @cache[key] = id
    @index[id] = key
    ObjectSpace.define_finalizer(value, finalizer_proc)
  end
  value
end
delete(key) click to toggle source

Remove cache entry for key

@param [Object] key @return [Object] the previously referenced object

# File lib/rdf/util/cache.rb, line 98
def delete(key)
  id = @cache[key]
  @cache.delete(key)
  @index.delete(id) if id
end

Private Instance Methods

finalizer_proc() click to toggle source
# File lib/rdf/util/cache.rb, line 106
def finalizer_proc
  proc { |id| @cache.delete(@index.delete(id)) }
end