Last active
May 2, 2025 12:59
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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