aifordiscord-api
Version:
An advanced npm package for Discord bots providing AI-enhanced random content, memes, jokes, and utilities with comprehensive documentation.
61 lines • 1.61 kB
JavaScript
;
/**
* Simple in-memory cache implementation
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SimpleCache = void 0;
class SimpleCache {
constructor(defaultTTL = 300000) {
this.cache = new Map();
this.defaultTTL = defaultTTL;
this.startCleanupInterval();
}
set(key, data, ttl) {
const entry = {
data,
timestamp: Date.now(),
ttl: ttl || this.defaultTTL
};
this.cache.set(key, entry);
}
get(key) {
const entry = this.cache.get(key);
if (!entry)
return null;
const now = Date.now();
if (now - entry.timestamp > entry.ttl) {
this.cache.delete(key);
return null;
}
return entry.data;
}
has(key) {
const entry = this.cache.get(key);
if (!entry)
return false;
const now = Date.now();
if (now - entry.timestamp > entry.ttl) {
this.cache.delete(key);
return false;
}
return true;
}
delete(key) {
return this.cache.delete(key);
}
clear() {
this.cache.clear();
}
startCleanupInterval() {
setInterval(() => {
const now = Date.now();
for (const [key, entry] of this.cache.entries()) {
if (now - entry.timestamp > entry.ttl) {
this.cache.delete(key);
}
}
}, 60000); // Cleanup every minute
}
}
exports.SimpleCache = SimpleCache;
//# sourceMappingURL=cache.js.map