QueryCache<T extends Entity> class

A cache for query results with TTL and LRU eviction.

The cache stores results of previous queries and returns them for identical subsequent queries, improving performance for repeated or expensive queries.

Features

  • TTL Expiration: Entries automatically expire after a configurable duration
  • LRU Eviction: Least recently used entries are evicted when cache is full
  • Selective Invalidation: Only invalidate queries affected by mutations
  • Field Tracking: Track which fields each query depends on
  • Statistics: Monitor cache performance with hit/miss ratios

Thread Safety

This class is NOT thread-safe. External synchronization is required when accessed from multiple isolates or concurrent operations.

Constructors

QueryCache({QueryCacheConfig? config})
Creates a query cache with the given configuration.
QueryCache.withParams({int maxSize = 100, Duration defaultTtl = const Duration(minutes: 5), bool enableSelectiveInvalidation = true, bool collectStatistics = true})
Creates a query cache with individual parameters.
factory

Properties

cachedFields → Set<String>
Gets the fields that currently have cached queries.
no setter
config → QueryCacheConfig
The cache configuration.
final
hashCode → int
The hash code for this object.
no setterinherited
isEmpty → bool
Whether the cache is empty.
no setter
isNotEmpty → bool
Whether the cache is not empty.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
size → int
Number of entries currently in the cache.
no setter
statistics → CacheStatistics
Current cache statistics.
no setter

Methods

clear() → void
Clears all entries and resets statistics.
containsQuery(IQuery query) → bool
Checks if a query is currently cached and valid.
get(IQuery query) → List<T>?
Gets a cached result for the given query.
invalidateAll() → int
Invalidates all cached entries.
invalidateField(String field) → int
Invalidates all cached entries for queries using the specified field.
invalidateFields(Set<String> fields) → int
Invalidates all cached entries for queries using any of the specified fields.
invalidateQuery(IQuery query) → bool
Invalidates a specific cached query.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
put(IQuery query, List<T> results, {Duration? ttl}) → void
Caches a query result.
queriesForField(String field) → int
Gets the number of queries cached for a specific field.
removeExpired() → int
Removes all expired entries from the cache.
resetStatistics() → void
Resets statistics without clearing the cache.
toString() → String
A string representation of this object.
inherited

Operators

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