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
Source
# File lib/rdf/util/cache.rb, line 73 def [](key) if value_id = @cache[key] ObjectSpace._id2ref(value_id) rescue nil end end
@param [Object] key @return [Object]
Source
# 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
@param [Object] key @param [Object] value @return [Object]
Source
# File lib/rdf/util/cache.rb, line 98 def delete(key) id = @cache[key] @cache.delete(key) @index.delete(id) if id end
Remove cache entry for key
@param [Object] key @return [Object] the previously referenced object
Private Instance Methods
Source
# File lib/rdf/util/cache.rb, line 106 def finalizer_proc proc { |id| @cache.delete(@index.delete(id)) } end