[](https://www.npmjs.org/package/@awsless/weak-cache) [](https://www.npmjs.org/package/@awsless/weak-cac
npm install @awsless/weak-cache


The @awsless/weak-cache package provides a fully typed simple cache that works with the JS garbage collection (GC). It uses weak references (and finalization registry) to allow GC to remove the cached data as part of the normal GC cycles, but still continue to provide cached access to the data as long as it still resides in memory and hasn't been collected.
Install with (NPM):
```
npm i @awsless/weak-cache
`ts
import { WeakCache } from '@awsless/weak-cache';
// Create new WeakCache instance
const cache = new WeakCache
// Cache an empty object
cache.set('key', {});
// Return the object above as long as it is still cached
console.log(cache.get('key'));
// Return a boolean if the object above still exists in the cache
console.log(cache.has('key'));
// Delete a cached item
console.log(cache.delete('key'));
// Clear the cache
console.log(cache.clear());
// Return the amount of cached items
console.log(cache.size);
// Iterate over the cached items
for(const [ key, value ] of cache) {
...
}
``
MIT