Inspect existing queries synchronously
Cache inspection
TanStack · Class
QueryCache stores TanStack Query's query data, metadata, and state. It supports creating, finding, clearing, and observing cached queries, but applications normally use QueryClient instead.
QueryCache is TanStack Query's storage layer for query data, metadata, and state. It can be constructed with configuration, queried synchronously, cleared, or observed for tracked cache changes, although most application code should use QueryClient instead.
new QueryCache(config?: QueryCacheConfig): QueryCache;| Name | Type | Description |
|---|---|---|
config | QueryCacheConfig | Optional configuration for cache behavior and callbacks. |
A QueryCache instance; build returns an existing or newly created Query, while subscription methods provide an unsubscribe function.
import { QueryCache } from '@tanstack/query-core'
const queryCache = new QueryCache({
onError: (error, query) => {
console.error(query.queryKey, error)
},
})
const unsubscribe = queryCache.subscribe((event) => {
console.log(event.type)
})
queryCache.clear()
unsubscribe()Creates a cache with an error callback, observes cache events, clears it, and then removes the subscription.
Cache inspection
Cache lifecycle
Instrumentation
Core integrations
QueryCache stores the data, metadata, and state for the queries it contains. QueryClient is normally the application-facing interface for working with a specific cache.
Follow-up: How can an application observe cache changes? Use subscribe; it reports safe, tracked updates such as queries being added, removed, or updated.
The constructor accepts an optional QueryCacheConfig object. The cache can then expose operations such as build, find, findAll, clear, and subscribe.
Follow-up: What does build do when the requested query is absent? It creates a Query instance and adds it to the cache.
build checks the supplied query options' queryKey or queryHash. It returns the existing matching Query, or creates and stores a new one when no match exists.
Follow-up: Why might direct QueryCache access be inappropriate for UI code? QueryClient and framework integrations provide the intended higher-level interaction model.
QueryClientQuerySubscribablequeryClient.getQueryCache()