Detalhes do pacote

file-system-cache

philcockfield16.5mMIT3.0.0-alpha.8

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

cache, fs, file-system

readme (leia-me)

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

changelog (log de mudanças)

Change Log

All notable changes to this project will be documented in this file. This project adheres to Semantic Versioning.

[next] - YYYY-MM-DD

Added

Changed

Deprecated

Removed

Fixed

Security

[3.0.0] - 2024-07-16

Changed

  • converted to ESM (thanks to @ndelangen on PR#47)

[2.4.7] - 2024-07-16

Fixed

  • revert to bundle that supports require('file-system-cache') on patch-release version number prior to re-releasing the ESM build on the next major version (see 3.0).

[2.4.5] - 2024-07-16

Added

Changed

  • switched testing framework from mocha to vitest
  • switched ts-node to tsx
  • converted to ESM (thanks to @ndelangen on PR#47)

    Deprecated

    Removed

    Fixed

    Security

[2.4.4] - 2023-08-11

Fixed

  • Expired item no longer throwing error on call to getSync (thanks @ejc3 for reporting)

[2.4.3] - 2023-07-28

Fixed

  • "@types/fs-extra" types as devDependency causing upstream issues (thanks @jbpenrath)
  • Transpile errors within lib imports (thanks @hankyupark)

[2.4.2] - 2023-06-8

Fixed

  • Issue #33: "@types/ramda" types as devDependency causing upstream issues on --skipLib

[2.4.1] - 2023-06-28

Fixed

  • Cleared case difference in generated /lib folder (name "Util.js" → "util.js")

[2.4.0] - 2023-06-27

Added

  • multiple path hashing options (thanks @trevor-vaughan)

[2.3.0] - 2023-05-22

Added

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

[2.1.0] - 2023-04-19

Changed

  • Switched hashing algorithm from MD5 to SHA1 (thanks @rmarone)

[2.0.2] - 2022-01-17

Security

  • Updated (via package.json {resolution}) to JSON5 >= 1.0.2 (which was marked as a security risk see here)

[2.0.1] - 2022-10-12

Changed

  • Updated package.json dependencies

[2.0.0] - 2022-05-14

Changed

  • Converted project to Typescript

    Fixed

  • Update refs (ramda), thanks to @shernaz

[1.1.0] - 2021-04-04

Changed

  • bumped the ramda depedency version to resolve ReDos

[1.0.3] - 2016-01-26

Changed

[0.0.1] - 2015-09-19

Added

Initial creation and publish.