Skip to content

PL30-413: CacheRegistry + CacheResetListener — distributed cache flush for all services#124

Open
pankti-shah-1kosmos wants to merge 5 commits into
developfrom
feature/flush-cache
Open

PL30-413: CacheRegistry + CacheResetListener — distributed cache flush for all services#124
pankti-shah-1kosmos wants to merge 5 commits into
developfrom
feature/flush-cache

Conversation

@pankti-shah-1kosmos

@pankti-shah-1kosmos pankti-shah-1kosmos commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds shared modules for distributed cache flush across all 1Kosmos microservices. Any service can achieve 100% cache flush with 3 changes (no per-file migration needed).

New Files

CacheRegistry.js

  • Auto-patches NodeCache.prototype.init at module load — every new NodeCache() is auto-registered
  • Top-level WTM resolve (no repeated require() per flush call)
  • flushAll() → clears all registered NodeCache instances + WTM HTTP response cache
  • Returns stats: { nodeCaches, totalKeys, wtmKeys }
  • Must be the first require() in each service's index.js

CacheResetListener.js

  • Kafka consumer that listens on platform_cache_reset topic
  • Fan-out groupId per pod (HOSTNAME) — all pods flush simultaneously
  • Calls CacheRegistry.flushAll() on valid signal
  • Signature verification hook (verifySignature callback — actual ECDSA logic in private service repos)
  • Replay protection (30s window)
  • Target filtering (flush specific services or all)
  • Consumer cleanup on partial init failure (no dangling connections)
  • Fully non-fatal — service never breaks if Kafka is unavailable

WTM.js (modified)

  • Added flushCache() export — clears internal HTTP response cache

Safety

  • No secrets, keys, or crypto logic in this public repo
  • Only the verification hook interface is exposed
  • All ECDSA signing/verification lives in private service repos
  • Service never breaks if broker/topic/CaaS is unavailable

Usage (per service — 3 changes)

// 1. First line of src/index.js:
require('blockid-nodejs-helpers/CacheRegistry');

// 2. Create src/services/Kafka/CacheResetConsumer.js (self-contained wrapper)

// 3. Call in index.js:
CacheResetConsumer().catch((err) => Logger.error(err.message));

@pankti-shah-1kosmos pankti-shah-1kosmos changed the title feat: add flushCache() export to WTM for distributed cache invalidation PL30-413: CacheRegistry + CacheResetListener — distributed cache flush for all services Jul 3, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a shared, helper-library implementation for distributed cache flush across 1Kosmos microservices by auto-registering node-cache instances and listening for Kafka “cache reset” signals, plus exposing a WTM cache flush hook.

Changes:

  • Added CacheRegistry to auto-register all NodeCache instances and provide a centralized flushAll() (including WTM HTTP cache).
  • Added CacheResetListener Kafka consumer to receive cache-reset events and trigger CacheRegistry.flushAll().
  • Updated WTM to export flushCache() for clearing its internal response cache.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 8 comments.

File Description
WTM.js Exposes flushCache() so external modules can flush WTM’s internal NodeCache.
CacheResetListener.js Introduces Kafka listener logic to trigger process-wide cache flush on reset signals.
CacheRegistry.js Introduces global registry + NodeCache auto-registration hook and flushAll() entrypoint.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread CacheRegistry.js Outdated
Comment thread CacheResetListener.js
Comment thread CacheResetListener.js
Comment thread CacheResetListener.js
Comment thread CacheResetListener.js
Comment thread CacheResetListener.js
Comment thread CacheResetListener.js
Comment thread CacheResetListener.js
…ache

node-cache v5.x does not have a .init() prototype method, so the
previous patch (NodeCache.prototype.init) never fired. Instances were
never registered, causing 'O caches, 0 keys cleared' on flush.

Fix: replace the module export in require.cache with a wrapper function
that captures every new instance. All subsequent require('node-cache')
calls get the patched version.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment thread CacheRegistry.js
Comment thread CacheRegistry.js

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

Comment thread CacheResetListener.js
Comment thread CacheResetListener.js
Comment thread CacheResetListener.js
Comment thread CacheResetListener.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants