officernd-mcp-server
Version:
MCP server for OfficeRnD workspace management - create, search, update and cancel bookings
77 lines • 1.95 kB
JavaScript
export class ResourceCache {
cache = new Map();
cacheLifetime; // in milliseconds
constructor(cacheLifetimeMinutes = 60) {
this.cacheLifetime = cacheLifetimeMinutes * 60 * 1000;
}
/**
* Get a resource from cache if it exists and is not expired
*/
get(resourceId) {
const cached = this.cache.get(resourceId);
if (!cached) {
return null;
}
const now = new Date();
const age = now.getTime() - cached.fetchedAt.getTime();
if (age > this.cacheLifetime) {
// Cache expired
this.cache.delete(resourceId);
return null;
}
return cached.resource;
}
/**
* Store a resource in cache
*/
set(resource) {
this.cache.set(resource._id, {
resource,
fetchedAt: new Date(),
});
}
/**
* Store multiple resources in cache
*/
setMany(resources) {
const now = new Date();
resources.forEach(resource => {
this.cache.set(resource._id, {
resource,
fetchedAt: now,
});
});
}
/**
* Clear all cached resources
*/
clear() {
this.cache.clear();
}
/**
* Get cache statistics
*/
getStats() {
const resources = Array.from(this.cache.keys());
return {
size: this.cache.size,
resources,
};
}
/**
* Remove expired entries from cache
*/
cleanExpired() {
const now = new Date();
let removed = 0;
for (const [key, value] of this.cache.entries()) {
const age = now.getTime() - value.fetchedAt.getTime();
if (age > this.cacheLifetime) {
this.cache.delete(key);
removed++;
}
}
return removed;
}
}
//# sourceMappingURL=resource-cache.js.map