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