What is sync-disk-cache?
The sync-disk-cache npm package provides a simple, synchronous API for caching data to disk. It is useful for scenarios where you need to store and retrieve data quickly without the overhead of asynchronous operations.
What are sync-disk-cache's main functionalities?
Set Cache
This feature allows you to set a key-value pair in the cache. The data is stored on disk and can be retrieved later.
const Cache = require('sync-disk-cache');
const cache = new Cache('myCache');
cache.set('key', 'value');
Get Cache
This feature allows you to retrieve a value from the cache using its key. If the key exists, the value is returned; otherwise, it returns null.
const Cache = require('sync-disk-cache');
const cache = new Cache('myCache');
const result = cache.get('key');
console.log(result.value);
Remove Cache
This feature allows you to remove a key-value pair from the cache. The specified key and its associated value are deleted from the disk.
const Cache = require('sync-disk-cache');
const cache = new Cache('myCache');
cache.remove('key');
Clear Cache
This feature allows you to clear all key-value pairs from the cache. The entire cache is emptied, removing all stored data from the disk.
const Cache = require('sync-disk-cache');
const cache = new Cache('myCache');
cache.clear();
Other packages similar to sync-disk-cache
node-cache
node-cache is an in-memory caching module for Node.js. Unlike sync-disk-cache, it does not persist data to disk, making it faster for read/write operations but less suitable for long-term storage.
flat-cache
flat-cache is a lightweight disk-based cache for Node.js. It provides similar functionality to sync-disk-cache but includes additional features like automatic cache invalidation based on time-to-live (TTL) settings.
diskcache
diskcache is another disk-based caching solution for Node.js. It offers both synchronous and asynchronous APIs, providing more flexibility compared to sync-disk-cache, which only offers synchronous operations.
sync-disk-cache
A sync disk cache. inspired by jgable/cache-swap
An async sibling version is also available: stefanpenner/async-disk-cache
Example
var Cache = require('sync-disk-cache');
var cache = new Cache('my-cache');
cache.has('foo') === wasFooFound;
cache.get('foo') === {
isCached: true,
path: 'foo',
content: 'content of foo'
}
cache.get('foo') === {
isCached: false,
path: 'foo',
content: undefined
}
cache.set('foo', 'content of foo');
cache.clear();
Enable compression:
note: node 0.10.x does not support the synchronous zlib method variants, so compression is only available > 0.10.x
var Cache = require('sync-disk-cache');
var cache = new Cache('my-cache', {
compression: 'gzip' | 'deflate' | 'deflateRaw'
})
License
Licensed under the MIT License, Copyright 2015 Stefan Penner