Skip to main content
The QueryCache is responsible for storing and managing all Query instances. It’s the underlying storage mechanism used by QueryClient.

Constructor

Creates a new QueryCache instance.
QueryCacheConfig
Configuration options for the QueryCache

Example

Methods

build

Builds or retrieves a query instance. If a query with the same hash exists, it returns that query. Otherwise, it creates a new one.
QueryClient
required
The QueryClient instance.
QueryOptions
required
Options for the query including queryKey and queryHash.
QueryState
Optional initial state for a new query.
Query
Returns the Query instance (either existing or newly created).

add

Adds a query to the cache.
Query
required
The query instance to add.

remove

Removes a query from the cache and destroys it.
Query
required
The query instance to remove.

clear

Clears all queries from the cache.

Example

get

Retrieves a query from the cache by its query hash.
string
required
The hash of the query to retrieve.
Query
Returns the Query instance if found, otherwise undefined.

getAll

Returns all queries in the cache.
Array
Returns an array of all Query instances in the cache.

Example

find

Finds a single query matching the provided filters.
QueryFilters
required
Filters to match queries.
Query
Returns the first matching Query instance, or undefined if not found.

Example

findAll

Finds all queries matching the provided filters.
QueryFilters
Filters to match queries. If not provided, returns all queries.
Array
Returns an array of all matching Query instances.

Example

notify

Notifies all cache listeners of an event.
QueryCacheNotifyEvent
required
The event to notify listeners about.

subscribe

Subscribes to cache events.
(event: QueryCacheNotifyEvent) => void
required
Function called when cache events occur.
function
Returns an unsubscribe function.

Example

Events

The QueryCache emits the following events:

added

Fired when a query is added to the cache.

removed

Fired when a query is removed from the cache.

updated

Fired when a query is updated.

observerAdded

Fired when an observer is added to a query.

observerRemoved

Fired when an observer is removed from a query.

observerResultsUpdated

Fired when observer results are updated.

observerOptionsUpdated

Fired when observer options are updated.

Internal Methods

onFocus

Called when the window regains focus. Triggers onFocus on all queries.

onOnline

Called when the network comes back online. Triggers onOnline on all queries.

Usage with QueryClient

While you can use QueryCache directly, it’s typically used through a QueryClient: