Add cache hit/miss metrics for regex pattern cache #64

Closed
opened 2026-03-11 17:38:26 +03:00 by NiXTheDev · 0 comments
NiXTheDev commented 2026-03-11 17:38:26 +03:00 (Migrated from github.com)

Overview

Expose cache hit/miss ratios from the LRU regex cache to help operators tune CACHE_MAX_SIZE and CACHE_TTL_MS for their workload.

Implementation

  1. Update utils.ts – Modify getRegexCacheStats() to include:

    • hits: number – Total cache hits since start
    • misses: number – Total cache misses
    • hitRatio: number – Calculated ratio (hits / (hits + misses))
    • missRatio: number – Calculated ratio (misses / (hits + misses))
  2. Update LRUCache class – Add internal counters for hits and misses, increment them in get() and set() appropriately.

  3. Update metrics.ts – Include these new stats in the /metrics command output, e.g.:

Cache Stats:
Enabled: true
Size: 847/1000
TTL: 300s
Hits: 15234
Misses: 891
Hit Ratio: 94.5%
  1. Consider resetting counters – Optionally provide a way to reset stats (e.g., on bot restart or via a hidden command).

Notes

  • Stats should be cumulative since bot start (ephemeral, matches database behavior).
  • No persistence needed.
  • Helps operators identify if cache is too small (low hit ratio) or TTL too short.
## Overview Expose cache hit/miss ratios from the LRU regex cache to help operators tune `CACHE_MAX_SIZE` and `CACHE_TTL_MS` for their workload. ## Implementation 1. **Update `utils.ts`** – Modify `getRegexCacheStats()` to include: - `hits: number` – Total cache hits since start - `misses: number` – Total cache misses - `hitRatio: number` – Calculated ratio (hits / (hits + misses)) - `missRatio: number` – Calculated ratio (misses / (hits + misses)) 2. **Update `LRUCache` class** – Add internal counters for hits and misses, increment them in `get()` and `set()` appropriately. 3. **Update `metrics.ts`** – Include these new stats in the `/metrics` command output, e.g.: ``` Cache Stats: Enabled: true Size: 847/1000 TTL: 300s Hits: 15234 Misses: 891 Hit Ratio: 94.5% ``` 4. **Consider resetting counters** – Optionally provide a way to reset stats (e.g., on bot restart or via a hidden command). ## Notes - Stats should be cumulative since bot start (ephemeral, matches database behavior). - No persistence needed. - Helps operators identify if cache is too small (low hit ratio) or TTL too short.
Sign in to join this conversation.
No description provided.