A lightweight Node.js caching library that implements key-value cache storage based on local JSON files, with an API style similar to the browser's localStorage.
⚠️ Important Notice: This is a simple single-process caching system that does not support multi-process, multi-threaded, or cluster environments. It is only suitable for command-line tools, development environments, single-machine scripts, and other single-process scenarios. For concurrent support, please use professional solutions like Redis or SQLite.
- 🚀 Simple and easy to use, API design similar to localStorage
- 💾 Persistent storage based on local JSON files
- 🔑 Support for key-value pair caching
- 📁 Customizable cache file storage path
- ⚡ Lightweight with no external dependencies
- 🔒 Complete TypeScript type definitions
- 🗂️ Intelligent MD5 sharding storage, avoiding large file read/write operations
- 📊 3-level nested directory structure, optimizing file system performance
npm install node-json-file-cacheOr using yarn:
yarn add node-json-file-cacheconst JsonFileCache = require('node-json-file-cache');
// Initialize cache object with cache folder path
const cache = new JsonFileCache('./cache');
// Set cache
cache.setItem('username', 'zhangsan');
cache.setItem('userInfo', { id: 1, name: 'zhangsan', age: 25 });
// Get cache
const username = cache.getItem('username');
console.log(username); // 'zhangsan'
const userInfo = cache.getItem('userInfo');
console.log(userInfo); // { id: 1, name: 'zhangsan', age: 25 }
// Remove cache
cache.removeItem('username');
// Clear all cache
cache.clear();
// Get all cache keys
const keys = cache.keys();
console.log(keys); // ['userInfo']
// Get cache item count
const length = cache.length;
console.log(length); // 1import JsonFileCache from 'node-json-file-cache';
// Define type
interface UserInfo {
id: number;
name: string;
age: number;
}
// Initialize cache object
const cache = new JsonFileCache('./cache');
// Set cache with type
cache.setItem<string>('username', 'zhangsan');
cache.setItem<UserInfo>('userInfo', { id: 1, name: 'zhangsan', age: 25 });
// Get cache with type hints
const username = cache.getItem<string>('username');
const userInfo = cache.getItem<UserInfo>('userInfo');
// TypeScript provides complete type checking and intelligent hints
if (userInfo) {
console.log(userInfo.name); // Type-safe
}const cache = new JsonFileCache(cachePath, options);Parameters:
cachePath(string): Folder path where cache files are storedoptions(object, optional): Configuration optionsfilename(string): Cache file name, defaults tocache.jsonautoSave(boolean): Whether to auto-save, defaults totrue
Set a cache item.
cache.setItem('key', 'value');
cache.setItem('user', { name: 'zhangsan' });Parameters:
key(string): Cache key namevalue(any): Cache value, supports any serializable JavaScript type
Return value: void
Get a cache item.
const value = cache.getItem('key');Parameters:
key(string): Cache key name
Return value: Cache value, or null if not found
Delete a specific cache item.
cache.removeItem('key');Parameters:
key(string): Cache key name
Return value: void
Clear all cache.
cache.clear();Return value: void
Get all cache key names.
const allKeys = cache.keys();
console.log(allKeys); // ['key1', 'key2', 'key3']Return value: string[] - Array of all cache key names
Get the number of cache items (property).
const count = cache.length;
console.log(count); // 3Return value: number - Number of cache items
1. Command-line Tool Configuration
// CLI tool saves user configuration
const cache = new JsonFileCache('~/.my-cli/cache');
cache.setItem('apiKey', 'xxx');
cache.setItem('lastUpdate', Date.now());2. Development Environment Data Mocking
// Cache API responses during development to avoid frequent requests
const cache = new JsonFileCache('./dev-cache');
const mockData = cache.getItem('userList');
if (!mockData) {
const data = await fetchFromAPI();
cache.setItem('userList', data);
}3. Single-Machine Script Data Persistence
// Web scraper script saves progress
const cache = new JsonFileCache('./crawler-cache');
cache.setItem('lastCrawledPage', 100);
cache.setItem('processedUrls', urlList);4. Local Application Configuration Storage
// Electron app saves user preferences
const cache = new JsonFileCache(app.getPath('userData'));
cache.setItem('theme', 'dark');
cache.setItem('language', 'en-US');- Web Servers: Multi-process/cluster environments lead to data inconsistency
- High-Concurrency Applications: No locking mechanism, cannot guarantee data safety
- Distributed Systems: Does not support cross-machine cache sharing
- Scenarios Requiring Transactions: No ACID guarantees
- High Real-Time Requirements: Synchronous I/O may block the main thread
This library provides complete TypeScript type definition files (.d.ts), no need to install @types packages separately.
In TypeScript or JSDoc-supporting editors (like VSCode), you will get:
- Complete method parameter hints
- Return value type inference
- Detailed documentation comments
- Intelligent code completion
// Use generics to specify value types
const user = cache.getItem<UserInfo>('user');
const count = cache.getItem<number>('count');
const tags = cache.getItem<string[]>('tags');This library uses an MD5 hash sharding storage strategy. For detailed design, please refer to ARCHITECTURE.md.
Core Features:
- First 3 MD5 characters serve as 3-level nested folders (e.g.,
a/b/c/) - 4th MD5 character serves as file name (e.g.,
d.json) - Complete MD5 used as key within files to avoid conflicts
- Theoretically supports 65,536 different file shards
- Cache data is stored in JSON format, so only serializable data types are supported
- Functions, Symbols, and other non-serializable types are not supported
⚠️ Does not support multi-process concurrent access: Simultaneous read/write by multiple processes may corrupt data⚠️ Does not support multi-threaded concurrent access: No file locking mechanism, thread safety not guaranteed⚠️ Does not support cluster environments: Not suitable for use in Node.js cluster mode- Recommended for use in single-process, single-threaded environments
- Suitable for small to medium-sized data caching (< 1 million records)
- File operations are synchronous, large-scale writes may impact performance
- Each read/write operation performs file I/O
- ✅ Local cache for single-process applications
- ✅ Temporary data storage in development environments
- ✅ Configuration cache for command-line tools
- ✅ Data persistence for single-machine scripts
- ❌ High-concurrency applications in production
- ❌ Multi-process/cluster deployed applications
- ❌ Scenarios requiring transaction support
- ❌ Browser environments
MIT
Issues and Pull Requests are welcome!
[Your Name]
- Initial release
- Implemented basic localStorage-style API
- Support for JSON file persistent storage