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

Inherits:
RDF::Util::Cache show all
Defined in:
vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/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 Method Summary collapse

Methods inherited from RDF::Util::Cache

#has_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



69
70
71
72
73
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/util/cache.rb', line 69

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

#[]=(key, value) ⇒ Object

Parameters:

Returns:

Since:

  • 0.2.0



79
80
81
82
83
84
85
86
87
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/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) ⇒ Object

Remove cache entry for key

Parameters:

Returns:

  • (Object)

    the previously referenced object

Since:

  • 0.2.0



94
95
96
97
98
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/util/cache.rb', line 94

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