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 🙏

© 2026 – Pkg Stats / Ryan Hefner

api-cache-layer

v1.0.0

Published

Intelligent caching layer for API calls with memory, Redis, dynamic TTL, and auto-invalidation

Downloads

158

Readme

api-cache-layer

Intelligent caching layer for API calls. Supports in-memory and Redis backends, dynamic TTL, tag-based invalidation, and Axios/Fetch integration.

Installation

npm install api-cache-layer
# Optional: Redis support
npm install ioredis

Features

  • Memory cache with LRU eviction
  • Redis adapter for distributed caching
  • Dynamic TTL per response
  • Tag-based invalidation
  • Pattern-based invalidation
  • Axios interceptor integration
  • Fetch wrapper
  • Hit/miss statistics and callbacks
  • Full TypeScript support

Quick Start

import { CacheLayer } from "api-cache-layer";

const cache = new CacheLayer({
  defaultTtl: 60000,
  maxMemoryEntries: 1000,
  keyPrefix: "myapp",
  onHit: (key) => { /* analytics */ },
  onMiss: (key) => { /* analytics */ },
});

const data = await cache.wrap("users:list", async () => {
  const response = await fetch("https://api.example.com/users");
  return response.json();
}, { ttl: 30000, tags: ["users"] });

Adapters

Memory (default)

import { CacheLayer, MemoryCacheAdapter } from "api-cache-layer";

const cache = new CacheLayer({
  adapter: new MemoryCacheAdapter(500),
  defaultTtl: 60000,
});

Redis

import Redis from "ioredis";
import { CacheLayer, RedisCacheAdapter } from "api-cache-layer";

const redis = new Redis({ host: "localhost", port: 6379 });

const cache = new CacheLayer({
  adapter: new RedisCacheAdapter(redis, "myapp:cache:"),
  defaultTtl: 300000,
});

API

cache.wrap(key, fetcher, options?)

Fetch with caching. Calls fetcher only on cache miss.

const user = await cache.wrap(
  `user:${id}`,
  () => fetchUser(id),
  {
    ttl: 30000,
    tags: ["users", `user:${id}`],
  }
);

cache.set(key, value, options?)

Store a value manually.

await cache.set("config", appConfig, { ttl: 3600000 });

cache.get(key)

Retrieve a cached value or null.

const config = await cache.get<AppConfig>("config");

cache.invalidate(key)

Remove a single cache entry.

await cache.invalidate("user:42");

cache.invalidateByTag(tag)

Remove all entries with a given tag.

// After updating a user, invalidate all cached data tagged with their ID
await cache.invalidateByTag("user:42");

cache.invalidateByPattern(pattern)

Remove all entries matching a regex pattern.

await cache.invalidateByPattern("^users:");

cache.clear()

Remove all cached entries and reset stats.

cache.getStats()

const { hits, misses, hitRate } = cache.getStats();

Dynamic TTL

const cache = new CacheLayer({
  ttlByStatus: {
    200: 60000,
    404: 5000,
    500: 0,
  },
});

// Or per-request function
await cache.wrap("key", fetcher, {
  ttl: (response) => response.isStale ? 5000 : 60000,
});

Axios Integration

import axios from "axios";
import { CacheLayer, createAxiosInterceptor } from "api-cache-layer";

const cache = new CacheLayer();
const interceptor = createAxiosInterceptor(cache);

axios.interceptors.response.use(interceptor.response.onFulfilled);

Fetch Wrapper

const cachedFetch = cache.wrapFetch("https://api.example.com");

const response = await cachedFetch("/users");
const data = await response.json();

Running Tests

npm install
npm test

License

MIT