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 69
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 79
def []=(key, value)
  if has_capacity?
    id = value.__id__
    @cache[key] = id
    @index[id] = key
    ObjectSpace.define_finalizer(value, proc {|id| @cache.delete(@index.delete(id))})
  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 94
def delete(key)
  id = @cache[key]
  @cache.delete(key)
  @index.delete(id) if id
end