Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A recursive function to limit the number of items in a specified cache.
// Limit the number of items in a specified cache.
function trimCache(cacheName, maxItems) {
caches.open(cacheName)
.then( cache => {
cache.keys()
.then(keys => {
if (keys.length > maxItems) {
cache.delete(keys[0])
.then(
trimCache(cacheName, maxItems)
); // end delete then
} // end if
}); // end keys then
}); // end open then
} // end function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment