Class: RDF::Util::Cache
- Inherits:
-
Object
- Object
- RDF::Util::Cache
- Defined in:
- 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](github.com/ruby-rdf/sparql/) and [Spira](github.com/ruby-rdf/spira). Do be sure to include any changes here in the RDF.rb changelog.
Direct Known Subclasses
Defined Under Namespace
Classes: ObjectSpaceCache, WeakRefCache
Instance Attribute Summary collapse
-
#capacity ⇒ Object
readonly
The configured cache capacity.
Class Method Summary collapse
Instance Method Summary collapse
- #capacity? ⇒ Boolean (also: #has_capacity?)
-
#initialize(capacity = nil) ⇒ Cache
constructor
A new instance of Cache.
- #size ⇒ Integer
Constructor Details
Instance Attribute Details
#capacity ⇒ Object (readonly)
The configured cache capacity.
21 22 23 |
# File 'lib/rdf/util/cache.rb', line 21 def capacity @capacity end |
Class Method Details
.new(*args) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rdf/util/cache.rb', line 25 def self.new(*args) # JRuby doesn't support `ObjectSpace#_id2ref` unless the `-X+O` # startup option is given. In addition, ObjectSpaceCache is very slow # on Rubinius. On those platforms we'll default to using # the WeakRef-based cache: if RUBY_PLATFORM == 'java' || (defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx') klass = WeakRefCache else klass = ObjectSpaceCache end cache = klass.allocate cache.send(:initialize, *args) cache end |
Instance Method Details
#capacity? ⇒ Boolean Also known as: has_capacity?
56 57 58 |
# File 'lib/rdf/util/cache.rb', line 56 def capacity? @capacity.equal?(-1) || @capacity > @cache.size end |
#size ⇒ Integer
50 51 52 |
# File 'lib/rdf/util/cache.rb', line 50 def size @cache.size end |