Here's your chance to speak at GraphQL Summit in New York City, October 8 - 10, 2024! 🏙️ Submit your proposal by May 31.
Launch GraphOS Studio

HTTP cache

This page focuses on the HTTP cache. If you want to deduplicate the storage of your objects and/or notify your UI when your data changes, take a look at the normalized cache instead.

ℹ️ The HTTP cache is only available on the JVM.


To enable HTTP cache support, add the dependency to your project's build.gradle file:

dependencies {

If you're targeting Android API level < 26, you'll need to enable core library desugaring to support the java.time API:

android {
compileOptions {
// Flag to enable support for the new language APIs
coreLibraryDesugaringEnabled = true

Then configure your HTTP cache:

apolloClient = ApolloClient.Builder()
// Use a dedicated directory for the cache
directory = File(pathToCacheDirectory),
// Configure a max size of 100MB
maxSize = 100 * 1024 * 1024


The HTTP cache is a least recently used (LRU) cache with a configurable max size.

Once your cache setup is complete, the cache will be used by default by all your queries. By default, queries will try to find a result in the cache first and go the network if it's not there. This is the HttpFetchPolicy.CacheFirst. You can customize that behaviour with httpFetchPolicy(HttpFetchPolicy):

val response = apolloClient.query(query)
// Don't use the cache
// Or only use the cache
// Finally, execute your query

If the is present in cache, it will be used to return If not, a HttpCacheMissException will be thrown.

You can also set an expiration time either globally or for specific queries. The entries will automatically be removed from the cache after the expiration time:

// Globally
apolloClient = ApolloClient.Builder()
// Expire after 1 hour
.httpExpireTimeout(60 * 60 * 1000)
// On a specific query
val response = apolloClient.query(query)
// Expire after 1 hour
.httpExpireTimeout(60 * 60 * 1000)

If a specific query must not be cached, you can use httpDoNotStore():

val response = apolloClient.query(query)
// Don't cache this query
.httpDoNotStore(httpDoNotStore = true)

Clearing the cache

Call apolloClient.httpCache.clearAll() to clear the cache of all entries.

Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy