Class: Prawn::SynchronizedCache
- Inherits:
-
Object
- Object
- Prawn::SynchronizedCache
- Defined in:
- lib/prawn/utilities.rb
Overview
Throughout the Prawn 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
- #[]=(key, value) ⇒ Object
-
#initialize ⇒ SynchronizedCache
constructor
As an optimization, this could access the hash directly on VMs with a global interpreter lock (like MRI).
Constructor Details
#initialize ⇒ SynchronizedCache
As an optimization, this could access the hash directly on VMs with a global interpreter lock (like MRI)
21 22 23 24 |
# File 'lib/prawn/utilities.rb', line 21 def initialize @cache = {} @mutex = Mutex.new end |
Instance Method Details
#[](key) ⇒ Object
25 26 27 |
# File 'lib/prawn/utilities.rb', line 25 def [](key) @mutex.synchronize { @cache[key] } end |
#[]=(key, value) ⇒ Object
28 29 30 |
# File 'lib/prawn/utilities.rb', line 28 def []=(key,value) @mutex.synchronize { @cache[key] = value } end |