Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/joyqi/node-inmemory
A simple in-memory cache system for node.js.
https://github.com/joyqi/node-inmemory
Last synced: 17 days ago
JSON representation
A simple in-memory cache system for node.js.
- Host: GitHub
- URL: https://github.com/joyqi/node-inmemory
- Owner: joyqi
- Created: 2023-01-04T07:25:35.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-05-15T09:37:11.000Z (over 1 year ago)
- Last Synced: 2024-10-25T21:56:44.983Z (20 days ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/node-inmemory
- Size: 28.3 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# node-inmemory
A simple in-memory cache for node.js. Create a new cache with a given ttl (time to live) and a callback function to be called when the cache expires.
## Installation
```bash
npm install node-inmemory
```## Usage
```javascript
const { inMemory } = require('node-inmemory');const [get, reset] = inMemory(() => {
console.log('Cache initialized');
return 'Hello World';
}, 1000);console.log(get()); // Cache initialized \n Hello World
reset();
console.log(get()); // Cache initialized \n Hello WorldsetTimeout(() => {
console.log(get()); // Cache initialized \n Hello World
}, 2000);
```Async functions are also supported:
```javascript
const { inMemory } = require('node-inmemory');const [get, reset] = inMemory(async () => {
console.log('Cache initialized');
return 'Hello World';
}, 1000);console.log(await get()); // Cache initialized \n Hello World
```Set ttl to 0 (default) to disable the expiration:
```javascript
const { inMemory } = require('node-inmemory');const [get, reset] = inMemory(() => {
console.log('Cache initialized');
return 'Hello World';
});console.log(get()); // Cache initialized \n Hello World
setTimeout(() => {
console.log(get()); // Hello World
}, 2000);
```