Skip to content

Instantly share code, notes, and snippets.

@adactio
Last active August 17, 2020 13:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adactio/c85978cfa1becaadb0ec47f6de6064c1 to your computer and use it in GitHub Desktop.
Save adactio/c85978cfa1becaadb0ec47f6de6064c1 to your computer and use it in GitHub Desktop.
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