Class: RDF::Util::Cache

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

Constructor Details

#initialize(capacity = -1)) ⇒ Cache

Returns a new instance of Cache

Parameters:

  • capacity (Integer) (defaults to: -1))

Since:

  • 0.2.0



39
40
41
42
43
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/util/cache.rb', line 39

def initialize(capacity = -1)
  @capacity = capacity
  @cache  ||= {}
  @index  ||= {}
end

Instance Method Details

#has_capacity?Boolean

Returns:

  • (Boolean)

Since:

  • 0.2.0



53
54
55
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/util/cache.rb', line 53

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

#sizeInteger

Returns:

Since:

  • 0.2.0



47
48
49
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/util/cache.rb', line 47

def size
  @cache.size
end