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

ts-promise-cache

v0.9.0

Published

a loading cache for promises

Downloads

943

Readme

ts-promise-cache

Build Status Coverage Status Dependencies NPM

Loading cache for promises. Does not suffer from thundering herds problem (aka cache stampede).

Usage

The constructor takes a loader that loads missing entries. By default rejected Promises are not kept in the cache.

loader: (key: string) => Promise

const cache = new PromiseCache<string>((key: string) => Promise.resolve("value"));

const value = await cache.get("key");

expect(value).to.eq("value");

Config

The second constructor argument is an optional config (Partial<CacheConfig> config is ok).

export class CacheConfig<T> {
    // how often to check for expired entries
    public readonly checkInterval: number | "NEVER" = "NEVER";
    // time to live (milliseconds)
    public readonly ttl: number | "FOREVER" = "FOREVER";
    // specifies that entries should be removed 'ttl' milliseconds from either when the cache was accessed (read) or when the cache value was created or replaced
    public readonly ttlAfter: "ACCESS" | "WRITE" = "ACCESS";
    // remove rejected promises?
    public readonly removeRejected: boolean = true;
    // fallback for rejected promises
    public readonly onReject: (error: Error, key: string, loader: (key: string) => Promise<T>) => Promise<T>
        = (error) => Promise.reject(error)
    // called before entries are removed because of ttl
    public readonly onRemove: (key: string, p: Promise<T>) => void
        = () => undefined
}

Using Partial<CacheConfig> it looks like:

new PromiseCache<string>(loader, {ttl: 1000, onRemove: (key) => console.log("removing", key)});

Single promise cache

In case you have a single value to cache, use singlePromiseCache factory.

// singlePromiseCache<T>(loader: () => Promise<T>, config?: Partial<CacheConfig<T>>): () => Promise<T>

const cache = singlePromiseCache(() => Promise.resolve("value"));

const value = await cache();
expect(value).to.eq("value");

TTL Expiry

The default behavior is for values to remain in the cache for 'ttl' milliseconds since the last access. It is sometimes useful for cache expiry to be relative to when the cached value was first created (or last changed).

For this situation, set ttlAfter:"WRITE":

new PromiseCache<string>(loader, {ttl: 1000, ttlAfter:"WRITE"});

Statistics

stats() returns statistics:

interface Stats {
   readonly misses: number;
   readonly hits: number;
   readonly entries: number;
   readonly failedLoads: number;
}

Set value

You can set a value directly

cache.set("key", "value");
expect(await cache.get("key")).to.eq("value");

Retry

Retry can by implemented by using ts-retry-promise

const loader = failsOneTime("value");
const cache = new PromiseCache<string>(() => retry(loader));
expect(await cache.get("key")).to.eq("value");