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