use of ch.qos.logback.core.util.Duration in project webofneeds by researchstudio-sat.
the class CachingLinkedDataSource method fetchAndCacheIfAppropriate.
private DatasetResponseWithStatusCodeAndHeaders fetchAndCacheIfAppropriate(final URI resource, final URI requesterWebID, final LinkedDataCacheEntry linkedDataCacheEntry, final HttpHeaders headers) {
DatasetResponseWithStatusCodeAndHeaders responseData = fetchWithEtagValidation(resource, requesterWebID, linkedDataCacheEntry, headers);
Date expires = parseCacheControlMaxAgeValue(resource, responseData);
if (responseData.getDataset() == null) {
throw new LinkedDataFetchingException(resource, "Could not load dataset for URI " + resource + " and requesterWebID " + requesterWebID);
}
if (expires == null) {
expires = parseExpiresHeader(resource, responseData);
if (expires != null && expires.getTime() == 0) {
// Don't cache.
if (logger.isDebugEnabled()) {
logger.debug("Fetched {}. Will not be cached due to invalid 'Expires' header sent by server", resource);
}
return responseData;
}
}
EnumSet<CacheControlFlag> cacheControlFlags = parseCacheControlHeaderFlags(resource, responseData);
if (cacheControlFlags.contains(CacheControlFlag.NO_STORE) || cacheControlFlags.contains(CacheControlFlag.NO_CACHE)) {
// we are not allowed to cache the result
// make sure it's not in the cache from a previous request
cache.remove(makeCacheKey(resource, requesterWebID));
if (logger.isDebugEnabled()) {
logger.debug("Fetched {}. Will not be cached due to Cache-Control headers sent by server", resource);
}
return responseData;
}
Date responseDate = parseDateHeader(resource, responseData);
if (responseDate != null && expires != null) {
// old way of saying don't cache: Date header >= Expires header
if (responseDate.equals(expires) || responseDate.after(expires)) {
// make sure it's not in the cache from a previous request
if (logger.isDebugEnabled()) {
logger.debug("Fetched {}. Will not be cached due to Expires/Date header combination sent by server", resource);
}
cache.remove(makeCacheKey(resource, requesterWebID));
return responseData;
}
}
// if we don't get a new etag, see if we have a 304 code - then we can use th
// old etag
String etag = responseData.getResponseHeaders().getFirst(HttpHeaders.ETAG);
if (etag == null && responseData.getStatusCode() == HttpStatus.NOT_MODIFIED.value() && linkedDataCacheEntry != null) {
etag = linkedDataCacheEntry.getEtag();
}
// cache the result
LinkedDataCacheEntry entry = new LinkedDataCacheEntry(etag, expires, writeDatasetToByteArray(responseData.getDataset()), cacheControlFlags, responseData.getResponseHeaders(), responseData.getStatusCode());
this.cache.put(new Element(makeCacheKey(resource, requesterWebID), entry));
if (logger.isDebugEnabled()) {
logger.debug("Fetched and cached {}, will {}", resource, expires == null ? "never expire" : "expire in " + new Duration(expires.getTime() - new Date().getTime()).toString());
logger.debug("cache size: {} elements, in-memory size: {} bytes", cache.getSize(), cache.calculateInMemorySize());
}
return responseData;
}
Aggregations