Cache事件通过 EventListener API 暴露，典型场景如下:
Under a cache miss the normal request events are seen but an additional event shows the presence of the cache. Cache Miss will be typical if the item has not been read from the network, is uncacheable, or is past it’s lifetime based on Response cache headers.
- … Standard Events …
When cache flags require checking the cache results are still valid an early cacheConditionalHit event is received followed by a cache hit or miss. Critically in the cache hit scenario the server won’t send the response body.
The response will have non-null
cacheResponse will be used as the top level response only if the response code is HTTP/1.1 304 Not Modified.
- … Standard Events…
- ResponseBodyEnd (0 bytes)
The cache directory must be exclusively owned by a single instance.
Deleting the cache when it is no longer needed can be done. However this may delete the purpose of the cache which is designed to persist between app restarts.
Pruning the entire Cache to clear space temporarily can be done using evictAll.
Removing individual items can be done using the urls iterator. This would be typical after a user initiates a force refresh by a pull to refresh type action.
- Valid cacheable responses are not being cached
Make sure you are reading responses fully as unless they are read fully, cancelled or stalled Responses will not be cached.
See Cache documentation. https://square.github.io/okhttp/4.x/okhttp/okhttp3/-cache/