Class: RDF::Util::Cache::WeakRefCache
- Inherits:
-
RDF::Util::Cache
- Object
- RDF::Util::Cache
- RDF::Util::Cache::WeakRefCache
- Defined in:
- 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 2.x.
Instance Attribute Summary
Attributes inherited from RDF::Util::Cache
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#delete(key) ⇒ Object
Remove cache entry for key.
-
#initialize(capacity = nil) ⇒ WeakRefCache
constructor
A new instance of WeakRefCache.
Methods inherited from RDF::Util::Cache
Constructor Details
#initialize(capacity = nil) ⇒ WeakRefCache
Returns a new instance of WeakRefCache.
113 114 115 116 |
# File 'lib/rdf/util/cache.rb', line 113 def initialize(capacity = nil) require 'weakref' unless defined?(::WeakRef) super end |
Instance Method Details
#[](key) ⇒ Object
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/rdf/util/cache.rb', line 121 def [](key) if (ref = @cache[key]) if ref.weakref_alive? value = ref.__getobj__ rescue nil else @cache.delete(key) nil end end end |
#[]=(key, value) ⇒ Object
136 137 138 139 140 141 |
# File 'lib/rdf/util/cache.rb', line 136 def []=(key, value) if capacity? @cache[key] = WeakRef.new(value) end value end |
#delete(key) ⇒ Object
Remove cache entry for key
148 149 150 151 |
# File 'lib/rdf/util/cache.rb', line 148 def delete(key) ref = @cache.delete(key) ref.__getobj__ rescue nil end |