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