cachly
Version:
Type-safe, production-ready in-memory cache system for Node.js and TypeScript with advanced features.
211 lines • 7.94 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.CachlyCLI = void 0;
const Cachly_1 = require("./Cachly");
const FSAdapter_1 = require("./adapters/FSAdapter");
const readline = __importStar(require("readline"));
class CachlyCLI {
constructor() {
this.commands = [
{
name: 'get',
description: 'Get a value from cache',
usage: 'get <key>',
execute: async (args) => {
if (args.length < 1) {
console.log('Usage: get <key>');
return;
}
const value = await this.cache.get(args[0] || '');
console.log(value ? JSON.stringify(value, null, 2) : 'Not found');
},
},
{
name: 'set',
description: 'Set a value in cache',
usage: 'set <key> <value> [ttl]',
execute: async (args) => {
if (args.length < 2) {
console.log('Usage: set <key> <value> [ttl]');
return;
}
const ttl = args[2] ? parseInt(args[2]) : undefined;
await this.cache.set(args[0] || '', args[1] || '', { ttl });
console.log('Value set successfully');
},
},
{
name: 'delete',
description: 'Delete a key from cache',
usage: 'delete <key>',
execute: async (args) => {
if (args.length < 1) {
console.log('Usage: delete <key>');
return;
}
this.cache.delete(args[0] || '');
console.log('Key deleted successfully');
},
},
{
name: 'stats',
description: 'Show cache statistics',
usage: 'stats',
execute: async () => {
const stats = this.cache.stats();
console.log('Cache Statistics:');
console.log(JSON.stringify(stats, null, 2));
},
},
{
name: 'keys',
description: 'List all keys in cache',
usage: 'keys [pattern]',
execute: async (args) => {
const pattern = args[0];
const keys = this.cache.getKeys(pattern);
console.log(`Found ${keys.length} keys:`);
keys.forEach(key => console.log(` ${key}`));
},
},
{
name: 'top',
description: 'Show top accessed keys',
usage: 'top [limit]',
execute: async (args) => {
const limit = args[0] ? parseInt(args[0]) : 10;
const topKeys = this.cache.getTopKeys(limit);
console.log(`Top ${limit} accessed keys:`);
topKeys.forEach((item, index) => {
console.log(`${index + 1}. ${item.key} (${item.accessCount} accesses)`);
});
},
},
{
name: 'tags',
description: 'List all tags',
usage: 'tags',
execute: async () => {
// This would need to be implemented in the cache class
console.log('Tags feature not yet implemented in CLI');
},
},
{
name: 'clear',
description: 'Clear all cache',
usage: 'clear',
execute: async () => {
this.cache.clear();
console.log('Cache cleared successfully');
},
},
{
name: 'health',
description: 'Show cache health status',
usage: 'health',
execute: async () => {
const health = this.cache.health();
console.log('Cache Health:');
console.log(JSON.stringify(health, null, 2));
},
},
{
name: 'help',
description: 'Show this help message',
usage: 'help',
execute: async () => {
console.log('Available commands:');
this.commands.forEach(cmd => {
console.log(` ${cmd.name.padEnd(10)} - ${cmd.description}`);
console.log(` Usage: ${cmd.usage}`);
});
},
},
{
name: 'exit',
description: 'Exit the CLI',
usage: 'exit',
execute: async () => {
this.cache.destroy();
this.rl.close();
process.exit(0);
},
},
];
this.cache = new Cachly_1.Cachly({
persistence: new FSAdapter_1.FSAdapter('./.cachly'),
log: true,
});
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
}
async start() {
console.log('Cachly CLI - Type "help" for available commands');
const askQuestion = () => {
return new Promise((resolve) => {
this.rl.question('cachly> ', resolve);
});
};
while (true) {
try {
const input = await askQuestion();
const [command, ...args] = input.trim().split(' ');
if (!command)
continue;
const cmd = this.commands.find(c => c.name === command);
if (cmd) {
await cmd.execute(args);
}
else {
console.log(`Unknown command: ${command}. Type "help" for available commands.`);
}
}
catch (error) {
console.error('Error:', error);
}
}
}
}
exports.CachlyCLI = CachlyCLI;
// Start CLI if this file is run directly
if (require.main === module) {
const cli = new CachlyCLI();
cli.start().catch(console.error);
}
//# sourceMappingURL=cli.js.map