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

cachedts

v0.0.5

Published

Simple transparent caching utils for TypeScript

Readme

cachedts – Simple Function-Level Caching for TypeScript APIs

cachedts is a lightweight utility for function-level memoization of object methods in TypeScript. It wraps an object and automatically caches the results of its functions based on input arguments.

Perfect for avoiding repeated computations, expensive API calls, or just improving performance with minimal setup.


Features

  • Zero-dependency
  • Memoizes function results by arguments
  • Optional TTL-based expiration
  • Per-function or global settings
  • Debug logging
  • Custom cache key generation

Quick Start

import { cached } from "cachedts";

const mathApi = {
  slowSquare(x: number) {
    console.log("Calculating...");
    return x * x;
  },
};

const cachedMath = cached(mathApi);

cachedMath.slowSquare(5); // logs "Calculating..." and returns 25
cachedMath.slowSquare(5); // returns 25 instantly (from cache)

Options

const cachedApi = cached(apiObject, {
  debug: true, // Log cache hits/misses
  settings: {
    enabled: true,
    ttl: 5000, // Cache expires after 5 seconds
  },
  overrides: {
    // Per-method override
    expensiveMethod: { ttl: 10000 },
  },
  getCacheKey(methodName, args) {
    return `${methodName}-${JSON.stringify(args)}`;
  },
});

| Option | Type | Description | |---------------|---------------------------------------------|---------------------------------------------------| | cache | Map | Provide a shared cache instance | | debug | boolean | Enable logging for cache hits/misses | | settings | { enabled?: boolean; ttl?: number } | Global cache settings | | overrides | Partial<Record<keyof TApi, CacheSettings>>| Per-method cache control | | getCacheKey | (methodName, args) => string \| symbol | Custom key generator for cache entries |


Example with TTL

const api = {
  greet(name: string) {
    return `Hello, ${name}`;
  },
};

const cachedApi = cached(api, {
  settings: { ttl: 1000 },
});

cachedApi.greet("Alice"); // cache miss
setTimeout(() => {
  cachedApi.greet("Alice"); // cache hit
}, 500);

setTimeout(() => {
  cachedApi.greet("Alice"); // cache expired, miss
}, 1500);

Cache Invalidation

You can clear cached results using the invalidate function. It allows three levels of granularity:

Invalidate the entire cache

import { invalidate } from "cachedts";

invalidate(cachedApi); // Clears all cached entries for all methods

Invalidate a specific method's cache

invalidate(cachedApi, "getParams"); // Clears all cache entries for `getParams`

Invalidate a specific method call with given arguments

invalidate(cachedApi, "getParams", "user123", 42); // Clears cache entry for `getParams("user123", 42)`

This is useful when the underlying data changes and a fresh fetch is needed for specific arguments.

Notes

  • No effect is applied if the method is not a function or has no cached entries.
  • Cache key computation uses the same mechanism as the one used by cached(), including custom getCacheKey if provided.

Advanced Usage: Custom Cache Key

By default, cache keys are generated based on the function name and serialized arguments.

To customize:

const cachedApi = cached(api, {
  getCacheKey(methodName, args) {
    if (methodName === "fetchUser") {
      return args[0]; // Use user ID directly
    }
  },
});

Accessing Cache Internals

You can introspect the wrapped object via the cacheStateKey:

import { cacheStateKey } from "cachedts";

const state = cachedApi[cacheStateKey];
console.log(state.cache); // underlying Map

Or by using the getCacheState function:

import { getCacheState } from "cachedts";

const state = getCacheState(cachedApi);
console.log(state.cache); // nderlying Map

Disabling Caching

Disable globally or per-method:

cached(api, {
  settings: { enabled: false }, // globally off
  overrides: {
    compute: { enabled: true }, // override for `compute` method
  },
});

Installation

npm install cachedts

License

MIT