0
# Caching
1
2
HTTP response caching with filesystem storage, cache control directives, and LRU eviction.
3
4
## Capabilities
5
6
### Cache
7
8
HTTP response cache using filesystem storage with LRU eviction.
9
10
```java { .api }
11
public final class Cache {
12
public Cache(File directory, long maxSize);
13
public void initialize() throws IOException;
14
public void delete() throws IOException;
15
public void evictAll() throws IOException;
16
public Iterator<String> urls() throws IOException;
17
public long getSize() throws IOException;
18
public long getMaxSize();
19
public void flush() throws IOException;
20
public void close() throws IOException;
21
public File getDirectory();
22
public boolean isClosed();
23
public int getNetworkCount();
24
public int getHitCount();
25
public int getRequestCount();
26
}
27
```
28
29
### CacheControl
30
31
Cache-Control header directives for request and response caching policies.
32
33
```java { .api }
34
public final class CacheControl {
35
public static final CacheControl FORCE_NETWORK;
36
public static final CacheControl FORCE_CACHE;
37
public static CacheControl parse(Headers headers);
38
public boolean noCache();
39
public boolean noStore();
40
public int maxAgeSeconds();
41
public int sMaxAgeSeconds();
42
public boolean isPrivate();
43
public boolean isPublic();
44
public boolean mustRevalidate();
45
public int maxStaleSeconds();
46
public int minFreshSeconds();
47
public boolean onlyIfCached();
48
public boolean noTransform();
49
50
public static final class Builder {
51
public Builder();
52
public Builder noCache();
53
public Builder noStore();
54
public Builder maxAge(int maxAge, TimeUnit timeUnit);
55
public Builder maxStale(int maxStale, TimeUnit timeUnit);
56
public Builder minFresh(int minFresh, TimeUnit timeUnit);
57
public Builder onlyIfCached();
58
public Builder noTransform();
59
public CacheControl build();
60
}
61
}
62
```
63
64
**Usage Examples:**
65
66
```java
67
// Set up cache
68
File cacheDir = new File(context.getCacheDir(), "http-cache");
69
Cache cache = new Cache(cacheDir, 10 * 1024 * 1024); // 10 MB
70
client.setCache(cache);
71
72
// Force network request
73
Request request = new Request.Builder()
74
.url("https://api.example.com/data")
75
.cacheControl(CacheControl.FORCE_NETWORK)
76
.build();
77
78
// Use cache only
79
Request cacheOnlyRequest = new Request.Builder()
80
.url("https://api.example.com/data")
81
.cacheControl(CacheControl.FORCE_CACHE)
82
.build();
83
84
// Custom cache control with builder
85
CacheControl customCacheControl = new CacheControl.Builder()
86
.maxAge(60, TimeUnit.SECONDS) // Cache for 60 seconds
87
.maxStale(300, TimeUnit.SECONDS) // Accept stale cache up to 5 minutes
88
.minFresh(30, TimeUnit.SECONDS) // Require fresh data for 30 seconds
89
.build();
90
91
Request customRequest = new Request.Builder()
92
.url("https://api.example.com/data")
93
.cacheControl(customCacheControl)
94
.build();
95
96
// No cache request
97
CacheControl noCache = new CacheControl.Builder()
98
.noCache()
99
.noStore()
100
.build();
101
102
// Cache monitoring
103
System.out.println("Cache hits: " + cache.getHitCount());
104
System.out.println("Network requests: " + cache.getNetworkCount());
105
System.out.println("Total requests: " + cache.getRequestCount());
106
System.out.println("Cache size: " + cache.getSize() + " bytes");
107
```