Skip to main content

Overview

The QueryCache is responsible for storing and managing all Query instances. It provides methods to build, access, and manipulate queries, and emits events when queries are added, removed, or updated.

Constructor

QueryCacheConfig
Configuration options for the QueryCache
Example:

Methods

build()

Builds or retrieves a query instance.
QueryClient
required
The QueryClient instance
QueryOptions
required
Query options including queryKey
QueryState
Initial state for the query
Returns: A Query instance
If a query with the same hash already exists, it returns the existing query. Otherwise, creates a new one.

add()

Adds a query to the cache.
Query
required
The query instance to add
This method is typically called internally by the build() method.

remove()

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

clear()

Removes all queries from the cache.
Example:

get()

Retrieves a query from the cache by its hash.
string
required
The hash of the query to retrieve
Returns: The Query instance or undefined if not found Example:

getAll()

Returns all queries in the cache.
Returns: Array of all Query instances Example:

find()

Finds a single query that matches the filters.
QueryFilters
required
Filters to match queries
Returns: The first matching Query or undefined Example:

findAll()

Finds all queries that match the filters.
QueryFilters
Filters to match queries
Returns: Array of matching Query instances Example:

notify()

Notifies all subscribers of a cache event.
QueryCacheNotifyEvent
required
The event to notify subscribers about

subscribe()

Subscribes to cache events.
(event: QueryCacheNotifyEvent) => void
required
Callback function to handle cache events
Returns: Function to unsubscribe Example:

onFocus()

Called when the window regains focus. Triggers focus events on all queries.
This method is typically called automatically by the QueryClient.

onOnline()

Called when the network comes back online. Triggers online events on all queries.
This method is typically called automatically by the QueryClient.

Events

The QueryCache emits the following events:
  • added - When a query is added to the cache
  • removed - When a query is removed from the cache
  • updated - When a query’s state changes
  • observerAdded - When an observer subscribes to a query
  • observerRemoved - When an observer unsubscribes from a query
  • observerResultsUpdated - When observer results are updated
  • observerOptionsUpdated - When observer options are updated

Usage with QueryClient