Class: WickedPdf::AssetCache
- Inherits:
-
Object
- Object
- WickedPdf::AssetCache
- 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
- #clear ⇒ Object
- #fetch(key) ⇒ Object
-
#initialize ⇒ AssetCache
constructor
A new instance of AssetCache.
- #size ⇒ Object
Constructor Details
#initialize ⇒ AssetCache
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
#clear ⇒ Object
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 |
#size ⇒ Object
29 30 31 |
# File 'lib/wicked_pdf/asset_cache.rb', line 29 def size @mutex.synchronize { @store.size } end |