node-inmemory
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

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

npm install node-inmemory

Usage

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 World

setTimeout(() => {
  console.log(get()); // Cache initialized \n Hello World
}, 2000);

Async functions are also supported:

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:

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);

Readme

Keywords

none

Package Sidebar

Install

npm i node-inmemory

Weekly Downloads

26

Version

0.1.1

License

none

Unpacked Size

18.4 kB

Total Files

15

Last publish

Collaborators

  • joyqi