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

@chriscdn/file-cache

v0.0.14

Published

File cache

Readme

@chriscdn/file-cache

FileCache is a node utility for caching generated and remote files. Common use cases include:

  • Caching files stored in Amazon S3: Keep a local cache of frequently accessed files to avoid repeatedly fetching them.
  • Thumbnail caching: Store generated thumbnails locally for quick access instead of regenerating them every time.

FileCache does not store state in memory, meaning your cache remains unaffected by application restarts. Cached files are automatically expired using a time-to-live (TTL) policy and the file's modified date. The file's modified date is updated each time the file is accessed.

Installing

Using npm:

npm install @chriscdn/file-cache

Using yarn:

yarn add @chriscdn/file-cache

Usage

The general approach is this:

  • Create a callback — Write an async function that takes a file path and your custom input (like a URL), then generates or downloads the file and saves it to the path.
  • Set up the cache — Initialize a FileCache with your callback and some options (like where to store files and how long to keep them).
  • Get a file — Call the cache with your input. It will:
    • Hash the input to create a unique file name.
    • If the file is already cached and still valid, return its path.
    • If not, run your callback to create it, then cache and return the path.

Here's an example of an image cache:

import { FileCache, type FileCacheOptions } from "@chriscdn/file-cache";

// not required, but helps with duration calculations
import { Duration } from "@chriscdn/duration";

const downloadAndConvertToJPG = async (url: string, filePath: string) => {
  // ...
};

type MyCallbackArgs = {
  url: string;
};

const options: FileCacheOptions<MyCallbackArgs> = {
  // The path to the cache directory. The directory must exist. Do not store
  // anything else in this directory.
  cachePath: "/some/path/on/your/filesystem/",

  // optional, automatically create cachePath if it doesn't exist
  autoCreateCachePath: false,

  // An asynchronous callback function, which is executed if no matching cached
  // file is found. It is the responsibility of this function to use `args`
  // to write a file to `filePath`.
  cb: async (filePath: string, args: MyCallbackArgs) => {
    const { url } = args;
    await downloadAndConvertToJPG(url, filePath);
  },

  // Specifies the file extension for the cached file. Can be synchronous or asynchronous.
  ext: (args: MyCallbackArgs) => ".jpg",

  // Determines the time-to-live (TTL) of a cached file, in milliseconds,
  // based on when it was last accessed.
  ttl: Duration.toMilliseconds({ days: 7 }),

  // How often the cleanup task should run to purge expired cached files, in milliseconds.
  cleanupInterval: Duration.toMilliseconds({ hours: 4 }),
};

// create an instance
const imageCache = new FileCache(options);

const imageFilePath = await imageCache({
  url: "https://example.com/some/file/path.jpg",
});

Cleanup

FileCache instances are intended to be instantiated as singletons, persisting throughout the lifecycle of your app. If you need to deallocate an instance, be sure to call the destroy() method to prevent a memory leak. This does not cleanup the cache directory.

Tests

Run the tests using:

yarn test

License

MIT