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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@m4x1m1l14n/cache

v1.1.0

Published

Lightweight in-memory isomorphic cache implementation with TTL for browser & Node JS written in TypeScript

Downloads

1,153

Readme

@m4x1m1l14n/cache

npm (scoped) ci codecov GitHub issues

Lightweight in-memory isomorphic cache implementation with TTL for browser & Node JS

This simple cache is written in TypeScript and works both for browser and Node. It is build on top of built-in Map thus theoretical limit for stored entries is in case of Node 2 ^ 24, which is approximately 16.6 million. Each entry in cache can has its own TTL (timeout) and assigned expiration callback fired once entry expire.

Table of Contents

Installation

npm i @m4x1m1l14n/cache

Documentation

Instantiation

Cache implementation uses templates so when you use this library in TypeScript project, you can restrict what type of key and value cache has to consume.

import { Cache } from '@m4x1m1l14n/cache';

const cache = new Cache<number, string>();

Cache behavior can be altered with options object passed to its constructor. Following table shows options that can be changed.

Option | Default value | Description --- | --- | --- resolution | 1000 | Interval in milliseconds after which entries are checked for expiry defaultTTL | Infinity | Default TTL for all added entries maxItems | 1000 | Maximum number of entries stored in cache

Example:

import { Cache, CacheOptions } from '@m4x1m1l14n/cache';

const options: CacheOptions =
{
	resolution: 1000,
	defaultTTL: Number.POSITIVE_INFINITY,
	maxItems: 1000
};

const cache = new Cache<number, string>( options );

set

Store value in cache under specified key.

Prototype:

public set( key : K, value : T, ttl? : number, callback?: ExpirationCallback<T> ) : Cache<K, T>;

Params:

  • key: Key under which value will be stored
  • value: Value to store
  • ttl (optional): Timeout after which cache entry will expire and will be deleted
  • callback (optional): Callback invoked once entry expire

Return value:

set method returns itself, so it is possible to chain multiple sets

import { Cache } from '@m4x1m1l14n/cache';

const cache = new Cache<number, string>();

cache
	.set( 1, 'Hello' )
	.set( 2, 'World' )
	.set( 3, '!');

Example:

import { Cache } from '@m4x1m1l14n/cache';

const cache = new Cache<number, string>();

// Key 1 will persist in cache until standardTTL is reached
cache.set( 1, 'Hello' );
// Key 2 will expire after 5 seconds
cache.set( 2, 'World', 5000 );
// Key 3 will expire after 3 seconds, and callback is called
cache.set( 3, 'Some other value', 3000, (value) =>
{
	console.log( `'${value}' has expired` );
	// Expected output
	// > 'Some other value' has expired
} );

get

Retrieve value of cached entry by its key.

Prototype:

public get( key : K, refresh = false ) : T | undefined

Params:

  • key: Key to retrieve
  • refresh (default: false): Whether to refresh entry TTL or not. In case true is passed, item TTL is refreshed and its expiration is postponed, like item is freshly inserted in cache.

Return value:

get returns value assigned to specified key. If key is not found in cache, undefined is returned.

Example:

import { Cache } from '@m4x1m1l14n/cache';

const cache = new Cache<number, string>();

cache.set( 1, 'Hello' );
console.log( cache.get( 1 ) );
// Expected output
// > Hello

take

Retrieve value of cached entry by its key and remove it from cache.

Prototype:

public take( key: K ): T | undefined

Params:

  • key: Key to retrieve

Return value:

take returns value assigned to specified key. If key is not found in cache, undefined is returned.

Example:

import { Cache } from '@m4x1m1l14n/cache';

const cache = new Cache<number, string>();

cache.set( 1, 'Hello' );
console.log( cache.take( 1 ) );
console.log( cache.take( 1 ) );
// Expected output
// > Hello
// > undefined

has

Returns wether cache contains specified key or not.

Prototype:

public has( key : K ) : boolean

Params:

  • key: Key to check for

Return value:

true in case key exists in cache, false otherwise

Example:

import { Cache } from '@m4x1m1l14n/cache';

const cache = new Cache<number, string>();

cache.set( 1, 'Hello' );
console.log( cache.has( 1 ) );
console.log( cache.has( 2 ) );
// Expected output
// > true
// > false

delete

Removes cache entry by specified key.

Prototype:

public delete( key : K ) : boolean

Params:

  • key: Key of entry to remove

Return value:

true in case key was removed, false otherwise

Example:

import { Cache } from '@m4x1m1l14n/cache';

const cache = new Cache<number, string>();

cache.set( 1, 'Hello' );
console.log( cache.get( 1 ) );
console.log( cache.delete( 1 ) );
console.log( cache.get( 1 ) );
console.log( cache.delete( 1 ) );
// Expected output
// > Hello
// > true
// > undefined
// > false