Join us for GraphQL Summit, October 10-12 in San Diego. Use promo code ODYSSEY for $400 off your pass.
Launch GraphOS Studio
You're viewing documentation for a previous version of this software. Switch to the latest stable version.

HTTP cache

Apollo Android provides two different kinds of caches: an HTTP cache and a normalized cache. The HTTP cache is easier to set up but also has more limitations. 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.

HTTP cache

To enable HTTP cache support, add the dependency to your project's `build.gradle file. The latest version is Maven Central

dependencies {

Raw HTTP response cache

// Directory where cached responses will be stored
val file = File(cacheDir, "apolloCache")
// Size in bytes of the cache
val size: Long = 1024 * 1024
// Create the http response cache store
val cacheStore = DiskLruHttpCacheStore(file, size)
// Build the ApolloClient
val apolloClient = ApolloClient.builder()
// Control the cache policy
val query = FeedQuery(limit = 10, type = FeedType.HOT)
val dataResponse = apolloClient.query(query)
//Directory where cached responses will be stored
File file = new File(cacheDir, "apolloCache");
//Size in bytes of the cache
long size = 1024*1024;
//Create the http response cache store
DiskLruHttpCacheStore cacheStore = new DiskLruHttpCacheStore(file, size);
//Build the ApolloClient
ApolloClient apolloClient = ApolloClient.builder()
.httpCache(new ApolloHttpCache(cacheStore))
.enqueue(new ApolloCall.Callback<FeedQuery.Data>() {
@Override public void onResponse(@NotNull Response<FeedQuery.Data> dataResponse) {
@Override public void onFailure(@NotNull Throwable t) {

IMPORTANT: Caching is provided only for query s. It isn't available for mutation s.

There are four available cache policies HttpCachePolicy:

  • NETWORK_ONLY - Fetch a response from the network only, ignoring any cached responses. This is the default.
  • CACHE_ONLY - Fetch a response from the cache only, ignoring the network. If the cached response doesn't exist or is expired, then return an error.
  • CACHE_FIRST - Fetch a response from the cache first. If the response doesn't exist or is expired, then fetch a response from the network.
  • NETWORK_FIRST - Fetch a response from the network first. If the network fails and the cached response isn't expired, then return cached data instead.

For CACHE_ONLY, CACHE_FIRST and NETWORK_FIRST policies you can define the timeout after what cached response is treated as expired and will be evicted from the http cache, expireAfter(expireTimeout, timeUnit).`

Normalized cache
Persisted queries
Edit on GitHubEditForumsDiscord