Class: PDF::Reader::SynchronizedCache

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeSynchronizedCache

Returns a new instance of SynchronizedCache.



23
24
25
26
# File 'lib/pdf/reader/synchronized_cache.rb', line 23

def initialize
  @cache = {}
  @mutex = Mutex.new
end

Instance Method Details

#[](key) ⇒ Object



27
28
29
# File 'lib/pdf/reader/synchronized_cache.rb', line 27

def [](key)
  @mutex.synchronize { @cache[key] }
end

#[]=(key, value) ⇒ Object



30
31
32
# File 'lib/pdf/reader/synchronized_cache.rb', line 30

def []=(key,value)
  @mutex.synchronize { @cache[key] = value }
end