What is async-disk-cache?
The async-disk-cache npm package is a simple and efficient way to cache data on disk asynchronously. It is useful for scenarios where you need to store and retrieve data quickly without relying on in-memory storage, which can be volatile and limited in size.
What are async-disk-cache's main functionalities?
Set Cache
This feature allows you to set a cache entry with a specific key and value. The data is stored on disk asynchronously.
const Cache = require('async-disk-cache');
const cache = new Cache('my-cache');
async function setCache(key, value) {
await cache.set(key, value);
console.log(`Cache set for key: ${key}`);
}
setCache('exampleKey', 'exampleValue');
Get Cache
This feature allows you to retrieve a cache entry by its key. It checks if the data is cached and returns the value if it exists.
const Cache = require('async-disk-cache');
const cache = new Cache('my-cache');
async function getCache(key) {
const result = await cache.get(key);
if (result.isCached) {
console.log(`Cache hit for key: ${key}, value: ${result.value}`);
} else {
console.log(`Cache miss for key: ${key}`);
}
}
getCache('exampleKey');
Remove Cache
This feature allows you to remove a cache entry by its key. The data is deleted from the disk asynchronously.
const Cache = require('async-disk-cache');
const cache = new Cache('my-cache');
async function removeCache(key) {
await cache.remove(key);
console.log(`Cache removed for key: ${key}`);
}
removeCache('exampleKey');
Clear Cache
This feature allows you to clear all cache entries. It removes all data stored in the cache asynchronously.
const Cache = require('async-disk-cache');
const cache = new Cache('my-cache');
async function clearCache() {
await cache.clear();
console.log('All cache cleared');
}
clearCache();
Other packages similar to async-disk-cache
node-cache
node-cache is an in-memory caching module for Node.js. It provides similar functionalities like setting, getting, and deleting cache entries, but it stores data in memory rather than on disk. This makes it faster but less persistent compared to async-disk-cache.
lru-cache
lru-cache is a Least Recently Used (LRU) cache implementation for Node.js. It is designed to store data in memory with a limit on the number of items it can hold. It automatically removes the least recently used items when the limit is reached. Unlike async-disk-cache, it does not persist data on disk.
flat-cache
flat-cache is a lightweight disk-based cache for Node.js. It provides similar functionalities to async-disk-cache, such as setting, getting, and removing cache entries. However, flat-cache is designed to be simpler and more lightweight, making it suitable for smaller projects.
async-disk-cache

An async disk cache. inspired by jgable/cache-swap
A sync sibling version is also available: stefanpenner/sync-disk-cache
By default, this will usge TMPDIR/<username>/
for storage, but this can be changed by setting the $TMPDIR
environment variable.
Example
var Cache = require('async-disk-cache');
var cache = new Cache('my-cache');
cache.has('foo').then(function(wasFooFound) {
});
cache.get('foo').then(function(cacheEntry) {
cacheEntry === {
isCached: true,
key: 'foo',
value: 'content of foo'
}
});
cache.get('foo').then(function(cacheEntry) {
cacheEntry === {
isCached: false,
key: 'foo',
value: undefined
}
});
cache.set('foo', 'content of foo').then(function() {
});
cache.remove('foo').then(function() {
})
cache.clear().then(function() {
})
Enable compression:
var Cache = require('async-disk-cache');
var cache = new Cache('my-cache', {
compression: 'gzip' | 'deflate' | 'deflateRaw',
supportBuffer: 'true' | 'false'
})
HELP!...my TMP dir is growing unbounded!
description
In general most OS distributions come with cron like tasks, which purge unused files in $TMPDIR
. For example, ubuntu typically uses tmpreaper
and macOS uses various tasks in /etc/periodic/*
.
options
If your OS distribution does not provide such a cleanup mechanism:
a) We stronglly recommend utilizing one, as other sync-disk-cache is not alone in rely on this behavior
b) If that is not possible, we recommend changing your $TMPDIR
to something project specific and manually purging it.
License
Licensed under the MIT License, Copyright 2015 Stefan Penner