Open History to see the last commit
1---
2id: QueryCache
3title: QueryCache
4redirect_from:
5 - framework/react/reference/QueryCache
6---
7β
8The `QueryCache` is the storage mechanism for TanStack Query. It stores all the data, meta information and state of queries it contains.
9β
10**Normally, you will not interact with the QueryCache directly and instead use the `QueryClient` for a specific cache.**
11β
12```tsx
13import { QueryCache } from '@tanstack/react-query'
14β
15const queryCache = new QueryCache({
16 onError: (error) => {
17 console.log(error)
18 },
19 onSuccess: (data) => {
20 console.log(data)
21 },
22 onSettled: (data, error) => {
23 console.log(data, error)
24 },
25})
26β
27const query = queryCache.find({ queryKey: ['posts'] })
28```
29β
30Its available methods are:
31β
32- [`queryCache.find`](#querycache-find)
33- [`queryCache.findAll`](#querycache-findall)
34- [`queryCache.subscribe`](#querycache-subscribe)
35- [`queryCache.clear`](#querycache-clear)
36- [Further reading](#further-reading)
37β
38**Options**
39β
40- `onError?: (error: unknown, query: Query) => void`
41 - Optional
42 - This function will be called if some query encounters an error.
43- `onSuccess?: (data: unknown, query: Query) => void`
44 - Optional
45 - This function will be called if some query is successful.
46- `onSettled?: (data: unknown | undefined, error: unknown | null, query: Query) => void`
47 - Optional
48 - This function will be called if some query is settled (either successful or errored).
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](../framework/react/guides/filters#query-filters)
63 - `queryKey: QueryKey`: [Query Keys](../framework/react/guides/query-keys.md)
64β
65**Returns**
66β
67- `Query`
68 - The query instance from the cache
69β
70## `queryCache.findAll`
71β
72`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.
73β
74> Note: This is not typically needed for most applications, but can come in handy when needing more information about a query in rare scenarios
75β
76```tsx
77const queries = queryCache.findAll({ queryKey })
78```
79β
80**Options**
81β
82- `filters?: QueryFilters`: [Query Filters](../framework/react/guides/filters.md#query-filters)
83β
84**Returns**
85β
86- `Query[]`
87 - Query instances from the cache
88β
89## `queryCache.subscribe`
90β
91The `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
92β
93```tsx
94const callback = (event) => {
95 console.log(event.type, event.query)
96}
97β
98const unsubscribe = queryCache.subscribe(callback)
99```
100β
101**Options**
102β
103- `callback: (event: QueryCacheNotifyEvent) => void`
104 - 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
105β
106**Returns**
107β
108- `unsubscribe: Function => void`
109 - This function will unsubscribe the callback from the query cache.
110β
111## `queryCache.clear`
112β
113The `clear` method can be used to clear the cache entirely and start fresh.
114β
115```tsx
116queryCache.clear()
117```
118β
119[//]: # 'Materials'
120β
121## Further reading
122β
123To get a better understanding how the QueryCache works internally, have a look at [the Inside React Query article by TkDodo](https://tkdodo.eu/blog/inside-react-query).
124β
125[//]: # 'Materials'