Overview
TheQueryCache 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
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
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.get()
Retrieves a query from the cache by its hash.string
required
The hash of the query to retrieve
Query instance or undefined if not found
Example:
getAll()
Returns all queries in the cache.Query instances
Example:
find()
Finds a single query that matches the filters.QueryFilters
required
Filters to match queries
Query or undefined
Example:
findAll()
Finds all queries that match the filters.QueryFilters
Filters to match queries
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
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 cacheremoved- When a query is removed from the cacheupdated- When a query’s state changesobserverAdded- When an observer subscribes to a queryobserverRemoved- When an observer unsubscribes from a queryobserverResultsUpdated- When observer results are updatedobserverOptionsUpdated- When observer options are updated