Search in sources :

Example 1 with HttpCacheRecord

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;
    }
}
Also used : Response(okhttp3.Response) HttpCacheRecord(com.apollographql.apollo.api.cache.http.HttpCacheRecord) ForwardingSource(okio.ForwardingSource) Source(okio.Source) ForwardingSource(okio.ForwardingSource) IOException(java.io.IOException)

Example 2 with HttpCacheRecord

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;
}
Also used : HttpCacheRecord(com.apollographql.apollo.api.cache.http.HttpCacheRecord) DiskLruCache(okhttp3.internal.cache.DiskLruCache)

Aggregations

HttpCacheRecord (com.apollographql.apollo.api.cache.http.HttpCacheRecord)2 IOException (java.io.IOException)1 Response (okhttp3.Response)1 DiskLruCache (okhttp3.internal.cache.DiskLruCache)1 ForwardingSource (okio.ForwardingSource)1 Source (okio.Source)1