Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

QueryCache.md

489 lines10.1 KB

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