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

@ardrive/ardrive-promise-cache

v1.4.0

Published

A promise cache utility.

Downloads

283

Readme

@ardrive/ardrive-promise-cache

@ardrive/ardrive-promise-cache is a caching library designed to cache promises, enabling faster subsequent retrievals of data. It includes two types of caching implementations:

  • PromiseCache - a simple cache that stores promises in memory
  • ReadThroughPromiseCache - a wrapper of PromiseCache that allows providing an async readThroughFunction that is called when the cache does contain the requested key

Installation

You can install the library using npm or yarn:

npm i @ardrive/ardrive-promise-cache
yarn add @ardrive/ardrive-promise-cache

Usage

PromiseCache

Example

import { PromiseCache, CacheParams } from '@ardrive/ardrive-promise-cache';

const params: CacheParams = {
  cacheCapacity: 100,
  cacheTTL: 60_000, // cache for 1 minute
};

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

// Storing a promise
const promise1 = new Promise<number>((resolve) => resolve(42));

cache.put('answer', promise1);

// resolves to 42
await cache.get('answer');

const promise2 = new Promise<number>((resolve) => resolve(100));

// overwrite existing cached promise
cache.put('answer', promise2);

// resolves to 100
await cache.get('answer');

// removes the promise from the cache
cache.remove('answer');

// resolves to undefined as no longer exists in the cache
await cache.get('answer');

// clear the cache
cache.clear();

// Getting the cache size
const size = cache.size();

API

constructor({ cacheCapacity, cacheTTL }: CacheParams)

Creates a new PromiseCache instance with the specified capacity and time-to-live (TTL) for cached items.

put(key: K, value: Promise<V>): Promise<V>

Stores a promise with the specified key in the cache.

get(key: K): Promise<V> | undefined

Retrieves a promise from the cache using the specified key. Returns undefined if the key is not found.

remove(key: K): void

Removes a promise from the cache using the specified key.

clear(): void

Clears the entire cache.

size(): number

Returns the current number of items in the cache.


ReadThroughPromiseCache

A Read Through Cache is useful for high throughput systems. If the cache contains the requested data, it is immediately returned. If the data does not exist (or has expired), the cache will fetch and place resulting promise in the cache for future requests. Some examples and use cases for Read Through Caching (and other caching patterns) can be found here.

Example

import { ReadThroughPromiseCache, CacheParams } from '@ardrive/ardrive-promise-cache';

const params: CacheParams = {
  cacheCapacity: 100,
  cacheTTL: 60_000, // cache for 1 minute
};

const readThroughCacheParams: ReadThroughPromiseCacheParams<string,AxiosResponse> = {
  cacheParams: params,
  readThroughFunction: async (key: K) => {
    // This function will be called when the cache does not contain the requested key.
    // It should return a promise that resolves with the value to cache.
    return axios.get(`https://example.com/api/${key}`);
  }
};

// create ReadThroughPromiseCache with capacity of 100 and TTL of 1 minute, and readThroughFunction to call API function
const readThroughCache = new ReadThroughPromiseCache<string, AxiosResponse>(readThroughCacheParams);

// caches new key and resulting axios promise (https://example.com/api/example) for 1 minute
const { status, data } = await readThroughCache.get('example')

// the cached axios promise will be returned
const { status: cachedStatus, data: cacheData } = await readThroughCache.get('example')

// wait 1 minute
await new Promise((res) => setTimeout(() => res, 60_000);

// 'example' key has expired, so readThroughFunction will be called again and new promise will be returned
const { status: refreshedStatus, data:refreshedData } = await readThroughCache.get('example')

API

constructor({ cacheParams: { cacheCapacity, cacheTTL }, readThroughFunction: (key: string) => Promise<V>: ReadThroughPromiseCacheParams)

Creates a new ReadThroughPromiseCache instance with the specified capacity and time-to-live (TTL) for cached items and readThroughFunction that will be called when the cache does not contain the key.

getWithStatus(key: K): Promise<{status: 'hit' | 'miss', data: V}>

Returns a Promise'd object, with the key 'data' pre-awaited and 'status' key indicating if it was a hit or a miss.

Note

The method cacheKeyString(key: K): string is used internally to create cache keys. The default implementation may not sufficiently differentiate keys for certain object types, depending on their toJSON implementation. You may need to override this method if you encounter issues.