@smartbear/mcp
Version:
MCP server for interacting SmartBear Products
64 lines (63 loc) • 1.56 kB
JavaScript
import NodeCache from "node-cache";
/**
* Common cache service that can be shared across all clients.
* Wraps NodeCache and provides a way to disable caching entirely.
* Reads CACHE_ENABLED and CACHE_TTL environment variables for configuration.
*/
export class CacheService {
cache;
enabled;
constructor() {
// Read configuration from environment variables
this.enabled = process.env.CACHE_ENABLED !== "false";
const ttl = process.env.CACHE_TTL
? Number.parseInt(process.env.CACHE_TTL, 10)
: 86400; // Default 24 hours
this.cache = this.enabled
? new NodeCache({
stdTTL: ttl,
})
: null;
}
/**
* Get a value from the cache
*/
get(key) {
if (!this.enabled || !this.cache) {
return undefined;
}
return this.cache.get(key);
}
/**
* Set a value in the cache
*/
set(key, value) {
if (!this.enabled || !this.cache) {
return false;
}
return this.cache.set(key, value);
}
/**
* Delete a value from the cache
*/
del(key) {
if (!this.enabled || !this.cache) {
return 0;
}
return this.cache.del(key);
}
/**
* Check if caching is enabled
*/
isEnabled() {
return this.enabled;
}
/**
* Clear all cache entries
*/
flushAll() {
if (this.enabled && this.cache) {
this.cache.flushAll();
}
}
}