Skip to content

feat: implement caching for user admission check#449

Closed
Mohit-Davar wants to merge 2 commits intocameri:mainfrom
Mohit-Davar:cache
Closed

feat: implement caching for user admission check#449
Mohit-Davar wants to merge 2 commits intocameri:mainfrom
Mohit-Davar:cache

Conversation

@Mohit-Davar
Copy link
Copy Markdown
Contributor

Description

This PR implements the TODO: use cache in EventMessageHandler by introducing a Redis-based caching layer for user admission status.

Key Implementation Details

  • State-Aware Caching

    • Uses getKey to differentiate:
      • 1 → Admitted users
      • 0 → Blocked / insufficient balance
      • null → Cache miss
  • TTL Strategy

    • Admitted users: 300s (5 minutes)
    • Blocked / low-balance users: 60s (1 minute)
    • Prevents database hammering while keeping data reasonably fresh
  • Atomicity & Resilience

    • Uses a non-blocking cacheSet helper
    • Redis operations are async and do not impact event ingestion latency
  • Testing

    • Updated event-message-handler.spec.ts with mock cache behavior

Related Issue

Resolves the caching TODO in:
src/handlers/event-message-handler.ts
#447


Motivation and Context

On high-traffic relays, performing database checks for every event introduces significant overhead.
This change:

  • Reduces database load
  • Improves event processing latency
  • Enhances scalability under high throughput

How Has This Been Tested?

  • Unit Tests
    • Updated test/unit/handlers/event-message-handler.spec.ts

Screenshots

N/A


Types of Changes

  • Non-functional change (docs, style, minor refactor)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (would cause existing functionality to change)

Checklist

  • Code follows project style guidelines
  • Documentation updated (if required)
  • CONTRIBUTING guidelines reviewed
  • Tests added/updated
  • All tests passing

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Implements Redis-backed caching for the EventMessageHandler.isUserAdmitted check to reduce repeated database lookups during high-throughput event ingestion.

Changes:

  • Injected an ICacheAdapter into EventMessageHandler and added cache read/write logic for admission decisions.
  • Introduced a Redis-backed cache singleton in message-handler-factory for reuse across handlers.
  • Updated unit test constructions of EventMessageHandler to pass a cache adapter stub.

Reviewed changes

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

File Description
src/handlers/event-message-handler.ts Adds Redis cache read/write paths and a helper for non-blocking cache writes in isUserAdmitted.
src/factories/message-handler-factory.ts Creates/reuses a Redis cache adapter instance and injects it into EventMessageHandler.
test/unit/handlers/event-message-handler.spec.ts Updates handler instantiation to include the new cache dependency.

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

Comment thread src/handlers/event-message-handler.ts
Comment thread src/handlers/event-message-handler.ts Outdated
Comment thread test/unit/handlers/event-message-handler.spec.ts
@Mohit-Davar Mohit-Davar force-pushed the cache branch 5 times, most recently from 40bcf16 to c33d57a Compare April 11, 2026 13:15
@cameri cameri self-assigned this Apr 18, 2026
@cameri
Copy link
Copy Markdown
Owner

cameri commented Apr 18, 2026

@Mohit-Davar Please fix the failing tests

* chore(tooling): migrate npm tooling scripts to biome

* chore(biome): add biome configuration

* ci: switch lint workflow to biome

* docs: update contribution and readme for biome

* chore(lint): remove legacy eslint config files

* docs: update lint tooling wording to biome

* fix(lint): address Copilot review feedback
@cameri
Copy link
Copy Markdown
Owner

cameri commented Apr 18, 2026

@Mohit-Davar will you recreate this PR?

@Mohit-Davar Mohit-Davar reopened this Apr 18, 2026
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.

4 participants