gather-ts
Version:
A powerful code analysis and packaging tool designed for creating AI-friendly code representations for javascript and typescript projects.
201 lines • 6.71 kB
JavaScript
"use strict";
// src/core/dependency/DependencyCache.ts
Object.defineProperty(exports, "__esModule", { value: true });
exports.DependencyCache = void 0;
const errors_1 = require("@/errors");
class DependencyCache {
constructor(deps, options = {}) {
this.deps = deps;
this.isInitialized = false;
this.debug = options.debug || false;
this.timeout = options.timeout || 5 * 60 * 1000; // 5 minutes default
this.cache = new Map();
this.stats = {
size: 0,
hits: 0,
misses: 0,
oldestEntry: null,
averageAge: 0,
invalidations: 0,
errors: 0,
};
this.cacheDuration = 0;
}
initializeStats() {
this.stats = {
size: 0,
hits: 0,
misses: 0,
oldestEntry: null,
averageAge: 0,
invalidations: 0,
errors: 0,
};
}
async initialize(options = {}) {
this.logDebug("Initializing DependencyCache");
try {
if (this.isInitialized) {
this.logDebug("DependencyCache already initialized");
return;
}
if (options.force) {
this.cache.clear();
this.logDebug("Forced cache clear during initialization");
}
if (options.timeout) {
this.cacheDuration = options.timeout;
this.logDebug(`Cache timeout set to ${options.timeout}ms`);
}
this.cache.clear();
this.initializeStats();
this.isInitialized = true;
this.logDebug("DependencyCache initialization complete");
}
catch (error) {
throw new errors_1.CacheError(`Failed to initialize cache: ${error instanceof Error ? error.message : String(error)}`, "initialize");
}
}
cleanup() {
this.logDebug("Cleaning up DependencyCache");
this.cache.clear();
this.initializeStats();
this.isInitialized = false;
}
logDebug(message) {
if (this.debug) {
this.deps.logger.debug(message);
}
}
handleError(operation, error, key) {
const message = error instanceof Error ? error.message : String(error);
const cacheError = new errors_1.CacheError(`Cache ${operation} failed: ${message}`, operation, key);
this.stats.errors++;
this.deps.logger.error(cacheError.message);
throw cacheError;
}
get(key, options = {}) {
if (!this.isInitialized) {
throw new errors_1.CacheError("Cache not initialized", "read");
}
this.logDebug(`Getting cache entry for key: ${key}`);
try {
const entry = this.cache.get(key);
if (!entry) {
this.stats.misses++;
this.logDebug(`Cache miss for key: ${key}`);
return null;
}
const age = Date.now() - entry.timestamp;
if (age > (options.timeout || this.timeout)) {
this.cache.delete(key);
this.stats.invalidations++;
this.stats.misses++;
this.logDebug(`Cache entry expired for key: ${key}`);
return null;
}
this.stats.hits++;
this.logDebug(`Cache hit for key: ${key}`);
return entry.dependencies;
}
catch (error) {
this.handleError("read", error, key);
}
}
set(key, dependencies, options = {}) {
if (!this.isInitialized) {
throw new errors_1.CacheError("Cache not initialized", "write");
}
this.logDebug(`Setting cache entry for key: ${key}`);
try {
const entry = {
dependencies,
timestamp: Date.now(),
hash: this.computeHash(dependencies),
timeout: options.timeout || this.cacheDuration,
};
if (options.force) {
this.logDebug(`Force writing cache entry for ${key}`);
this.cache.set(key, entry);
}
else if (!this.cache.has(key)) {
this.cache.set(key, entry);
}
this.updateStats();
this.logDebug(`Cache entry set for key: ${key}`);
}
catch (error) {
this.handleError("write", error, key);
}
}
delete(key) {
if (!this.isInitialized) {
throw new errors_1.CacheError("Cache not initialized", "delete");
}
this.logDebug(`Deleting cache entry for key: ${key}`);
try {
const deleted = this.cache.delete(key);
if (deleted) {
this.updateStats();
this.logDebug(`Cache entry deleted for key: ${key}`);
}
}
catch (error) {
this.handleError("delete", error, key);
}
}
clear() {
if (!this.isInitialized) {
throw new errors_1.CacheError("Cache not initialized", "clear");
}
this.logDebug("Clearing cache");
try {
this.cache.clear();
this.initializeStats();
this.logDebug("Cache cleared");
}
catch (error) {
this.handleError("clear", error);
}
}
has(key) {
if (!this.isInitialized) {
throw new errors_1.CacheError("Cache not initialized", "read");
}
const entry = this.cache.get(key);
if (!entry)
return false;
const age = Date.now() - entry.timestamp;
if (age > this.timeout) {
this.cache.delete(key);
this.stats.invalidations++;
return false;
}
return true;
}
computeHash(dependencies) {
return JSON.stringify(dependencies);
}
updateStats() {
const entries = Array.from(this.cache.values());
let oldestTimestamp = Date.now();
let totalAge = 0;
entries.forEach((entry) => {
const age = Date.now() - entry.timestamp;
oldestTimestamp = Math.min(oldestTimestamp, entry.timestamp);
totalAge += age;
});
this.stats = {
...this.stats,
size: this.cache.size,
oldestEntry: this.cache.size > 0 ? oldestTimestamp : null,
averageAge: this.cache.size > 0 ? totalAge / this.cache.size : 0,
};
}
getStats() {
this.updateStats();
return { ...this.stats };
}
}
exports.DependencyCache = DependencyCache;
//# sourceMappingURL=DependencyCache.js.map