UNPKG

@vessl-ai/mcpctl-control-plane

Version:

Vessl MCP Control Plane service for managing distributed jobs.

132 lines 4.79 kB
var AppCacheService_1; import { __decorate, __metadata, __param } from "tslib"; import { Cache, CACHE_MANAGER } from '@nestjs/cache-manager'; import { Inject, Injectable, Logger } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { CacheableMemory } from 'cacheable'; import { existsSync } from 'fs'; import * as fs from 'fs/promises'; import * as path from 'path'; let AppCacheService = AppCacheService_1 = class AppCacheService { cacheManager; configService; logger = new Logger(AppCacheService_1.name); MEMORY_CACHE_FILE = 'memory-cache.json'; constructor(cacheManager, configService) { this.cacheManager = cacheManager; this.configService = configService; this.restore(); } isOnlyMemoryStore() { return (this.cacheManager.stores.some((store) => store.store instanceof CacheableMemory) && this.cacheManager.stores.length === 1); } async restore() { if (this.isOnlyMemoryStore()) { this.logger.log('Restoring cache...'); const backupDir = this.configService.get('cache')?.defaultCache.backupDir; if (!backupDir) { this.logger.error('No backup directory found'); return; } const backupPath = path.join(backupDir, this.MEMORY_CACHE_FILE); if (!existsSync(backupPath)) { this.logger.error('No backup file found'); return; } const entries = JSON.parse(await fs.readFile(backupPath, 'utf8')); for (const [key, value] of Object.entries(entries)) { await this.cacheManager.set(key, value); } this.logger.log('Cache restored'); } } async backup() { this.logger.log('Backing up cache...'); if (this.isOnlyMemoryStore()) { this.logger.log('Backing up memory cache since only it is used'); const store = this.cacheManager.stores.filter((store) => store.store instanceof CacheableMemory)[0]; if (!store) { this.logger.error('No memory store found'); return; } if (!store.iterator) { this.logger.error('No iterator found'); return; } const entries = {}; for await (const [key, value] of store.iterator(undefined)) { entries[key] = value; } const backupDir = this.configService.get('cache')?.backupDir; if (!backupDir) { this.logger.error('No backup directory found'); return; } if (!existsSync(backupDir)) { await fs.mkdir(backupDir, { recursive: true }); } const backupPath = path.join(backupDir, this.MEMORY_CACHE_FILE); await fs.writeFile(backupPath, JSON.stringify(entries, null, 2)); this.logger.log('Memory cache backed up'); } } async get(key) { return this.cacheManager.get(key); } async mget(keys) { return this.cacheManager.mget(keys); } async ttl(key) { return this.cacheManager.ttl(key); } async set(key, value, ttl) { await this.cacheManager.set(key, value, ttl); return value; } async mset(list) { await this.cacheManager.mset(list); return list; } async del(key) { return this.cacheManager.del(key); } async mdel(keys) { return this.cacheManager.mdel(keys); } async clear() { return this.cacheManager.clear(); } on(event, listener) { return this.cacheManager.on(event, listener); } off(event, listener) { return this.cacheManager.off(event, listener); } async disconnect() { return this.cacheManager.disconnect(); } get cacheId() { return this.cacheManager.cacheId; } get stores() { return this.cacheManager.stores; } async wrap(key, fnc, ttl, refreshThreshold) { const safeTtl = typeof ttl === 'number' || (typeof ttl === 'function' && ttl.length === 1) ? ttl : undefined; const safeRefresh = typeof refreshThreshold === 'number' || (typeof refreshThreshold === 'function' && refreshThreshold.length === 1) ? refreshThreshold : undefined; return this.cacheManager.wrap(key, fnc, safeTtl, safeRefresh); } }; AppCacheService = AppCacheService_1 = __decorate([ Injectable(), __param(0, Inject(CACHE_MANAGER)), __metadata("design:paramtypes", [Cache, ConfigService]) ], AppCacheService); export { AppCacheService }; //# sourceMappingURL=appcache.service.js.map