Open History to see the last commit
1---
2id: QueryCache
3title: QueryCache
4---
5β
6The `QueryCache` is the storage mechanism for TanStack Query. It stores all the data, meta information and state of queries it contains.
7β
8**Normally, you will not interact with the QueryCache directly and instead use the `QueryClient` for a specific cache.**
9β
10```tsx
11import { QueryCache } from '@tanstack/react-query'
12β
13const queryCache = new QueryCache({
14 onError: error => {
15 console.log(error)
16 },
17 onSuccess: data => {
18 console.log(data)
19 }
20})
21β
22const query = queryCache.find({ queryKey: ['posts'] })
23```
24β
25Its available methods are:
26β
27- [`find`](#querycachefind)
28- [`findAll`](#querycachefindall)
29- [`subscribe`](#querycachesubscribe)
30- [`clear`](#querycacheclear)
31β
32**Options**
33β
34- `onError?: (error: unknown, query: Query) => void`
35 - Optional
36 - This function will be called if some query encounters an error.
37- `onSuccess?: (data: unknown, query: Query) => void`
38 - Optional
39 - This function will be called if some query is successful.
40- `createStore?: () => QueryStore`
41 - Optional
42 - This function will be called to create the store that will be used to store the queries. By default, a `Map` is used.
43β
44## Global callbacks
45β
46The `onError` and `onSuccess` callbacks on the QueryCache can be used to handle these events on a global level. They are different to `defaultOptions` provided to the QueryClient because:
47- `defaultOptions` can be overridden by each Query - the global callbacks will **always** be called.
48- `defaultOptions` callbacks will be called once for each Observer, while the global callbacks will only be called once per Query.
49β
50## `queryCache.find`
51β
52`find` is a slightly more advanced synchronous method that can be used to get an existing query instance from the cache. This instance not only contains **all** the state for the query, but all of the instances, and underlying guts of the query as well. If the query does not exist, `undefined` will be returned.
53β
54> Note: This is not typically needed for most applications, but can come in handy when needing more information about a query in rare scenarios (eg. Looking at the query.state.dataUpdatedAt timestamp to decide whether a query is fresh enough to be used as an initial value)
55β
56```tsx
57const query = queryCache.find({ queryKey })
58```
59β
60**Options**
61β
62- `filters?: QueryFilters`: [Query Filters](../guides/filters#query-filters)
63β
64**Returns**
65β
66- `Query`
67 - The query instance from the cache
68β
69## `queryCache.findAll`
70β
71`findAll` is even more advanced synchronous method that can be used to get existing query instances from the cache that partially match query key. If queries do not exist, empty array will be returned.
72β
73> Note: This is not typically needed for most applications, but can come in handy when needing more information about a query in rare scenarios
74β
75```tsx
76const queries = queryCache.findAll({ queryKey })
77```
78β
79**Options**
80β
81- `filters?: QueryFilters`: [Query Filters](../guides/filters#query-filters)
82β
83**Returns**
84β
85- `Query[]`
86 - Query instances from the cache
87β
88## `queryCache.subscribe`
89β
90The `subscribe` method can be used to subscribe to the query cache as a whole and be informed of safe/known updates to the cache like query states changing or queries being updated, added or removed
91β
92```tsx
93const callback = event => {
94 console.log(event.type, event.query)
95}
96β
97const unsubscribe = queryCache.subscribe(callback)
98```
99β
100**Options**
101β
102- `callback: (event: QueryCacheNotifyEvent) => void`
103 - This function will be called with the query cache any time it is updated via its tracked update mechanisms (eg, `query.setState`, `queryClient.removeQueries`, etc). Out of scope mutations to the cache are not encouraged and will not fire subscription callbacks
104β
105**Returns**
106β
107- `unsubscribe: Function => void`
108 - This function will unsubscribe the callback from the query cache.
109β
110## `queryCache.clear`
111β
112The `clear` method can be used to clear the cache entirely and start fresh.
113β
114```tsx
115queryCache.clear()
116```
117[//]: # 'Materials'
118β
119## Further reading
120β
121To get a better understanding how the QueryCache works internally, have a look at [#18: Inside React Query
122](../community/tkdodos-blog#18-inside-react-query) from the Community Resources.
123β
124[//]: # 'Materials'