npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@ricb/cache

v0.14.1

Published

Effective and simple caching

Readme

Cache

A simple, efficient caching module for Node.js applications.

It handles maxAge (aka ttl) and the storage location and file names can be configured

See MaxAge and Config

Installation

Install the package via npm:

npm install @ricb/cache

Usage

Store and retrieve

import { put, get } from '@ricb/cache';

const key = 'testKey';
const value = 'testValue';

await put(key, value);
const result = await get(key);
console.log(result); // Outputs: 'testValue'

Retrieve with max age

import { put, get } from '@ricb/cache';

const key = 'testKey';
const value = 'testValue';

await put(key, value);
await new Promise(resolve => setTimeout(resolve, 11));
const result = await get(key, '10 ms');
console.log(result); // Outputs: null

MaxAge

MaxAgeParam is a type that can be a string, number, or MaxAgeObject. It is used to specify the maximum age for a cache entry.

MaxAgeObject

MaxAgeObject is an object that can have the following properties:

  • d or days: The number of days.
  • h or hours: The number of hours.
  • m or minutes: The number of minutes.
  • s or seconds: The number of seconds.
  • ms or milliseconds: The number of milliseconds.

Each property is optional and can be used to specify the maximum age in different units of time.

maxAgeToMs Function

The maxAgeToMs function is used to convert a MaxAgeParam to milliseconds. It accepts a MaxAgeParam as an argument and returns a number.

If the MaxAgeParam is a number, it is returned as is. If it is a string, it is parsed and converted to milliseconds. If it is a MaxAgeObject, each property is converted to milliseconds and summed up.

Here are some examples of how to use maxAgeToMs:

// Using a number
const maxAgeMs1 = maxAgeToMs(1000);
console.log(maxAgeMs1); // Outputs: 1000

// Using a string
const maxAgeMs2 = maxAgeToMs('2 hours');
console.log(maxAgeMs2); // Outputs: 7200000

// Using a MaxAgeObject
const maxAgeMs3 = maxAgeToMs({ days: 3 });
console.log(maxAgeMs3); // Outputs: 259200000

Config

The cache module allows you to configure the cache directory and prefix using the config function or setCacheDir and setPrefix functions.

init Function

The config function is used to initialize the cache directory and prefix. It accepts two optional arguments:

  • prefix: The prefix to be used for the cache files.
  • cacheDir: The directory where the cache files will be stored.

If these arguments are not provided, the function will use the CACHE_DIR and CACHE_PREFIX environment variables. If these environment variables are also not set, it will use the system's temporary directory for cacheDir and an empty string for prefix.

setCacheDir Function

The setCacheDir function is used to set the cache directory. It accepts one argument:

  • dir: The directory where the cache files will be stored.

setPrefix Function

The setPrefix function is used to set the prefix for the cache files. It accepts one argument:

  • p: The prefix to be used for the cache files.

Here are some examples of how to use these functions:

// Initialize with a prefix and cache directory
init('_test.', __dirname);

// or set them individually
setCacheDir(__dirname);
setPrefix('_test.');