ZenQueryCache class

Global cache manager for ZenQuery instances

Supports both global queries and scope-aware queries for flexible lifecycle management and cache isolation.

Handles:

  • Query deduplication
  • Cache invalidation
  • Memory management
  • Query coordination
  • Smart refetching (Focus/Reconnect)

Properties

hashCode → int
The hash code for this object.
no setterinherited
isOnline → bool
Whether the app is currently connected to the network
no setter
queries → List<ZenQuery>
Get all queries
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
useRealTimers → bool
Whether real timers are being used (false in test mode)
no setter

Methods

cancelQueries(ZenQueryFilter filter) → void
Cancel any in-flight fetch requests for all active queries matching filter.
clear() → void
Clear all queries and cache
clearCacheEntry(Object queryKey) → void
Clear cached data for queryKey while preserving any registered ZenQuery instance so rollbacks, invalidations, and subsequent refetches still work.
clearScope(String scopeId) → void
Clear all queries and stream queries in a specific scope
configureForTesting({bool useRealTimers = false}) → void
Configure timer behavior for testing
deduplicateFetch<T>(String queryKey, Future<T> fetcher()) → Future<T>
Deduplicate concurrent fetches for the same query
ensureLifecycleObserver() → void
Ensures this cache is subscribed to ZenLifecycleManager app lifecycle events. Called during construction and re-called by Zen.init after Zen.reset.
getAllQueries() → List<ZenQuery>
Get all queries (for lifecycle management)
getAllStreamQueries() → List<ZenStreamQuery>
Returns all registered ZenStreamQuery instances.
getCachedData<T>(Object queryKey) → T?
Get cached data for a query
getKeysByTag(String tag) → List<String>
Returns all query keys currently registered under tag.
getMatchingKeys(ZenQueryFilter filter) → List<String>
Returns all query keys matching filter across active queries and cached entries.
getQueries([ZenQueryFilter? filter]) → List<ZenQuery>
Returns all active queries matching filter.
getQueriesByTag(String tag) → List<ZenQuery>
Returns all live (non-disposed) queries that carry tag.
getQuery<T>(Object queryKey) → ZenQuery<T>?
Get query by key
getScopeQueries(String scopeId) → List<ZenQuery>
Get all queries in a specific scope
getScopeStats(String scopeId) → Map<String, dynamic>
Get statistics about scope queries
getStats() → Map<String, dynamic>
Get comprehensive cache statistics
getStreamQuery<T>(Object queryKey) → ZenStreamQuery<T>?
Retrieve an active ZenStreamQuery by key.
getTimestamp(Object queryKey) → DateTime?
Get cached timestamp for a query
hasCachedData(Object queryKey) → bool
Check if non-expired cached data exists for queryKey.
hydrate<T>(String key, ZenQueryConfig<T> config) → Future<T?>
Try to hydrate a query from storage
invalidateQueries([dynamic filterOrPredicate]) → void
Invalidate queries matching filterOrPredicate (mark as stale and refetch if active; resubscribes matching ZenStreamQuery instances).
invalidateQueriesByPattern(String pattern) → void
Invalidate all queries whose keys match a glob-style pattern.
invalidateQueriesByTag(String tag) → void
Invalidate all queries that carry tag.
invalidateQueriesWithPrefix(String prefix) → void
Invalidate all queries with a prefix
invalidateQuery(Object queryKey) → void
Invalidate query (mark as stale; resubscribes matching ZenStreamQuery)
invalidateScope(String scopeId) → void
Invalidate all queries in a specific scope
isFetching({ZenQueryFilter? filter, Object? queryKey, String? tag, bool predicate(ZenQuery query)?}) → bool
Returns whether queries are currently fetching.
isFetchingCount({ZenQueryFilter? filter, Object? queryKey, String? tag, bool predicate(ZenQuery query)?}) → int
Returns the count of queries currently fetching (optionally filtered by filter, queryKey, tag, or predicate).
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
prefetch<T>({required Object queryKey, required Future<T> fetcher(), Duration? staleTime, Duration? cacheTime}) → Future<T?>
Prefetch data and store it in the cache if it's not already fresh.
refetchQueries([dynamic filterOrPredicate]) → Future<void>
Refetch all queries matching filterOrPredicate.
refetchQueriesByPattern(String pattern) → Future<void>
Refetch all queries matching a glob-style pattern.
refetchQueriesByTag(String tag) → Future<void>
Refetch all queries that carry tag.
refetchQuery(Object queryKey) → Future<void>
Refetch a query by key
refetchScope(String scopeId) → Future<void>
Refetch all queries in a specific scope
register<T>(ZenQuery<T> query) → void
Register a query in the cache
registerRetryWhenOnline(ZenQuery query) → void
Register a query to retry its full fetch cycle when connectivity returns.
registerScoped<T>(ZenQuery<T> query, String scopedKey, String scopeId) → void
Register a scoped query with automatic scope tracking
registerScopedStream<T>(ZenStreamQuery<T> streamQuery, String scopedKey, String scopeId) → void
Register a scoped ZenStreamQuery with automatic scope tracking.
registerStream<T>(ZenStreamQuery<T> streamQuery) → void
Register a global ZenStreamQuery in the cache for lookup, tag indexing, invalidation, and DevTools visibility.
removeQueries(ZenQueryFilter filter) → void
Remove all queries and cache entries matching filter completely from the cache.
removeQuery(Object queryKey) → void
Remove query from cache
reset() → void
Complete reset of cache state, network subscription, online status, and storage. Called automatically by Zen.reset().
resetQueries(ZenQueryFilter filter) → void
Reset all active queries matching filter back to their initial/idle state.
setNetworkStream(Stream<bool> stream) → void
Set the network connectivity stream
setQueriesData<T>(ZenQueryFilter filter, T updater(T? oldData)) → void
Functionally update cached data for all queries matching filter.
setQueryData<T>(Object queryKey, T updater(T? oldData)) → void
Functionally update cached data for a single query.
setStorage(ZenStorage storage) → void
Set the global storage implementation for persistence
simulateLifecycleState(AppLifecycleState state) → void
Manually trigger a lifecycle change for testing purposes. Use this instead of didChangeAppLifecycleState in tests.
toString() → String
A string representation of this object.
inherited
unregister(String queryKey) → void
Unregister a query from the cache
unregisterRetryWhenOnline(ZenQuery query) → void
Unregister a query from the retry-when-online queue (e.g., on dispose).
unregisterStream(String cacheKey) → void
Unregister a ZenStreamQuery from the cache.
updateCache<T>(String queryKey, T data, DateTime timestamp) → void
Update cache for a query
updateStreamCache<T>(String cacheKey, T data, DateTime timestamp) → void
Mirror a ZenStreamQuery's latest event into the memory cache without spawning a background expiry timer (so widget tests using streams never fail with pending timers; expiration is still checked lazily via isExpired).

Operators

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

Static Properties

instance → ZenQueryCache
final