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.
22 23 24 25 26 27 |
# File 'lib/pdf/reader/object_cache.rb', line 22 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.
20 21 22 |
# File 'lib/pdf/reader/object_cache.rb', line 20 def hits @hits end |
#misses ⇒ Object (readonly)
Returns the value of attribute misses.
20 21 22 |
# File 'lib/pdf/reader/object_cache.rb', line 20 def misses @misses end |
Instance Method Details
#[](key) ⇒ Object
29 30 31 32 |
# File 'lib/pdf/reader/object_cache.rb', line 29 def [](key) update_stats(key) @objects[key] || @lru_cache[key] end |
#[]=(key, value) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/pdf/reader/object_cache.rb', line 34 def []=(key, value) if cacheable?(value) @objects[key] = value else @lru_cache[key] = value end end |
#each(&block) ⇒ Object Also known as: each_pair
47 48 49 50 |
# File 'lib/pdf/reader/object_cache.rb', line 47 def each(&block) @objects.each(&block) @lru_cache.each(&block) end |
#each_key(&block) ⇒ Object
53 54 55 56 |
# File 'lib/pdf/reader/object_cache.rb', line 53 def each_key(&block) @objects.each_key(&block) @lru_cache.each_key(&block) end |
#each_value(&block) ⇒ Object
58 59 60 61 |
# File 'lib/pdf/reader/object_cache.rb', line 58 def each_value(&block) @objects.each_value(&block) @lru_cache.each_value(&block) end |
#empty? ⇒ Boolean
68 69 70 |
# File 'lib/pdf/reader/object_cache.rb', line 68 def empty? @objects.empty? && @lru_cache.empty? end |
#fetch(key, local_default = nil) ⇒ Object
42 43 44 45 |
# File 'lib/pdf/reader/object_cache.rb', line 42 def fetch(key, local_default = nil) update_stats(key) @objects[key] || @lru_cache.fetch(key, local_default) end |
#has_value?(value) ⇒ Boolean
79 80 81 |
# File 'lib/pdf/reader/object_cache.rb', line 79 def has_value?(value) @objects.has_value?(value) || @lru_cache.has_value?(value) end |
#include?(key) ⇒ Boolean Also known as: has_key?, key?, member?
72 73 74 |
# File 'lib/pdf/reader/object_cache.rb', line 72 def include?(key) @objects.include?(key) || @lru_cache.include?(key) end |
#keys ⇒ Object
87 88 89 |
# File 'lib/pdf/reader/object_cache.rb', line 87 def keys @objects.keys + @lru_cache.keys end |
#size ⇒ Object Also known as: length
63 64 65 |
# File 'lib/pdf/reader/object_cache.rb', line 63 def size @objects.size + @lru_cache.size end |
#to_s ⇒ Object
83 84 85 |
# File 'lib/pdf/reader/object_cache.rb', line 83 def to_s "<PDF::Reader::ObjectCache size: #{self.size}>" end |
#values ⇒ Object
91 92 93 |
# File 'lib/pdf/reader/object_cache.rb', line 91 def values @objects.values + @lru_cache.values end |