Socket
Socket
Sign inDemoInstall

file-system-cache

Package Overview
Dependencies
5
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    file-system-cache

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


Version published
Weekly downloads
5.8M
decreased by-1.28%
Maintainers
1
Install size
1.24 MB
Created
Weekly downloads
 

Changelog

Source

[2.3.0] - 2023-05-22

Added
  • ttl (time to live) to expire caches (thanks @douglaslinsmeyer)

Readme

Source

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

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.
  ttl: 60               // Optional. A time-to-live that determines how long the cache item is valid, in seconds.
});

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

Keywords

FAQs

Last updated on 21 May 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc