Class: RDF::Util::Cache::ObjectSpaceCache

Inherits:
RDF::Util::Cache show all
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/cache.rb

Overview

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.

Instance Attribute Summary

Attributes inherited from RDF::Util::Cache

#capacity

Instance Method Summary collapse

Methods inherited from RDF::Util::Cache

#capacity?, #initialize, #size

Constructor Details

This class inherits a constructor from RDF::Util::Cache

Instance Method Details

#[](key) ⇒ Object

Parameters:

Returns:

Since:

  • 0.2.0



73
74
75
76
77
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/cache.rb', line 73

def [](key)
  if value_id = @cache[key]
    ObjectSpace._id2ref(value_id) rescue nil
  end
end

#[]=(key, value) ⇒ Object

Parameters:

Returns:

Since:

  • 0.2.0



83
84
85
86
87
88
89
90
91
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/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) ⇒ Object

Remove cache entry for key

Parameters:

Returns:

  • (Object)

    the previously referenced object

Since:

  • 0.2.0



98
99
100
101
102
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/cache.rb', line 98

def delete(key)
  id = @cache[key]
  @cache.delete(key)
  @index.delete(id) if id
end