Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mcollina/infinicache
https://github.com/mcollina/infinicache
Last synced: 12 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/mcollina/infinicache
- Owner: mcollina
- License: mit
- Created: 2021-07-15T16:28:09.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-07-16T07:51:50.000Z (over 3 years ago)
- Last Synced: 2024-12-25T09:46:51.635Z (15 days ago)
- Language: JavaScript
- Size: 4.88 KB
- Stars: 53
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# infinicache
A cache for Node.js that can use all available system memory without crashing.
It's based on `WeakRef` and `FinalizationRegistry`.## Install
```bash
npm i infinicache
```## Usage
```js
import Cache from 'infinicache'
import { promisify } from 'util'const immediate = promisify(setImmediate)
const cache = new Cache({
ttl: 5 // seconds
})// Create a scope so that obj goes
// out of scope
{
const obj = { foo: 'bar' }
cache.set('hello', obj)
console.log(cache.get('hello'))
}await immediate()
// We need to allocate a bazillion amount of objects
// to trigger a GC
const data = []
for (let i = 0; i < 1000000; i++) {
data.push({ i })
}console.log(cache.get('hello'))
```Note that this Cache is slower than most LRU caches. If you are looking for an LRU cache,
use [mnemonist `LRUCache`](https://yomguithereal.github.io/mnemonist/lru-cache.html).## License
MIT