# ForceCache

class ForceCache : Closeable, Flushable
Content copied to clipboard

Caches HTTP and HTTPS responses to the filesystem so they may be reused, saving time and bandwidth.

## Cache Optimization

To measure cache effectiveness, this class tracks three statistics:

- requestCount the number of HTTP requests issued since this cache was created.

- networkCount the number of those requests that required network use.

- hitCount the number of those requests whose responses were served by the cache.

Sometimes a request will result in a conditional cache hit. If the cache contains a stale copy of the response, the client will issue a conditional `GET`. The server will then send either the updated response if it has changed, or a short 'not modified' response if the client's copy is still valid. Such responses increment both the network count and hit count.

The best way to improve the cache hit rate is by configuring the web server to return cacheable responses. Although this client honors all HTTP/1.1 (RFC 7234) cache headers, it doesn't cache partial responses.

## Force a Network Response

In some situations, such as after a user clicks a 'refresh' button, it may be necessary to skip the cache, and fetch data directly from the server. To force a full refresh, add the `no-cache` directive:

`

    Request request = new Request.Builder()
        .cacheControl(new CacheControl.Builder().noCache().build())
        .url("http://publicobject.com/helloworld.txt")
        .build();

`

If it is only necessary to force a cached response to be validated by the server, use the more efficient `max-age=0` directive instead:

`

    Request request = new Request.Builder()
        .cacheControl(new CacheControl.Builder()
            .maxAge(0, TimeUnit.SECONDS)
            .build())
        .url("http://publicobject.com/helloworld.txt")
        .build();

`

## Force a Cache Response

Sometimes you'll want to show resources if they are available immediately, but not otherwise. This can be used so your application can show something while waiting for the latest data to be downloaded. To restrict a request to locally-cached resources, add the `only-if-cached` directive:

`

    Request request = new Request.Builder()
        .cacheControl(new CacheControl.Builder()
            .onlyIfCached()
            .build())
        .url("http://publicobject.com/helloworld.txt")
        .build();
    Response forceCacheResponse = client.newCall(request).execute();
    if (forceCacheResponse.code() != 504) {
      // The resource was cached! Show it.
    } else {
      // The resource was not cached.
    }

`

This technique works even better in situations where a stale response is better than no response. To permit stale cached responses, use the `max-stale` directive with the maximum staleness in seconds:

`

    Request request = new Request.Builder()
        .cacheControl(new CacheControl.Builder()
            .maxStale(365, TimeUnit.DAYS)
            .build())
        .url("http://publicobject.com/helloworld.txt")
        .build();

`

The CacheControl class can configure request caching directives and parse response caching directives. It even offers convenient constants CacheControl.FORCE_NETWORK and CacheControl.FORCE_CACHE that address the use cases above.

## Types

Companion
Link copied to clipboard
object Companion
Content copied to clipboard

## Functions

close
Link copied to clipboard
open override fun close()
Content copied to clipboard
delete
Link copied to clipboard
fun delete()
Content copied to clipboard
Closes the cache and deletes all of its stored values.
evictAll
Link copied to clipboard
fun evictAll()
Content copied to clipboard
Deletes all values stored in the cache.
flush
Link copied to clipboard
open override fun flush()
Content copied to clipboard
initialize
Link copied to clipboard
fun initialize()
Content copied to clipboard
Initialize the cache.
maxSize
Link copied to clipboard
fun maxSize(): Long
Content copied to clipboard
Max size of the cache (in bytes).
size
Link copied to clipboard
fun size(): Long
Content copied to clipboard
urls
Link copied to clipboard
fun urls(): MutableIterator<String>
Content copied to clipboard
Returns an iterator over the URLs in this cache.
writeAbortCount
Link copied to clipboard
@Synchronized()
fun writeAbortCount(): Int
Content copied to clipboard
writeSuccessCount
Link copied to clipboard
@Synchronized()
fun writeSuccessCount(): Int
Content copied to clipboard

## Properties

cache
Link copied to clipboard
val cache: DiskLruCache
Content copied to clipboard
directory
Link copied to clipboard
val directory: File
Content copied to clipboard
isClosed
Link copied to clipboard
val isClosed: Boolean
Content copied to clipboard