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.
// Directory where cached responses will be storedval file = File(cacheDir, "apolloCache")// Size in bytes of the cacheval size: Long = 1024 * 1024// Create the http response cache storeval cacheStore = DiskLruHttpCacheStore(file, size)// Build the ApolloClientval apolloClient = ApolloClient.builder().serverUrl("/").httpCache(ApolloHttpCache(cacheStore)).okHttpClient(okHttpClient).build()// Control the cache policyval query = FeedQuery(limit = 10, type = FeedType.HOT)val dataResponse = apolloClient.query(query).httpCachePolicy(HttpCachePolicy.CACHE_FIRST).toDeferred().await()
IMPORTANT: Caching is provided only for
query operations. It isn't available for
There are four available cache policies
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.
NETWORK_FIRST policies you can define the timeout after what cached response is treated as expired
and will be evicted from the http cache,