@onurege3467/zerohelper
Version:
ZeroHelper is a versatile high-performance utility library and database framework for Node.js, fully written in TypeScript.
93 lines (92 loc) ⢠4.23 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.cacheCommand = void 0;
const commander_1 = require("commander");
const chalk_1 = __importDefault(require("chalk"));
const ora_1 = __importDefault(require("ora"));
const config_1 = require("../utils/config");
exports.cacheCommand = new commander_1.Command().name('cache');
exports.cacheCommand
.command('clear')
.description('Clear all cache')
.option('-c, --config <path>', 'Path to config file', 'zero.config.ts')
.action(async (options) => {
const spinner = (0, ora_1.default)('Clearing cache...').start();
try {
const db = await (0, config_1.getDatabase)(options.config);
const metrics = db.getMetrics();
const beforeHits = metrics.cache?.hits || 0;
const beforeMisses = metrics.cache?.misses || 0;
await db.insert('_cache_clear', { timestamp: Date.now() });
await db.delete('_cache_clear', { timestamp: Date.now() });
const afterMetrics = db.getMetrics();
const afterHits = afterMetrics.cache?.hits || 0;
const afterMisses = afterMetrics.cache?.misses || 0;
spinner.succeed(chalk_1.default.green('ā
Cache cleared'));
console.log(chalk_1.default.gray(` Previous hits: ${beforeHits}`));
console.log(chalk_1.default.gray(` Previous misses: ${beforeMisses}`));
await db.close();
}
catch (error) {
spinner.fail(chalk_1.default.red('ā Cache clear failed'));
console.error(chalk_1.default.red(error.message));
process.exit(1);
}
});
exports.cacheCommand
.command('stats')
.description('Show detailed cache statistics')
.option('-c, --config <path>', 'Path to config file', 'zero.config.ts')
.action(async (options) => {
try {
const db = await (0, config_1.getDatabase)(options.config);
const metrics = db.getMetrics();
console.log(chalk_1.default.bold('\nš Cache Statistics'));
console.log(chalk_1.default.gray('ā'.repeat(50)));
if (!metrics.cache) {
console.log(chalk_1.default.yellow(' No cache statistics available'));
console.log(chalk_1.default.gray(' Cache may not be enabled for this adapter'));
}
else {
const hits = metrics.cache.hits || 0;
const misses = metrics.cache.misses || 0;
const total = hits + misses;
const hitRate = total > 0 ? (hits / total) * 100 : 0;
console.log(chalk_1.default.cyan(' Cache Hits:'), chalk_1.default.green(hits.toLocaleString()));
console.log(chalk_1.default.cyan(' Cache Misses:'), chalk_1.default.red(misses.toLocaleString()));
console.log(chalk_1.default.cyan(' Total Requests:'), chalk_1.default.white(total.toLocaleString()));
console.log(chalk_1.default.cyan(' Hit Rate:'), chalk_1.default.white(`${hitRate.toFixed(2)}%`));
let healthStatus = 'ā';
let healthColor = chalk_1.default.red;
if (hitRate >= 90) {
healthStatus = 'ā
Excellent';
healthColor = chalk_1.default.green;
}
else if (hitRate >= 70) {
healthStatus = 'ā ļø Good';
healthColor = chalk_1.default.yellow;
}
else if (hitRate >= 50) {
healthStatus = 'ā ļø Fair';
healthColor = chalk_1.default.yellow;
}
else {
healthStatus = 'ā Poor';
healthColor = chalk_1.default.red;
}
console.log(chalk_1.default.cyan(' Health:'), healthColor(healthStatus));
if (hitRate < 70) {
console.log(chalk_1.default.gray('\n š” Tip: Consider increasing cache TTL for better hit rate'));
}
}
console.log(chalk_1.default.gray('\n' + 'ā'.repeat(50)));
await db.close();
}
catch (error) {
console.error(chalk_1.default.red(`Error: ${error.message}`));
process.exit(1);
}
});