@algolia/client-common
:
null
: turns off caching.in memory
: stores all cached data in memory. The cache is cleared when the page is refreshed.local storage
: stores cached data in the browser’s local storage. Entries are cleared based on the browser’s cache TTL.fallbackable
: automatically selects one of the preceding cache strategies, based on availability.
Requests and responses
If a user repeats a query during the same search session, the API client retrieves the response from the cache instead of sending a new request. This avoids unnecessary API requests, for example, when a user deletes characters from their current query and returns to a previously searched term. The response cache stores results of previous queries within the same session.Hosts
When a host becomes unreachable, the API client moves it to the end of the host list for subsequent requests. The client caches the host’s state for 2 minutes, which helps avoid retrying failed hosts too soon.Default implementations
These are the default caching mechanisms per environment:Type | Browser | Node | Workers |
---|---|---|---|
Requests | in memory | null | null |
Responses | in memory | null | null |
Hosts | local storage with fallback to in memory | in memory | in memory |
Custom implementations
You can use a different caching solution when initializing the client. For example, use the Algolia-provided implementations from@algolia/client-common
:
JavaScript
cache
interface definition.
Clear caches
The API client exposes an asynchronousclearCache
method that calls the clear
method of the requests
and responses
caches.
JavaScript