开发者社区> 镜像站> NPM> file-system-cache
file-system-cache
A super-fast, promise-based cache that reads and writes to the file-system.
Last updated 5 years ago by philcockfield .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install file-system-cache 
SYNC missed versions from official npm registry.

file-system-cache

Build Status

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.
});

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.

get(key, defaultValue)

Retrieves the contents of a cached file.

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

Use getSync for a synchronous version. Pass a defaultValue parameter to have that value returned if nothing exists within the cache.

set(key, value)

Write a value to the cache.

cache.set("foo", "...value...")
  .then(result => /* Success */)

Value types are stored and respected on subsequent calls to get. For examples, passing in Object will return that in it's 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

# Watch and re-run tests.
npm run tdd

Current Tags

  • 1.0.5                                ...           latest (5 years ago)

6 Versions

  • 1.0.5                                ...           5 years ago
  • 1.0.4                                ...           5 years ago
  • 1.0.3                                ...           5 years ago
  • 1.0.2                                ...           5 years ago
  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 152
This Week 1,595
This Month 1,223
Last Day 387
Last Week 2,037
Last Month 8,365
Dependencies (3)
Dev Dependencies (4)