Product
Introducing Java Support in Socket
We're excited to announce that Socket now supports the Java programming language.
file-system-cache
Advanced tools
A super-fast, promise-based cache that reads and writes to the file-system.
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.
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');
});
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 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 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.
A super-fast, promise-based cache that reads and writes to the file-system.
npm install --save file-system-cache
Import
import Cache from 'file-system-cache';
// or ↑↓ (equivalent)
import { Cache } from 'file-system-cache';
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.
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.
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.
Deletes the specified cache item from the file-system.
cache.remove("foo")
.then(() => /* Removed */)
Clears all cached items from the file-system.
cache.clear()
.then(() => /* All items deleted */)
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. */)
Loads all files within the cache's namespace.
cache.load()
.then(result => /* The complete of cached files (for the ns). */)
# Run tests.
npm test
FAQs
A super-fast, promise-based cache that reads and writes to the file-system.
The npm package file-system-cache receives a total of 2,668,959 weekly downloads. As such, file-system-cache popularity was classified as popular.
We found that file-system-cache demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
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.
Product
We're excited to announce that Socket now supports the Java programming language.
Security News
Socket detected a malicious Python package impersonating a popular browser cookie library to steal passwords, screenshots, webcam images, and Discord tokens.
Security News
Deno 2.0 is now available with enhanced package management, full Node.js and npm compatibility, improved performance, and support for major JavaScript frameworks.