Skip to content

feat: implement caching for user admission check#449

Open
Mohit-Davar wants to merge 1 commit intocameri:mainfrom
Mohit-Davar:cache
Open

feat: implement caching for user admission check#449
Mohit-Davar wants to merge 1 commit 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.

@Mohit-Davar Mohit-Davar force-pushed the cache branch 4 times, most recently from 81e8d30 to 40bcf16 Compare April 11, 2026 13:03
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.

3 participants