Class: TemplateRenderer::PartialCache
- Inherits:
-
Object
- Object
- TemplateRenderer::PartialCache
- Defined in:
- lib/generators/template_renderer/partial_cache.rb
Overview
Caches compiled ERB template objects with mtime-based invalidation
Cache structure:
cache_key => { erb: ERB_object, mtime: Time, path: String }
Cache keys include trim_mode to support different whitespace handling:
"screenshot:-" => ERB object with trim_mode: '-'
"screenshot:" => ERB object with no trim_mode
Performance: ~10x speedup on cached renders (135ms → ~13.5ms)
Instance Attribute Summary collapse
-
#batch_mode ⇒ Object
Set to true during batch generation to skip File.mtime syscalls on cache hits.
Instance Method Summary collapse
-
#clear ⇒ Object
Clear the entire cache (useful for testing).
-
#initialize(generator_class) ⇒ PartialCache
constructor
A new instance of PartialCache.
-
#render_partial(name, binding, options = {}) ⇒ String
Render a partial with caching.
-
#stats ⇒ Object
Get cache statistics (useful for debugging/monitoring).
Constructor Details
#initialize(generator_class) ⇒ PartialCache
Returns a new instance of PartialCache.
22 23 24 25 26 |
# File 'lib/generators/template_renderer/partial_cache.rb', line 22 def initialize(generator_class) @cache = {} @resolver = PartialResolver.new(generator_class) @batch_mode = false end |
Instance Attribute Details
#batch_mode ⇒ Object
Set to true during batch generation to skip File.mtime syscalls on cache hits
20 21 22 |
# File 'lib/generators/template_renderer/partial_cache.rb', line 20 def batch_mode @batch_mode end |
Instance Method Details
#clear ⇒ Object
Clear the entire cache (useful for testing)
68 69 70 |
# File 'lib/generators/template_renderer/partial_cache.rb', line 68 def clear @cache.clear end |
#render_partial(name, binding, options = {}) ⇒ String
Render a partial with caching
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/generators/template_renderer/partial_cache.rb', line 37 def render_partial(name, binding, = {}) trim_mode = [:trim_mode] cache_key = build_cache_key(name, trim_mode) # Resolve the partial path path = @resolver.resolve(name, binding) # Get from cache or compile erb = get_or_compile(cache_key, path, trim_mode) # Render with provided binding erb.result(binding) rescue Errno::ENOENT => e raise TemplateNotFoundError.new( "Partial '#{name}' not found", partial_name: name, searched_paths: @resolver.search_paths(name, binding), original_error: e ) rescue TemplateError raise rescue StandardError => e # Catch ERB syntax errors or other rendering issues raise TemplateRenderError.new( e., partial_name: name, original_error: e ) end |
#stats ⇒ Object
Get cache statistics (useful for debugging/monitoring)
73 74 75 76 77 78 79 |
# File 'lib/generators/template_renderer/partial_cache.rb', line 73 def stats { size: @cache.size, entries: @cache.keys, memory_estimate: estimate_cache_size } end |