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.
20 21 22 23 24 25 |
# File 'lib/pdf/reader/object_cache.rb', line 20 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.
18 19 20 |
# File 'lib/pdf/reader/object_cache.rb', line 18 def hits @hits end |
#misses ⇒ Object (readonly)
Returns the value of attribute misses.
18 19 20 |
# File 'lib/pdf/reader/object_cache.rb', line 18 def misses @misses end |
Instance Method Details
#[](key) ⇒ Object
27 28 29 30 |
# File 'lib/pdf/reader/object_cache.rb', line 27 def [](key) update_stats(key) @objects[key] || @lru_cache[key] end |
#[]=(key, value) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/pdf/reader/object_cache.rb', line 32 def []=(key, value) if cacheable?(value) @objects[key] = value else @lru_cache[key] = value end end |
#each(&block) ⇒ Object Also known as: each_pair
45 46 47 48 |
# File 'lib/pdf/reader/object_cache.rb', line 45 def each(&block) @objects.each(&block) @lru_cache.each(&block) end |
#each_key(&block) ⇒ Object
51 52 53 54 |
# File 'lib/pdf/reader/object_cache.rb', line 51 def each_key(&block) @objects.each_key(&block) @lru_cache.each_key(&block) end |
#each_value(&block) ⇒ Object
56 57 58 59 |
# File 'lib/pdf/reader/object_cache.rb', line 56 def each_value(&block) @objects.each_value(&block) @lru_cache.each_value(&block) end |
#empty? ⇒ Boolean
66 67 68 |
# File 'lib/pdf/reader/object_cache.rb', line 66 def empty? @objects.empty? && @lru_cache.empty? end |
#fetch(key, local_default = nil) ⇒ Object
40 41 42 43 |
# File 'lib/pdf/reader/object_cache.rb', line 40 def fetch(key, local_default = nil) update_stats(key) @objects[key] || @lru_cache.fetch(key, local_default) end |
#has_value?(value) ⇒ Boolean
77 78 79 |
# File 'lib/pdf/reader/object_cache.rb', line 77 def has_value?(value) @objects.has_value?(value) || @lru_cache.has_value?(value) end |
#include?(key) ⇒ Boolean Also known as: has_key?, key?, member?
70 71 72 |
# File 'lib/pdf/reader/object_cache.rb', line 70 def include?(key) @objects.include?(key) || @lru_cache.include?(key) end |
#keys ⇒ Object
85 86 87 |
# File 'lib/pdf/reader/object_cache.rb', line 85 def keys @objects.keys + @lru_cache.keys end |
#size ⇒ Object Also known as: length
61 62 63 |
# File 'lib/pdf/reader/object_cache.rb', line 61 def size @objects.size + @lru_cache.size end |
#to_s ⇒ Object
81 82 83 |
# File 'lib/pdf/reader/object_cache.rb', line 81 def to_s "<PDF::Reader::ObjectCache size: #{self.size}>" end |
#values ⇒ Object
89 90 91 |
# File 'lib/pdf/reader/object_cache.rb', line 89 def values @objects.values + @lru_cache.values end |