@onurege3467/fastcache
v2.0.0
Published
JSON based database and cache for Node.js applications
Maintainers
Readme
FastCache
A JSON-based database caching library for Node.js.
Features
- Use JSON files as a database
- Fast access with in-memory caching
- CRUD (Create, Read, Update, Delete) operations
- Asynchronous API
- Nested data structures (with dot notation)
- Filtering and batch operations
- Automatic file and folder management
Installation
npm install @onurege3467/fastcacheBasic Usage
Creating a Database
const FastCache = require('@onurege3467/fastcache');
const cache = new FastCache({
filePath: './data/cache.json', // Database file path
cacheTime: 3600 // Cache duration (seconds) - default: 3600 (1 hour)
});Adding Data (Set)
// Add single data
await cache.set('users.123', {
id: 123,
name: 'Ahmet',
email: '[email protected]'
});
// Add nested data
await cache.set('settings.theme.colors.primary', '#3498cache');Reading Data (Get)
// Read single data
const user = await cache.get('users.123');
console.log(user);
// Read nested data
const primaryColor = await cache.get('settings.theme.colors.primary');
console.log(primaryColor); // #3498cache
// Get all database data
const allData = await cache.getAll();
console.log(allData);Updating Data (Update)
// Update existing data (merges objects)
await cache.update('users.123', {
age: 28,
country: 'Turkey'
});
// Result: users.123 now has name, email, age, and country fieldsDeleting Data (Delete)
// Delete data
const deleted = await cache.delete('users.123');
console.log(deleted); // true or falseAdvanced Usage
Bulk Data Addition
await cache.bulkSet({
'users.124': { name: 'Ayşe', age: 32 },
'users.125': { name: 'Mehmet', age: 45 },
'settings.version': '1.0.0'
});Filtering
// Find users older than 30
const adultUsers = await cache.filter('users', user => user.age > 30);
console.log(adultUsers);Cache Management
// Check cache status
const isCached = cache.isCached('users.123');
console.log('Is cached:', isCached);
// Clear the entire cache
cache.clearCache();
// Refresh the cache for a specific record
await cache.refreshCache('users.123');Statistics and Checks
// Count elements
const userCount = await cache.count('users');
console.log('User count:', userCount);
// Check existence
const exists = await cache.exists('users.123');
console.log('Does users.123 exist:', exists);Error Handling
try {
await cache.set('users.123', { name: 'Ahmet' });
} catch (err) {
console.error('Database error:', err.message);
}