July 2018
Intermediate to advanced
354 pages
8h 51m
English
The next cache invalidation strategy is based on how long a response can be cached. If you don't have access to the Cache-Control header, it may be challenging to determine how long to cache the response.
The good news is that the Cache-Control header is not the only way to determine a response's cache lifespan. When responses are added to a named cache, a "date" value is added. You can use the cached date value to apply a timeout rule to the cached responses:
let responseDate = new Date(response.headers.get("date")),
currentDate = Date.now();
if(!DateManager.compareDates(currentDate,
DateManager.addSecondsToDate(responseDate, 300))) {
cache.add(request);
}else{
cache.delete(request);
}
When a cached ...
Read now
Unlock full access