Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

async-cache

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-cache

Cache your async lookups and don't fetch the same thing more than necessary.

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
362K
decreased by-2.73%
Maintainers
1
Weekly downloads
 
Created

What is async-cache?

The async-cache npm package provides a simple and efficient way to cache asynchronous function results. It is particularly useful for caching the results of expensive or frequently called asynchronous operations, such as API requests or database queries.

What are async-cache's main functionalities?

Basic Caching

This feature demonstrates how to create a basic cache using async-cache. The cache is configured with a load function that simulates an asynchronous operation. When the cache's get method is called, it either returns the cached value or invokes the load function to fetch and cache the value.

const AsyncCache = require('async-cache');

const cache = new AsyncCache({
  load: function (key, callback) {
    // Simulate an async operation
    setTimeout(() => {
      callback(null, `Value for ${key}`);
    }, 1000);
  }
});

cache.get('testKey', (err, result) => {
  if (err) throw err;
  console.log(result); // Output: Value for testKey
});

Cache Expiration

This feature demonstrates how to set an expiration time for cache entries using the maxAge option. In this example, cache entries expire after 5 seconds. After the expiration time, the load function is called again to reload the value.

const AsyncCache = require('async-cache');

const cache = new AsyncCache({
  maxAge: 5000, // Cache entries expire after 5 seconds
  load: function (key, callback) {
    // Simulate an async operation
    setTimeout(() => {
      callback(null, `Value for ${key}`);
    }, 1000);
  }
});

cache.get('testKey', (err, result) => {
  if (err) throw err;
  console.log(result); // Output: Value for testKey
});

setTimeout(() => {
  cache.get('testKey', (err, result) => {
    if (err) throw err;
    console.log(result); // Output: Value for testKey (reloaded after expiration)
  });
}, 6000);

Cache Size Limit

This feature demonstrates how to set a maximum number of cache entries using the max option. In this example, the cache can hold a maximum of 2 entries. When a new entry is added beyond this limit, the least recently used entry is evicted.

const AsyncCache = require('async-cache');

const cache = new AsyncCache({
  max: 2, // Maximum number of cache entries
  load: function (key, callback) {
    // Simulate an async operation
    setTimeout(() => {
      callback(null, `Value for ${key}`);
    }, 1000);
  }
});

cache.get('key1', (err, result) => {
  if (err) throw err;
  console.log(result); // Output: Value for key1
});

cache.get('key2', (err, result) => {
  if (err) throw err;
  console.log(result); // Output: Value for key2
});

cache.get('key3', (err, result) => {
  if (err) throw err;
  console.log(result); // Output: Value for key3
});

cache.get('key1', (err, result) => {
  if (err) throw err;
  console.log(result); // Output: Value for key1 (reloaded because it was evicted)
});

Other packages similar to async-cache

Keywords

FAQs

Package last updated on 19 Mar 2016

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc