Skip to content

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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.