An HTTP & SPDY client for Android and Java applications with efficient connection pooling, interceptors, and modern protocol support
—
HTTP response caching with filesystem storage, cache control directives, and LRU eviction.
HTTP response cache using filesystem storage with LRU eviction.
public final class Cache {
public Cache(File directory, long maxSize);
public void initialize() throws IOException;
public void delete() throws IOException;
public void evictAll() throws IOException;
public Iterator<String> urls() throws IOException;
public long getSize() throws IOException;
public long getMaxSize();
public void flush() throws IOException;
public void close() throws IOException;
public File getDirectory();
public boolean isClosed();
public int getNetworkCount();
public int getHitCount();
public int getRequestCount();
}Cache-Control header directives for request and response caching policies.
public final class CacheControl {
public static final CacheControl FORCE_NETWORK;
public static final CacheControl FORCE_CACHE;
public static CacheControl parse(Headers headers);
public boolean noCache();
public boolean noStore();
public int maxAgeSeconds();
public int sMaxAgeSeconds();
public boolean isPrivate();
public boolean isPublic();
public boolean mustRevalidate();
public int maxStaleSeconds();
public int minFreshSeconds();
public boolean onlyIfCached();
public boolean noTransform();
public static final class Builder {
public Builder();
public Builder noCache();
public Builder noStore();
public Builder maxAge(int maxAge, TimeUnit timeUnit);
public Builder maxStale(int maxStale, TimeUnit timeUnit);
public Builder minFresh(int minFresh, TimeUnit timeUnit);
public Builder onlyIfCached();
public Builder noTransform();
public CacheControl build();
}
}Usage Examples:
// Set up cache
File cacheDir = new File(context.getCacheDir(), "http-cache");
Cache cache = new Cache(cacheDir, 10 * 1024 * 1024); // 10 MB
client.setCache(cache);
// Force network request
Request request = new Request.Builder()
.url("https://api.example.com/data")
.cacheControl(CacheControl.FORCE_NETWORK)
.build();
// Use cache only
Request cacheOnlyRequest = new Request.Builder()
.url("https://api.example.com/data")
.cacheControl(CacheControl.FORCE_CACHE)
.build();
// Custom cache control with builder
CacheControl customCacheControl = new CacheControl.Builder()
.maxAge(60, TimeUnit.SECONDS) // Cache for 60 seconds
.maxStale(300, TimeUnit.SECONDS) // Accept stale cache up to 5 minutes
.minFresh(30, TimeUnit.SECONDS) // Require fresh data for 30 seconds
.build();
Request customRequest = new Request.Builder()
.url("https://api.example.com/data")
.cacheControl(customCacheControl)
.build();
// No cache request
CacheControl noCache = new CacheControl.Builder()
.noCache()
.noStore()
.build();
// Cache monitoring
System.out.println("Cache hits: " + cache.getHitCount());
System.out.println("Network requests: " + cache.getNetworkCount());
System.out.println("Total requests: " + cache.getRequestCount());
System.out.println("Cache size: " + cache.getSize() + " bytes");Install with Tessl CLI
npx tessl i tessl/maven-com-squareup-okhttp--okhttp