TermUI.Layout.Cache (TermUI v0.2.0)
View SourceLayout cache with LRU eviction for caching constraint solver results.
The cache stores solved layouts keyed by constraint hash and dimensions, providing O(1) lookup for unchanged layouts. LRU eviction keeps memory bounded while maintaining frequently-used layouts.
Usage
# Start cache (typically in supervision tree)
Cache.start_link(max_size: 1000)
# Cached solve
rects = Cache.solve(constraints, area)
# Statistics
stats = Cache.stats()
# => %{size: 150, hits: 1234, misses: 56, hit_rate: 0.956}
# Clear on resize
Cache.clear()Configuration
:max_size- Maximum entries before eviction (default 500):eviction_count- Entries to remove per eviction (default 50)
Summary
Functions
Returns a specification to start this module under a supervisor.
Clears all cache entries.
Forces eviction synchronously. Useful for testing.
Inserts a result into the cache.
Invalidates a specific cache entry.
Invalidates cache entries matching constraints.
Looks up a cached result by key.
Resets cache statistics.
Returns the current cache size.
Solves constraints with automatic caching.
Solves constraints without caching.
Starts the layout cache.
Returns cache statistics.
Warms the cache with common layouts.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
Clears all cache entries.
Call this on terminal resize.
Forces eviction synchronously. Useful for testing.
Inserts a result into the cache.
Triggers eviction if cache exceeds max size.
Invalidates a specific cache entry.
Invalidates cache entries matching constraints.
Useful when a component's constraints change.
Looks up a cached result by key.
Returns {:ok, result} if found, :miss otherwise.
Resets cache statistics.
Returns the current cache size.
Solves constraints with automatic caching.
Checks cache first, falls back to solver on miss.
Parameters
constraints- list of constraintsarea- bounding rectangleopts- solver options (direction, gap, etc.)
Returns
List of positioned rectangles.
Solves constraints without caching.
Use for testing or when caching is not desired.
Starts the layout cache.
Options
:max_size- Maximum cache entries (default 500):eviction_count- Entries to remove per eviction (default 50):name- GenServer name (default MODULE)
Returns cache statistics.
Returns
Map with:
:size- current entry count:hits- total cache hits:misses- total cache misses:hit_rate- hits / (hits + misses)
Warms the cache with common layouts.
Parameters
layouts- list of{constraints, area, opts}tuples