Class: RDF::Util::Cache

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

Overview

A Hash-like cache that holds only weak references to the values it caches, meaning that values contained in the cache can be garbage collected. This allows the cache to dynamically adjust to changing memory conditions, caching more objects when memory is plentiful, but evicting most objects if memory pressure increases to the point of scarcity.

While this cache is something of an internal implementation detail of RDF.rb, some external libraries do currently make use of it as well, including SPARQL and Spira. Do be sure to include any changes here in the RDF.rb changelog.

Direct Known Subclasses

ObjectSpaceCache, WeakRefCache

Defined Under Namespace

Classes: ObjectSpaceCache, WeakRefCache

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(capacity = nil) ⇒ Cache

Returns a new instance of Cache.

Parameters:

  • capacity (Integer) (defaults to: nil)

Since:

  • 0.2.0



42
43
44
45
46
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/cache.rb', line 42

def initialize(capacity = nil)
  @capacity = capacity || RDF.config.cache_size
  @cache  ||= {}
  @index  ||= {}
end

Instance Attribute Details

#capacityObject (readonly)

The configured cache capacity.

Since:

  • 0.2.0



21
22
23
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/cache.rb', line 21

def capacity
  @capacity
end

Instance Method Details

#capacity?Boolean Also known as: has_capacity?

Returns:

  • (Boolean)

Since:

  • 0.2.0



56
57
58
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/cache.rb', line 56

def capacity?
  @capacity.equal?(-1) || @capacity > @cache.size
end

#sizeInteger

Returns:

Since:

  • 0.2.0



50
51
52
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/cache.rb', line 50

def size
  @cache.size
end