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 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.
19 20 21 22 |
# File 'lib/generators/template_renderer/partial_cache.rb', line 19 def initialize(generator_class) @cache = {} @resolver = PartialResolver.new(generator_class) end |
Instance Method Details
#clear ⇒ Object
Clear the entire cache (useful for testing)
64 65 66 |
# File 'lib/generators/template_renderer/partial_cache.rb', line 64 def clear @cache.clear end |
#render_partial(name, binding, options = {}) ⇒ String
Render a partial with caching
33 34 35 36 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 |
# File 'lib/generators/template_renderer/partial_cache.rb', line 33 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)
69 70 71 72 73 74 75 |
# File 'lib/generators/template_renderer/partial_cache.rb', line 69 def stats { size: @cache.size, entries: @cache.keys, memory_estimate: estimate_cache_size } end |