Skip to content

Instantly share code, notes, and snippets.

@vella-nicholas
Created November 20, 2018 11:27
Show Gist options
  • Save vella-nicholas/a424d1204fc82fbeee80c7eeb2bf7549 to your computer and use it in GitHub Desktop.
Save vella-nicholas/a424d1204fc82fbeee80c7eeb2bf7549 to your computer and use it in GitHub Desktop.
const maxAge = 30; // maximum cache age (min)
/**
* Gets the cached data for the specified request.
* @param url The request URL.
* @return The cached data or null if no cached data exists for this request.
*/
getCacheData(url: string): HttpResponse<any> | null {
const cacheEntry = this.cachedData[url];
if (cacheEntry) {
const exiprationDate = new Date();
exiprationDate.setMinutes(exiprationDate.getMinutes() - maxAge);
if (new Date(exiprationDate).getTime() >= new Date(cacheEntry.lastUpdated).getTime()) {
return null;
} else {
return cacheEntry.data;
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment