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