Class: PDF::Reader::ObjectCache
- Inherits:
-
Object
- Object
- PDF::Reader::ObjectCache
- Defined in:
- lib/pdf/reader/object_cache.rb
Overview
A Hash-like object for caching commonly used objects from a PDF file.
This is an internal class, no promises about a stable API.
Constant Summary collapse
- CACHEABLE_TYPES =
These object types use little memory and are accessed a heap of times as part of random page access, so we’ll cache the unmarshalled objects and avoid lots of repetitive (and expensive) tokenising
[:Catalog, :Page, :Pages]
Instance Attribute Summary collapse
-
#hits ⇒ Object
readonly
Returns the value of attribute hits.
-
#misses ⇒ Object
readonly
Returns the value of attribute misses.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #each(&block) ⇒ Object (also: #each_pair)
- #each_key(&block) ⇒ Object
- #each_value(&block) ⇒ Object
- #empty? ⇒ Boolean
- #fetch(key, local_default = nil) ⇒ Object
- #has_value?(value) ⇒ Boolean
- #include?(key) ⇒ Boolean (also: #has_key?, #key?, #member?)
-
#initialize(lru_size = 1000) ⇒ ObjectCache
constructor
A new instance of ObjectCache.
- #keys ⇒ Object
- #size ⇒ Object (also: #length)
- #to_s ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(lru_size = 1000) ⇒ ObjectCache
Returns a new instance of ObjectCache.
21 22 23 24 25 26 |
# File 'lib/pdf/reader/object_cache.rb', line 21 def initialize(lru_size = 1000) @objects = {} @lru_cache = Hashery::LRUHash.new(lru_size.to_i) @hits = 0 @misses = 0 end |
Instance Attribute Details
#hits ⇒ Object (readonly)
Returns the value of attribute hits.
19 20 21 |
# File 'lib/pdf/reader/object_cache.rb', line 19 def hits @hits end |
#misses ⇒ Object (readonly)
Returns the value of attribute misses.
19 20 21 |
# File 'lib/pdf/reader/object_cache.rb', line 19 def misses @misses end |
Instance Method Details
#[](key) ⇒ Object
28 29 30 31 |
# File 'lib/pdf/reader/object_cache.rb', line 28 def [](key) update_stats(key) @objects[key] || @lru_cache[key] end |
#[]=(key, value) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/pdf/reader/object_cache.rb', line 33 def []=(key, value) if cacheable?(value) @objects[key] = value else @lru_cache[key] = value end end |
#each(&block) ⇒ Object Also known as: each_pair
46 47 48 49 |
# File 'lib/pdf/reader/object_cache.rb', line 46 def each(&block) @objects.each(&block) @lru_cache.each(&block) end |
#each_key(&block) ⇒ Object
52 53 54 55 |
# File 'lib/pdf/reader/object_cache.rb', line 52 def each_key(&block) @objects.each_key(&block) @lru_cache.each_key(&block) end |
#each_value(&block) ⇒ Object
57 58 59 60 |
# File 'lib/pdf/reader/object_cache.rb', line 57 def each_value(&block) @objects.each_value(&block) @lru_cache.each_value(&block) end |
#empty? ⇒ Boolean
67 68 69 |
# File 'lib/pdf/reader/object_cache.rb', line 67 def empty? @objects.empty? && @lru_cache.empty? end |
#fetch(key, local_default = nil) ⇒ Object
41 42 43 44 |
# File 'lib/pdf/reader/object_cache.rb', line 41 def fetch(key, local_default = nil) update_stats(key) @objects[key] || @lru_cache.fetch(key, local_default) end |
#has_value?(value) ⇒ Boolean
78 79 80 |
# File 'lib/pdf/reader/object_cache.rb', line 78 def has_value?(value) @objects.has_value?(value) || @lru_cache.has_value?(value) end |
#include?(key) ⇒ Boolean Also known as: has_key?, key?, member?
71 72 73 |
# File 'lib/pdf/reader/object_cache.rb', line 71 def include?(key) @objects.include?(key) || @lru_cache.include?(key) end |
#keys ⇒ Object
86 87 88 |
# File 'lib/pdf/reader/object_cache.rb', line 86 def keys @objects.keys + @lru_cache.keys end |
#size ⇒ Object Also known as: length
62 63 64 |
# File 'lib/pdf/reader/object_cache.rb', line 62 def size @objects.size + @lru_cache.size end |
#to_s ⇒ Object
82 83 84 |
# File 'lib/pdf/reader/object_cache.rb', line 82 def to_s "<PDF::Reader::ObjectCache size: #{self.size}>" end |
#values ⇒ Object
90 91 92 |
# File 'lib/pdf/reader/object_cache.rb', line 90 def values @objects.values + @lru_cache.values end |