Socket
Book a DemoInstallSign in
Socket

async-data-cache

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-data-cache

Node.js cache for async data

1.0.7
latest
Source
npmnpm
Version published
Weekly downloads
9
Maintainers
1
Weekly downloads
 
Created
Source

async-data-cache

<img src=https://img.shields.io/npm/l/async-data-cache> <img src=https://img.shields.io/snyk/vulnerabilities/github/emiliosp/async-data-cache> <img src=https://img.shields.io/npm/v/async-data-cache> <img src=https://img.shields.io/badge/keyword-performance-blue> <img src=https://img.shields.io/badge/keyword-cache-blue>

A simple async data cache with no dependencies.

This library allows your to cache the result of an async call.

It's useful when you have to call an api or execute a heavy query and the performance are more important than the real-time. You can easily choose the cache time to live (ttl).

Installation

yarn add async-data-cache

Usage

// Simulate a function that requires 3 secs to complete (e.g. a heavy db query)
const dataFetcher = (): Promise<Array<number>> => {
  console.log('dataFetcher');
  return new Promise((res, rej) => {
    setTimeout(() => res([1, 2, 3, 4, 5]), 3000);
  })
}

(async () => {
  // With the cache the dataFetcher function will be called only 1 time.
  const cache = new DataCache<Array<number>>(dataFetcher);
  let data = await cache.getData();
  console.log(data);
  data = await cache.getData();
  console.log(data);
  data = await cache.getData();
  console.log(data);
  data = await cache.getData();
  console.log(data);
})();

To change the ttl, pass the ttl minutes to the DataCache constructor

// 1 hour of ttl
const cache = new DataCache<Array<number>>(dataFetcher, 60);

Keywords

async

FAQs

Package last updated on 19 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.