Class: PDF::Reader::SynchronizedCache
- Inherits:
-
Object
- Object
- PDF::Reader::SynchronizedCache
- Defined in:
- lib/pdf/reader/synchronized_cache.rb
Overview
Throughout the pdf-reader codebase, repeated calculations which can benefit from caching are made In some cases, caching and reusing results can not only save CPU cycles but also greatly reduce memory requirements But at the same time, we don’t want to throw away thread safety We have two interchangeable thread-safe cache implementations:
Instance Method Summary collapse
-
#[](key) ⇒ Object
: (Object) -> untyped.
-
#[]=(key, value) ⇒ Object
: (Object, (Object | nil)) -> untyped.
-
#initialize ⇒ SynchronizedCache
constructor
: () -> void.
Constructor Details
#initialize ⇒ SynchronizedCache
: () -> void
24 25 26 27 |
# File 'lib/pdf/reader/synchronized_cache.rb', line 24 def initialize @cache = {} #: Hash[Object, untyped] @mutex = Mutex.new #: Mutex end |
Instance Method Details
#[](key) ⇒ Object
: (Object) -> untyped
29 30 31 |
# File 'lib/pdf/reader/synchronized_cache.rb', line 29 def [](key) @mutex.synchronize { @cache[key] } end |
#[]=(key, value) ⇒ Object
: (Object, (Object | nil)) -> untyped
33 34 35 |
# File 'lib/pdf/reader/synchronized_cache.rb', line 33 def []=(key,value) @mutex.synchronize { @cache[key] = value } end |