class RDF::Util::Cache::WeakRefCache
This implementation uses the ‘WeakRef` class from Ruby’s standard library, and provides adequate performance on JRuby and on Ruby 3.x.
@see ruby-doc.org/stdlib-2.2.0/libdoc/weakref/rdoc/WeakRef.html
Public Class Methods
Source
# File lib/rdf/util/cache.rb, line 119 def initialize(capacity = nil) require 'weakref' unless defined?(::WeakRef) super end
@param [Integer] capacity
Calls superclass method
RDF::Util::Cache::new
Public Instance Methods
Source
# File lib/rdf/util/cache.rb, line 127 def [](key) if (ref = @cache[key]) if ref.weakref_alive? ref.__getobj__ rescue nil else @cache.delete(key) nil end end end
@param [Object] key @return [Object]
Source
# File lib/rdf/util/cache.rb, line 142 def []=(key, value) if capacity? @cache[key] = WeakRef.new(value) end value end
@param [Object] key @param [Object] value @return [Object]
Source
# File lib/rdf/util/cache.rb, line 154 def delete(key) ref = @cache.delete(key) ref.__getobj__ rescue nil end
Remove cache entry for key
@param [Object] key @return [Object] the previously referenced object