@simonecoelhosfo/optimizely-mcp-server
Version:
Optimizely MCP Server for AI assistants with integrated CLI tools
116 lines • 3.77 kB
JavaScript
/**
* Cache Invalidation Helper for Sync Process
*
* Provides a clean way to integrate cache invalidation with the
* existing IncrementalSyncManager without modifying its code.
*/
import { getLogger } from '../logging/Logger.js';
const logger = getLogger();
/**
* Helper class to track sync operations and trigger cache invalidation
*/
export class CacheInvalidationHelper {
syncHandler;
pendingInvalidations = new Map();
activeSyncs = new Set();
constructor(syncHandler) {
this.syncHandler = syncHandler;
if (this.syncHandler) {
logger.info('CacheInvalidationHelper initialized with sync handler');
}
}
/**
* Call when starting a project sync
*/
async onProjectSyncStart(projectId) {
if (!this.syncHandler)
return;
this.activeSyncs.add(projectId);
await this.syncHandler.onSyncStart(projectId);
}
/**
* Call when completing a project sync
*/
async onProjectSyncComplete(projectId) {
if (!this.syncHandler)
return;
this.activeSyncs.delete(projectId);
// Flush any pending invalidations for this project
const pending = this.pendingInvalidations.get(projectId);
if (pending && pending.length > 0) {
const entities = pending.map(e => ({
entity: e.entity,
entityId: e.entityId || e.entityKey || ''
}));
await this.syncHandler.onBulkSync(entities, projectId);
this.pendingInvalidations.delete(projectId);
}
await this.syncHandler.onSyncComplete(projectId);
}
/**
* Call after syncing an entity
*/
async onEntitySynced(projectId, entityType, entityId, entityKey, operation = 'update') {
if (!this.syncHandler)
return;
const event = {
type: operation === 'delete' ? 'entity_deleted' : 'entity_synced',
entity: entityType,
entityId,
entityKey,
projectId,
operation,
};
// If sync is active, batch the invalidations
if (this.activeSyncs.has(projectId)) {
if (!this.pendingInvalidations.has(projectId)) {
this.pendingInvalidations.set(projectId, []);
}
this.pendingInvalidations.get(projectId).push(event);
}
else {
// Otherwise, invalidate immediately
await this.syncHandler.onEntitySynced(event);
}
}
/**
* Call when an entity is deleted
*/
async onEntityDeleted(projectId, entityType, entityId) {
await this.onEntitySynced(projectId, entityType, entityId, undefined, 'delete');
}
/**
* Manually trigger cache invalidation for specific entities
*/
async invalidateEntities(projectId, entities) {
if (!this.syncHandler)
return;
for (const entity of entities) {
await this.onEntitySynced(projectId, entity.type, entity.id, entity.key, 'update');
}
}
/**
* Check if invalidation is enabled
*/
isEnabled() {
return !!this.syncHandler;
}
}
/**
* Example of how to use the helper in your sync code:
*
* ```typescript
* // In your sync manager or wherever you handle syncing
* const cacheHelper = new CacheInvalidationHelper(queryEngine?.getSyncCacheHandler());
*
* // When starting a sync
* await cacheHelper.onProjectSyncStart(projectId);
*
* // After syncing each entity
* await cacheHelper.onEntitySynced(projectId, 'flags', undefined, flagKey, 'update');
*
* // When sync is complete
* await cacheHelper.onProjectSyncComplete(projectId);
* ```
*/
//# sourceMappingURL=CacheInvalidationHelper.js.map