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