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

file-system-cache

v2.4.4

Published

A super-fast, promise-based cache that reads and writes to the file-system.

Downloads

23,274,898

Readme

ci(esm)

file-system-cache

A super-fast, promise-based cache that reads and writes to the file-system.

Installation

npm install --save file-system-cache

Import

import Cache from 'file-system-cache';

   // or ↑↓ (equivalent)

import { Cache } from 'file-system-cache';

Usage (API)

Create an instance of the cache optionally giving it a folder location to store files within.

import Cache from "file-system-cache";

const cache = Cache({
  basePath: "./.cache", // (optional) Path where cache files are stored (default).
  ns: "my-namespace",   // (optional) A grouping namespace for items.
  hash: "sha1"          // (optional) A hashing algorithm used within the cache key.
  ttl: 60               // (optional) A time-to-live (in secs) on how long an item remains cached.
});

Reference default for CommonJS, e.g: const Cache = require('file-system-cache').default

The optional ns ("namespace") allows for multiple groupings of files to reside within the one cache folder. When you have multiple caches with different namespaces you can re-use the same keys and they will not collide.

The optional ttl ("time to live") allows you to set a default expiration for the cache key, in seconds. For example if you set this value to 60 then cache hits to any cache key made beyond the limit of that 60 seconds will result in cache misses.

get(key, defaultValue)

Retrieves the contents of a cached file.

cache.get("foo")
  .then(result => /* Success */)
  .catch(err => /* Fail */);

or use modern async/await syntactic sugar of course:

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

Use getSync for a synchronous version.
Pass a defaultValue parameter to use if the key does not exist within the cache.

set(key, value, ttl)

Write a value to the cache.

/* This will apply the default TTL to this cache key */
cache.set("foo", "...value...")
  .then(result => /* Success */)

/* This will set a specific TTL for this cache key */
cache.set("bar", "...baz", 60)
  .then(result => /* Success */)

Value types are stored and respected on subsequent calls to get. For examples, passing in Object will return that in its parsed object state.

Use setSync for a synchronous version.

remove(key)

Deletes the specified cache item from the file-system.

cache.remove("foo")
  .then(() => /* Removed */)

clear()

Clears all cached items from the file-system.

cache.clear()
  .then(() => /* All items deleted */)

save()

Saves (sets) several items to the cache in one operation.

cache.save([{ key:"one", value:"hello" }, { key:"two", value:222 }])
  .then(result => /* All items saved. */)

load()

Loads all files within the cache's namespace.

cache.load()
  .then(result => /* The complete of cached files (for the ns). */)

Test

# Run tests.
npm test