Open History to see the last commit
1---
2id: QueryCache
3title: QueryCache
4---
5β
6Defined in: packages/query-core/dist-ts/src/queryCache.d.ts:85
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/dist-ts/src/queryCache.d.ts:88
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/dist-ts/src/queryCache.d.ts:87
64β
65***
66β
67### listeners
68β
69```ts
70protected listeners: Set<QueryCacheListener>;
71```
72β
73Defined in: packages/query-core/dist-ts/src/subscribable.d.ts:2
74β
75#### Inherited from
76β
77```ts
78Subscribable.listeners
79```
80β
81## Methods
82β
83### build()
84β
85```ts
86build<TQueryFnData, TError, TData, TQueryKey>(
87 client: QueryClient,
88 options: WithRequired<QueryOptions<TQueryFnData, TError, TData, TQueryKey, never>, "queryKey">,
89state?: QueryState<TData, TError>): Query<TQueryFnData, TError, TData, TQueryKey>;
90```
91β
92Defined in: packages/query-core/dist-ts/src/queryCache.d.ts:105
93β
94Returns the existing `Query` instance for the given options' `queryKey`/`queryHash`, or
95builds and adds a new one to the cache if none exists yet. Used by framework adapters and
96plugins (e.g. broadcast/persistence) that need to get-or-create a `Query` directly, bypassing
97the reactive `QueryObserver` machinery.
98β
99#### Type Parameters
100β
101##### TQueryFnData
102β
103`TQueryFnData` = `unknown`
104β
105##### TError
106β
107`TError` = `Error`
108β
109##### TData
110β
111`TData` = `TQueryFnData`
112β
113##### TQueryKey
114β
115`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
116β
117#### Parameters
118β
119##### client
120β
121[`QueryClient`](QueryClient.md)
122β
123##### options
124β
125[`WithRequired`](../type-aliases/WithRequired.md)\<[`QueryOptions`](../interfaces/QueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `never`\>, `"queryKey"`\>
126β
127##### state?
128β
129[`QueryState`](../interfaces/QueryState.md)\<`TData`, `TError`\>
130β
131#### Returns
132β
133[`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
134β
135#### Example
136β
137```ts
138const queryCache = queryClient.getQueryCache()
139β
140const query = queryCache.build(queryClient, {
141 queryKey: ['posts'],
142 queryFn: fetchPosts,
143})
144```
145β
146***
147β
148### clear()
149β
150```ts
151clear(): void;
152```
153β
154Defined in: packages/query-core/dist-ts/src/queryCache.d.ts:135
155β
156Removes all queries from the cache.
157β
158#### Returns
159β
160`void`
161β
162#### Example
163β
164```ts
165const queryCache = queryClient.getQueryCache()
166β
167queryCache.clear()
168```
169β
170***
171β
172### find()
173β
174```ts
175find<TQueryFnData, TError, TData>(filters: WithRequired<QueryFilters<readonly unknown[]>, "queryKey">):
176 | Query<TQueryFnData, TError, TData, readonly unknown[]>
177 | undefined;
178```
179β
180Defined in: packages/query-core/dist-ts/src/queryCache.d.ts:180
181β
182A slightly more advanced method that can be used to get an existing query instance from the
183cache. This instance not only contains all the state for the query, but all of the instances,
184and underlying guts of the query as well. If the query does not exist, `undefined` is
185returned.
186β
187This is not typically needed for most applications, but can come in handy when needing more
188information about a query in rare scenarios (e.g. looking at `query.state.dataUpdatedAt` to
189decide whether a query is fresh enough to be used as an initial value).
190β
191#### Type Parameters
192β
193##### TQueryFnData
194β
195`TQueryFnData` = `unknown`
196β
197##### TError
198β
199`TError` = `Error`
200β
201##### TData
202β
203`TData` = `TQueryFnData`
204β
205#### Parameters
206β
207##### filters
208β
209[`WithRequired`](../type-aliases/WithRequired.md)\<[`QueryFilters`](../interfaces/QueryFilters.md)\<readonly `unknown`[]\>, `"queryKey"`\>
210β
211#### Returns
212β
213 \| [`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, readonly `unknown`[]\>
214 \| `undefined`
215β
216#### See
217β
218[QueryCache#findAll](#findall)
219β
220#### Example
221β
222```ts
223const queryCache = queryClient.getQueryCache()
224β
225const query = queryCache.find({ queryKey: ['posts'] })
226```
227β
228***
229β
230### findAll()
231β
232```ts
233findAll(filters?: QueryFilters<any>): Query<unknown, Error, unknown, readonly unknown[]>[];
234```
235β
236Defined in: packages/query-core/dist-ts/src/queryCache.d.ts:196
237β
238An even more advanced method that can be used to get existing query instances from the cache
239that partially match a query key. If no queries match, an empty array is returned.
240β
241This is not typically needed for most applications, but can come in handy when needing more
242information about queries in rare scenarios.
243β
244#### Parameters
245β
246##### filters?
247β
248[`QueryFilters`](../interfaces/QueryFilters.md)\<`any`\>
249β
250#### Returns
251β
252[`Query`](Query.md)\<`unknown`, `Error`, `unknown`, readonly `unknown`[]\>[]
253β
254#### See
255β
256[QueryCache#find](#find)
257β
258#### Example
259β
260```ts
261const queryCache = queryClient.getQueryCache()
262β
263const queries = queryCache.findAll({ queryKey: ['posts'] })
264```
265β
266***
267β
268### get()
269β
270```ts
271get<TQueryFnData, TError, TData, TQueryKey>(queryHash: string):
272 | Query<TQueryFnData, TError, TData, TQueryKey>
273 | undefined;
274```
275β
276Defined in: packages/query-core/dist-ts/src/queryCache.d.ts:150
277β
278Returns the `Query` instance stored under the given `queryHash`, or `undefined` if none
279exists. Unlike [QueryCache#find](#find), this looks up by the already-computed hash rather
280than by `QueryFilters`. Used by plugins (e.g. broadcast/hydration) that already have a hash
281to look up directly.
282β
283#### Type Parameters
284β
285##### TQueryFnData
286β
287`TQueryFnData` = `unknown`
288β
289##### TError
290β
291`TError` = `Error`
292β
293##### TData
294β
295`TData` = `TQueryFnData`
296β
297##### TQueryKey
298β
299`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
300β
301#### Parameters
302β
303##### queryHash
304β
305`string`
306β
307#### Returns
308β
309 \| [`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
310 \| `undefined`
311β
312#### Example
313β
314```ts
315const queryCache = queryClient.getQueryCache()
316const queryHash = hashKey(['posts'])
317β
318const query = queryCache.get(queryHash)
319```
320β
321***
322β
323### getAll()
324β
325```ts
326getAll(): Query<unknown, Error, unknown, readonly unknown[]>[];
327```
328β
329Defined in: packages/query-core/dist-ts/src/queryCache.d.ts:161
330β
331Returns all queries within the cache.
332β
333#### Returns
334β
335[`Query`](Query.md)\<`unknown`, `Error`, `unknown`, readonly `unknown`[]\>[]
336β
337#### Example
338β
339```ts
340const queryCache = queryClient.getQueryCache()
341β
342const queries = queryCache.getAll()
343```
344β
345***
346β
347### hasListeners()
348β
349```ts
350hasListeners(): boolean;
351```
352β
353Defined in: packages/query-core/dist-ts/src/subscribable.d.ts:5
354β
355#### Returns
356β
357`boolean`
358β
359#### Inherited from
360β
361```ts
362Subscribable.hasListeners
363```
364β
365***
366β
367### onSubscribe()
368β
369```ts
370protected onSubscribe(): void;
371```
372β
373Defined in: packages/query-core/dist-ts/src/subscribable.d.ts:6
374β
375#### Returns
376β
377`void`
378β
379#### Inherited from
380β
381```ts
382Subscribable.onSubscribe
383```
384β
385***
386β
387### onUnsubscribe()
388β
389```ts
390protected onUnsubscribe(): void;
391```
392β
393Defined in: packages/query-core/dist-ts/src/subscribable.d.ts:7
394β
395#### Returns
396β
397`void`
398β
399#### Inherited from
400β
401```ts
402Subscribable.onUnsubscribe
403```
404β
405***
406β
407### remove()
408β
409```ts
410remove(query: Query<any, any, any, any>): void;
411```
412β
413Defined in: packages/query-core/dist-ts/src/queryCache.d.ts:124
414β
415Destroys the given `Query` and removes it from the cache, notifying subscribers with a
416`'removed'` event. A no-op if the query is no longer the one currently stored under its hash
417(e.g. it was already replaced). Used by plugins (e.g. the broadcast client) that mirror
418removals across `QueryCache` instances.
419β
420#### Parameters
421β
422##### query
423β
424[`Query`](Query.md)\<`any`, `any`, `any`, `any`\>
425β
426#### Returns
427β
428`void`
429β
430#### Example
431β
432```ts
433const queryCache = queryClient.getQueryCache()
434const query = queryCache.find({ queryKey: ['posts'] })
435β
436if (query) {
437 queryCache.remove(query)
438}
439```
440β
441***
442β
443### subscribe()
444β
445```ts
446subscribe(listener: QueryCacheListener): () => void;
447```
448β
449Defined in: packages/query-core/dist-ts/src/subscribable.d.ts:4
450β
451#### Parameters
452β
453##### listener
454β
455`QueryCacheListener`
456β
457Called on each update, with whatever the subclass passes to its subscribers.
458β
459#### Returns
460β
461```ts
462(): void;
463```
464β
465##### Returns
466β
467`void`
468β
469#### Example
470β
471```ts
472const unsubscribe = subscribable.subscribe(() => {
473 // react to the update
474})
475β
476unsubscribe()
477```
478β
479#### Inherited from
480β
481```ts
482Subscribable.subscribe
483```