Class: WickedPdf::AssetCache

Inherits:
Object
  • Object
show all
Defined in:
lib/wicked_pdf/asset_cache.rb

Overview

Process-wide memo for the output of the inlining asset helpers (wicked_pdf_asset_base64, wicked_pdf_stylesheet_link_tag, wicked_pdf_javascript_include_tag).

Reading, concatenating and base64-encoding assets is deterministic for a given deployment, but the helpers are invoked once per rendered document — expensive when many documents are rendered in one request (e.g. printing a batch). Enable with config.cache_assets = true; disabled by default so environments that recompile assets on change (development) stay live.

Cached values are frozen so a stale reference can't be mutated in place.

Instance Method Summary collapse

Constructor Details

#initializeAssetCache

Returns a new instance of AssetCache.



14
15
16
17
# File 'lib/wicked_pdf/asset_cache.rb', line 14

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

Instance Method Details

#clearObject



25
26
27
# File 'lib/wicked_pdf/asset_cache.rb', line 25

def clear
  @mutex.synchronize { @store.clear }
end

#fetch(key) ⇒ Object



19
20
21
22
23
# File 'lib/wicked_pdf/asset_cache.rb', line 19

def fetch(key)
  @mutex.synchronize do
    @store.fetch(key) { @store[key] = yield.freeze }
  end
end

#sizeObject



29
30
31
# File 'lib/wicked_pdf/asset_cache.rb', line 29

def size
  @mutex.synchronize { @store.size }
end