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***
66β
67### listeners
68β
69```ts
70protected listeners: Set<QueryCacheListener>;
71```
72β
73Defined in: [packages/query-core/src/subscribable.ts:7](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L7)
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/src/queryCache.ts:147](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L147)
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`
122β
123##### options
124β
125[`WithRequired`](../type-aliases/WithRequired.md)\<`QueryOptions`\<`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/src/queryCache.ts:228](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L228)
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/src/queryCache.ts:295](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L295)
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/src/queryCache.ts:320](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L320)
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/src/queryCache.ts:250](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L250)
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/src/queryCache.ts:273](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L273)
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/src/subscribable.ts:41](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L41)
354β
355Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
356β
357#### Returns
358β
359`boolean`
360β
361#### Inherited from
362β
363```ts
364Subscribable.hasListeners
365```
366β
367***
368β
369### onSubscribe()
370β
371```ts
372protected onSubscribe(): void;
373```
374β
375Defined in: [packages/query-core/src/subscribable.ts:45](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L45)
376β
377#### Returns
378β
379`void`
380β
381#### Inherited from
382β
383```ts
384Subscribable.onSubscribe
385```
386β
387***
388β
389### onUnsubscribe()
390β
391```ts
392protected onUnsubscribe(): void;
393```
394β
395Defined in: [packages/query-core/src/subscribable.ts:49](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L49)
396β
397#### Returns
398β
399`void`
400β
401#### Inherited from
402β
403```ts
404Subscribable.onUnsubscribe
405```
406β
407***
408β
409### remove()
410β
411```ts
412remove(query: Query<any, any, any, any>): void;
413```
414β
415Defined in: [packages/query-core/src/queryCache.ts:208](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L208)
416β
417Destroys the given `Query` and removes it from the cache, notifying subscribers with a
418`'removed'` event. A no-op if the query is no longer the one currently stored under its hash
419(e.g. it was already replaced). Used by plugins (e.g. the broadcast client) that mirror
420removals across `QueryCache` instances.
421β
422#### Parameters
423β
424##### query
425β
426[`Query`](Query.md)\<`any`, `any`, `any`, `any`\>
427β
428#### Returns
429β
430`void`
431β
432#### Example
433β
434```ts
435const queryCache = queryClient.getQueryCache()
436const query = queryCache.find({ queryKey: ['posts'] })
437β
438if (query) {
439 queryCache.remove(query)
440}
441```
442β
443***
444β
445### subscribe()
446β
447```ts
448subscribe(listener: QueryCacheListener): () => void;
449```
450β
451Defined in: [packages/query-core/src/subscribable.ts:27](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L27)
452β
453Registers a listener to be called on every update this object notifies about. Returns a function
454that removes the listener again β call it to stop listening. The base class never drops a listener
455on its own, though some subclasses clear all of theirs in `destroy()`.
456β
457#### Parameters
458β
459##### listener
460β
461`QueryCacheListener`
462β
463Called on each update, with whatever the subclass passes to its subscribers.
464β
465#### Returns
466β
467```ts
468(): void;
469```
470β
471##### Returns
472β
473`void`
474β
475#### Example
476β
477```ts
478const unsubscribe = subscribable.subscribe(() => {
479 // react to the update
480})
481β
482unsubscribe()
483```
484β
485#### Inherited from
486β
487```ts
488Subscribable.subscribe
489```