use of com.apollographql.apollo.api.cache.http.HttpCacheRecord in project apollo-android by apollographql.
the class ApolloHttpCache method read.
@Override
public Response read(@Nonnull final String cacheKey, final boolean expireAfterRead) {
HttpCacheRecord responseCacheRecord = null;
try {
responseCacheRecord = cacheStore.cacheRecord(cacheKey);
if (responseCacheRecord == null) {
return null;
}
final HttpCacheRecord cacheRecord = responseCacheRecord;
Source cacheResponseSource = new ForwardingSource(responseCacheRecord.bodySource()) {
@Override
public void close() throws IOException {
super.close();
closeQuietly(cacheRecord);
if (expireAfterRead) {
removeQuietly(cacheKey);
}
}
};
Response response = new ResponseHeaderRecord(responseCacheRecord.headerSource()).response();
String contentType = response.header("Content-Type");
String contentLength = response.header("Content-Length");
return response.newBuilder().body(new CacheResponseBody(cacheResponseSource, contentType, contentLength)).build();
} catch (Exception e) {
closeQuietly(responseCacheRecord);
logger.e(e, "Failed to read http cache entry for key: %s", cacheKey);
return null;
}
}
use of com.apollographql.apollo.api.cache.http.HttpCacheRecord in project apollo-android by apollographql.
the class DiskLruHttpCacheStore method cacheRecord.
@Override
public HttpCacheRecord cacheRecord(@Nonnull String cacheKey) throws IOException {
final DiskLruCache.Snapshot snapshot = cache.get(cacheKey);
if (snapshot == null) {
return null;
}
final HttpCacheRecord responseCacheRecord = new HttpCacheRecord() {
@Nonnull
@Override
public Source headerSource() {
return snapshot.getSource(ENTRY_HEADERS);
}
@Nonnull
@Override
public Source bodySource() {
return snapshot.getSource(ENTRY_BODY);
}
@Override
public void close() {
snapshot.close();
}
};
return responseCacheRecord;
}
Aggregations