Open History to see the last commit
1---
2id: QueryCache
3title: QueryCache
4---
5β
6Defined in: [packages/query-core/src/queryCache.ts:123](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L123)
7β
8The `QueryCache` is the storage mechanism for TanStack Query. It stores all the data, meta
9information, and state of the queries it contains.
10β
11Normally, you will not interact with the `QueryCache` directly and instead use a `QueryClient`
12for a specific cache. You can subscribe to it (inherited from `Subscribable`) to be informed of
13safe/known updates to the cache, such as queries being added, removed, or updated β updates made
14outside of the cache's own tracked mechanisms (e.g. mutating a query's state object directly) do
15not notify subscribers.
16β
17## Example
18β
19```ts
20const unsubscribe = queryCache.subscribe((event) => {
21 console.log(event.type, event.query)
22})
23```
24β
25## Extends
26β
27- `Subscribable`\<`QueryCacheListener`\>
28β
29## Constructors
30β
31### Constructor
32β
33```ts
34new QueryCache(config: QueryCacheConfig): QueryCache;
35```
36β
37Defined in: [packages/query-core/src/queryCache.ts:126](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L126)
38β
39#### Parameters
40β
41##### config
42β
43[`QueryCacheConfig`](../interfaces/QueryCacheConfig.md) = `{}`
44β
45#### Returns
46β
47`QueryCache`
48β
49#### Overrides
50β
51```ts
52Subscribable<QueryCacheListener>.constructor
53```
54β
55## Properties
56β
57### config
58β
59```ts
60config: QueryCacheConfig = {};
61```
62β
63Defined in: [packages/query-core/src/queryCache.ts:126](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L126)
64β
65## Methods
66β
67### build()
68β
69```ts
70build<TQueryFnData, TError, TData, TQueryKey>(
71 client: QueryClient,
72 options: WithRequired<QueryOptions<TQueryFnData, TError, TData, TQueryKey, never>, "queryKey">,
73state?: QueryState<TData, TError>): Query<TQueryFnData, TError, TData, TQueryKey>;
74```
75β
76Defined in: [packages/query-core/src/queryCache.ts:147](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L147)
77β
78Returns the existing `Query` instance for the given options' `queryKey`/`queryHash`, or
79builds and adds a new one to the cache if none exists yet. Used by framework adapters and
80plugins (e.g. broadcast/persistence) that need to get-or-create a `Query` directly, bypassing
81the reactive `QueryObserver` machinery.
82β
83#### Type Parameters
84β
85##### TQueryFnData
86β
87`TQueryFnData` = `unknown`
88β
89##### TError
90β
91`TError` = `Error`
92β
93##### TData
94β
95`TData` = `TQueryFnData`
96β
97##### TQueryKey
98β
99`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
100β
101#### Parameters
102β
103##### client
104β
105[`QueryClient`](QueryClient.md)
106β
107##### options
108β
109[`WithRequired`](../type-aliases/WithRequired.md)\<[`QueryOptions`](../interfaces/QueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `never`\>, `"queryKey"`\>
110β
111##### state?
112β
113[`QueryState`](../interfaces/QueryState.md)\<`TData`, `TError`\>
114β
115#### Returns
116β
117[`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
118β
119#### Example
120β
121```ts
122const queryCache = queryClient.getQueryCache()
123β
124const query = queryCache.build(queryClient, {
125 queryKey: ['posts'],
126 queryFn: fetchPosts,
127})
128```
129β
130***
131β
132### clear()
133β
134```ts
135clear(): void;
136```
137β
138Defined in: [packages/query-core/src/queryCache.ts:228](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L228)
139β
140Removes all queries from the cache.
141β
142#### Returns
143β
144`void`
145β
146#### Example
147β
148```ts
149const queryCache = queryClient.getQueryCache()
150β
151queryCache.clear()
152```
153β
154***
155β
156### find()
157β
158```ts
159find<TQueryFnData, TError, TData>(filters: WithRequired<QueryFilters<readonly unknown[]>, "queryKey">):
160 | Query<TQueryFnData, TError, TData, readonly unknown[]>
161 | undefined;
162```
163β
164Defined in: [packages/query-core/src/queryCache.ts:294](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L294)
165β
166A slightly more advanced method that can be used to get an existing query instance from the
167cache. This instance not only contains all the state for the query, but all of the instances,
168and underlying guts of the query as well. If the query does not exist, `undefined` is
169returned.
170β
171This is not typically needed for most applications, but can come in handy when needing more
172information about a query in rare scenarios (e.g. looking at `query.state.dataUpdatedAt` to
173decide whether a query is fresh enough to be used as an initial value).
174β
175#### Type Parameters
176β
177##### TQueryFnData
178β
179`TQueryFnData` = `unknown`
180β
181##### TError
182β
183`TError` = `Error`
184β
185##### TData
186β
187`TData` = `TQueryFnData`
188β
189#### Parameters
190β
191##### filters
192β
193[`WithRequired`](../type-aliases/WithRequired.md)\<[`QueryFilters`](../interfaces/QueryFilters.md)\<readonly `unknown`[]\>, `"queryKey"`\>
194β
195#### Returns
196β
197 \| [`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, readonly `unknown`[]\>
198 \| `undefined`
199β
200#### See
201β
202[QueryCache#findAll](#findall)
203β
204#### Example
205β
206```ts
207const queryCache = queryClient.getQueryCache()
208β
209const query = queryCache.find({ queryKey: ['posts'] })
210```
211β
212***
213β
214### findAll()
215β
216```ts
217findAll(filters: QueryFilters<any>): Query<unknown, Error, unknown, readonly unknown[]>[];
218```
219β
220Defined in: [packages/query-core/src/queryCache.ts:319](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L319)
221β
222An even more advanced method that can be used to get existing query instances from the cache
223that partially match a query key. If no queries match, an empty array is returned.
224β
225This is not typically needed for most applications, but can come in handy when needing more
226information about queries in rare scenarios.
227β
228#### Parameters
229β
230##### filters
231β
232[`QueryFilters`](../interfaces/QueryFilters.md)\<`any`\> = `{}`
233β
234#### Returns
235β
236[`Query`](Query.md)\<`unknown`, `Error`, `unknown`, readonly `unknown`[]\>[]
237β
238#### See
239β
240[QueryCache#find](#find)
241β
242#### Example
243β
244```ts
245const queryCache = queryClient.getQueryCache()
246β
247const queries = queryCache.findAll({ queryKey: ['posts'] })
248```
249β
250***
251β
252### get()
253β
254```ts
255get<TQueryFnData, TError, TData, TQueryKey>(queryHash: string):
256 | Query<TQueryFnData, TError, TData, TQueryKey>
257 | undefined;
258```
259β
260Defined in: [packages/query-core/src/queryCache.ts:250](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L250)
261β
262Returns the `Query` instance stored under the given `queryHash`, or `undefined` if none
263exists. Unlike [QueryCache#find](#find), this looks up by the already-computed hash rather
264than by `QueryFilters`. Used by plugins (e.g. broadcast/hydration) that already have a hash
265to look up directly.
266β
267#### Type Parameters
268β
269##### TQueryFnData
270β
271`TQueryFnData` = `unknown`
272β
273##### TError
274β
275`TError` = `Error`
276β
277##### TData
278β
279`TData` = `TQueryFnData`
280β
281##### TQueryKey
282β
283`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
284β
285#### Parameters
286β
287##### queryHash
288β
289`string`
290β
291#### Returns
292β
293 \| [`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
294 \| `undefined`
295β
296#### Example
297β
298```ts
299const queryCache = queryClient.getQueryCache()
300const queryHash = hashKey(['posts'])
301β
302const query = queryCache.get(queryHash)
303```
304β
305***
306β
307### getAll()
308β
309```ts
310getAll(): Query<unknown, Error, unknown, readonly unknown[]>[];
311```
312β
313Defined in: [packages/query-core/src/queryCache.ts:272](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L272)
314β
315Returns all queries within the cache.
316β
317#### Returns
318β
319[`Query`](Query.md)\<`unknown`, `Error`, `unknown`, readonly `unknown`[]\>[]
320β
321#### Example
322β
323```ts
324const queryCache = queryClient.getQueryCache()
325β
326const queries = queryCache.getAll()
327```
328β
329***
330β
331### hasListeners()
332β
333```ts
334hasListeners(): boolean;
335```
336β
337Defined in: [packages/query-core/src/subscribable.ts:41](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L41)
338β
339Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
340β
341#### Returns
342β
343`boolean`
344β
345#### Inherited from
346β
347```ts
348Subscribable.hasListeners
349```
350β
351***
352β
353### remove()
354β
355```ts
356remove(query: Query<any, any, any, any>): void;
357```
358β
359Defined in: [packages/query-core/src/queryCache.ts:208](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L208)
360β
361Destroys the given `Query` and removes it from the cache, notifying subscribers with a
362`'removed'` event. A no-op if the query is no longer the one currently stored under its hash
363(e.g. it was already replaced). Used by plugins (e.g. the broadcast client) that mirror
364removals across `QueryCache` instances.
365β
366#### Parameters
367β
368##### query
369β
370[`Query`](Query.md)\<`any`, `any`, `any`, `any`\>
371β
372#### Returns
373β
374`void`
375β
376#### Example
377β
378```ts
379const queryCache = queryClient.getQueryCache()
380const query = queryCache.find({ queryKey: ['posts'] })
381β
382if (query) {
383 queryCache.remove(query)
384}
385```
386β
387***
388β
389### subscribe()
390β
391```ts
392subscribe(listener: QueryCacheListener): () => void;
393```
394β
395Defined in: [packages/query-core/src/subscribable.ts:27](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L27)
396β
397Registers a listener to be called on every update this object notifies about. Returns a function
398that removes the listener again β call it to stop listening. The base class never drops a listener
399on its own, though some subclasses clear all of theirs in `destroy()`.
400β
401#### Parameters
402β
403##### listener
404β
405`QueryCacheListener`
406β
407Called on each update, with whatever the subclass passes to its subscribers.
408β
409#### Returns
410β
411```ts
412(): void;
413```
414β
415##### Returns
416β
417`void`
418β
419#### Example
420β
421```ts
422const unsubscribe = subscribable.subscribe(() => {
423 // react to the update
424})
425β
426unsubscribe()
427```
428β
429#### Inherited from
430β
431```ts
432Subscribable.subscribe
433```