Class: RDF::Util::Cache::WeakRefCache

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 uses the WeakRef class from Ruby's standard library, and provides adequate performance on JRuby and on Ruby 3.x.

Instance Attribute Summary

Attributes inherited from RDF::Util::Cache

#capacity

Instance Method Summary collapse

Methods inherited from RDF::Util::Cache

#capacity?, #size

Constructor Details

#initialize(capacity = nil) ⇒ WeakRefCache

Returns a new instance of WeakRefCache.

Parameters:

  • capacity (Integer) (defaults to: nil)

Since:

  • 0.2.0



119
120
121
122
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/cache.rb', line 119

def initialize(capacity = nil)
  require 'weakref' unless defined?(::WeakRef)
  super
end

Instance Method Details

#[](key) ⇒ Object

Parameters:

Returns:

Since:

  • 0.2.0



127
128
129
130
131
132
133
134
135
136
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/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

#[]=(key, value) ⇒ Object

Parameters:

Returns:

Since:

  • 0.2.0



142
143
144
145
146
147
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/cache.rb', line 142

def []=(key, value)
  if capacity?
    @cache[key] = WeakRef.new(value)
  end
  value
end

#delete(key) ⇒ Object

Remove cache entry for key

Parameters:

Returns:

  • (Object)

    the previously referenced object

Since:

  • 0.2.0



154
155
156
157
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/cache.rb', line 154

def delete(key)
  ref = @cache.delete(key)
  ref.__getobj__ rescue nil
end