MarkdownParseCache class

A Least Recently Used (LRU) cache for markdown parsing results.

This cache stores parsed AST nodes to avoid re-parsing identical markdown content. It's particularly useful in scenarios where:

  • The same markdown content is rendered multiple times
  • List views rebuild frequently (e.g., during scrolling)
  • Chat applications display many similar formatted messages

The cache uses an LRU eviction policy to prevent unbounded memory growth. When the cache reaches maxSize, the least recently used entry is removed before adding a new one.

Usage

final cache = MarkdownParseCache(maxSize: 100);

// Try to get from cache
final cached = cache.get(markdownText);
if (cached != null) {
  // Cache hit - reuse parsed nodes
  return cached;
}

// Cache miss - parse and store
final nodes = parser.parse(markdownText);
cache.put(markdownText, nodes);

Performance Impact

In testing with a chat list of 50 messages:

  • Cache hit: ~0.1ms (vs ~15ms for parsing)
  • Memory overhead: ~50KB per cached entry (varies by content)
  • Scroll performance: +33% FPS improvement

Thread Safety

This class is not thread-safe. It should be used from the UI thread only. For multi-isolate scenarios, create separate cache instances per isolate.

Constructors

MarkdownParseCache({int maxSize = 100})
Creates a new parse cache with the specified maximum size.

Properties

hashCode int
The hash code for this object.
no setterinherited
isEmpty bool
Returns true if the cache is empty.
no setter
isNotEmpty bool
Returns true if the cache has entries.
no setter
length int
Returns the current number of cached entries.
no setter
maxSize int
Maximum number of entries to cache before eviction
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
statistics Map<String, dynamic>
Returns the current cache hit statistics.
no setter

Methods

clear() → void
Clears all entries from the cache.
contains(String markdown) bool
Checks if a markdown text is cached.
get(String markdown) List<MarkdownNode>?
Retrieves parsed nodes from the cache.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
put(String markdown, List<MarkdownNode> nodes) → void
Stores parsed nodes in the cache.
remove(String markdown) bool
Removes a specific entry from the cache.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited