What is file-system-cache?
The file-system-cache npm package provides a simple and efficient way to cache data on the file system. It allows you to store, retrieve, and manage cached data, making it useful for applications that need to cache data locally to improve performance and reduce redundant operations.
What are file-system-cache's main functionalities?
Set Cache
This feature allows you to set a value in the cache with a specific key. The code sample demonstrates how to set a cache entry with the key 'key' and the value 'value'.
const FileSystemCache = require('file-system-cache');
const cache = FileSystemCache.default();
cache.set('key', 'value').then(() => {
console.log('Cache set successfully');
});
Get Cache
This feature allows you to retrieve a value from the cache using a specific key. The code sample demonstrates how to get the cached value for the key 'key'.
const FileSystemCache = require('file-system-cache');
const cache = FileSystemCache.default();
cache.get('key').then(value => {
console.log('Cached value:', value);
});
Remove Cache
This feature allows you to remove a specific entry from the cache using its key. The code sample demonstrates how to remove the cache entry for the key 'key'.
const FileSystemCache = require('file-system-cache');
const cache = FileSystemCache.default();
cache.remove('key').then(() => {
console.log('Cache removed successfully');
});
Clear Cache
This feature allows you to clear all entries from the cache. The code sample demonstrates how to clear the entire cache.
const FileSystemCache = require('file-system-cache');
const cache = FileSystemCache.default();
cache.clear().then(() => {
console.log('Cache cleared successfully');
});
Other packages similar to file-system-cache
node-cache
node-cache is an in-memory caching module for Node.js. It provides similar functionalities to file-system-cache but stores data in memory instead of the file system. This makes it faster for read/write operations but less persistent across application restarts.
cache-manager
cache-manager is a multi-level caching module for Node.js that supports various storage engines, including memory, Redis, and file system. It offers more flexibility and scalability compared to file-system-cache, making it suitable for more complex caching needs.
flat-cache
flat-cache is a lightweight and fast cache for Node.js that stores data in a flat file. It is similar to file-system-cache in terms of file-based storage but is designed to be simpler and more efficient for small to medium-sized caches.
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';
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",
ns: "my-namespace",
hash: "sha1",
ttl: 60,
});
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 => )
.catch(err => );
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.
cache.set("foo", "...value...")
.then(result => )
cache.set("bar", "...baz", 60)
.then(result => )
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(() => )
clear()
Clears all cached items from the file-system.
cache.clear()
.then(() => )
save()
Saves (sets) several items to the cache in one operation.
cache.save([{ key:"one", value:"hello" }, { key:"two", value:222 }])
.then(result => )
load()
Loads all files within the cache's namespace.
cache.load()
.then(result => )
Test
# Run tests.
npm test