Skip to content

Commit b8716a8

Browse files
committed
Add debug logs for dev
1 parent bd23fed commit b8716a8

4 files changed

Lines changed: 103 additions & 24 deletions

File tree

cache/__tests__/cache-metrics.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,57 @@ get_cache_stats() {
403403
curl -s "${API_BASE}/api/cache/stats" 2>/dev/null
404404
}
405405

406+
# Debug function to test if /cache/stats is causing cache entries
407+
debug_cache_stats_issue() {
408+
log_section "DEBUG: Testing if /cache/stats causes cache entries"
409+
410+
log_info "Clearing cache..."
411+
curl -s -X POST "${API_BASE}/api/cache/clear" > /dev/null 2>&1
412+
sleep 1
413+
414+
log_info "Getting initial stats..."
415+
local stats_before=$(curl -s "${API_BASE}/api/cache/stats" 2>/dev/null)
416+
local sets_before=$(echo "$stats_before" | jq -r '.sets' 2>/dev/null || echo "0")
417+
local misses_before=$(echo "$stats_before" | jq -r '.misses' 2>/dev/null || echo "0")
418+
local length_before=$(echo "$stats_before" | jq -r '.length' 2>/dev/null || echo "0")
419+
420+
log_info "Initial: sets=$sets_before, misses=$misses_before, length=$length_before"
421+
422+
log_info "Calling /cache/stats 3 more times..."
423+
for i in {1..3}; do
424+
local stats=$(curl -s "${API_BASE}/api/cache/stats" 2>/dev/null)
425+
local sets=$(echo "$stats" | jq -r '.sets' 2>/dev/null || echo "0")
426+
local misses=$(echo "$stats" | jq -r '.misses' 2>/dev/null || echo "0")
427+
local length=$(echo "$stats" | jq -r '.length' 2>/dev/null || echo "0")
428+
log_info "Call $i: sets=$sets, misses=$misses, length=$length"
429+
sleep 0.5
430+
done
431+
432+
log_info "Getting final stats..."
433+
local stats_after=$(curl -s "${API_BASE}/api/cache/stats" 2>/dev/null)
434+
local sets_after=$(echo "$stats_after" | jq -r '.sets' 2>/dev/null || echo "0")
435+
local misses_after=$(echo "$stats_after" | jq -r '.misses' 2>/dev/null || echo "0")
436+
local length_after=$(echo "$stats_after" | jq -r '.length' 2>/dev/null || echo "0")
437+
438+
log_info "Final: sets=$sets_after, misses=$misses_after, length=$length_after"
439+
440+
local sets_delta=$((sets_after - sets_before))
441+
local misses_delta=$((misses_after - misses_before))
442+
local length_delta=$((length_after - length_before))
443+
444+
log_info "Delta: sets=$sets_delta, misses=$misses_delta, length=$length_delta"
445+
446+
if [ $sets_delta -gt 0 ] || [ $misses_delta -gt 0 ]; then
447+
log_warning "⚠️ /cache/stats IS incrementing cache statistics!"
448+
log_warning "This means cache.get() or cache.set() is being called somewhere"
449+
log_warning "Check server logs for [CACHE DEBUG] messages to find the source"
450+
else
451+
log_success "✓ /cache/stats is NOT incrementing cache statistics"
452+
fi
453+
454+
echo ""
455+
}
456+
406457
# Helper: Create a test object and track it for cleanup
407458
# Returns the object ID
408459
create_test_object() {
@@ -1772,6 +1823,9 @@ main() {
17721823
get_auth_token
17731824
warmup_system
17741825

1826+
# Run debug test to check if /cache/stats increments stats
1827+
debug_cache_stats_issue
1828+
17751829
# Run optimized 5-phase test flow
17761830
log_header "Running Functionality & Performance Tests"
17771831

cache/docs/CACHE_METRICS_REPORT.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# RERUM Cache Metrics & Functionality Report
22

3-
**Generated**: Mon Oct 27 18:50:18 UTC 2025
3+
**Generated**: Tue Oct 28 16:33:49 UTC 2025
44
**Test Duration**: Full integration and performance suite
55
**Server**: http://localhost:3001
66

@@ -48,12 +48,12 @@
4848

4949
| Endpoint | Cold Cache (DB) | Warm Cache (Memory) | Speedup | Benefit |
5050
|----------|-----------------|---------------------|---------|---------|
51-
| `/query` | 348 | N/A | N/A | N/A |
52-
| `/search` | 104 | N/A | N/A | N/A |
53-
| `/searchPhrase` | 25 | N/A | N/A | N/A |
54-
| `/id` | 412 | N/A | N/A | N/A |
55-
| `/history` | 728 | N/A | N/A | N/A |
56-
| `/since` | 873 | N/A | N/A | N/A |
51+
| `/query` | 328 | N/A | N/A | N/A |
52+
| `/search` | 146 | N/A | N/A | N/A |
53+
| `/searchPhrase` | 24 | N/A | N/A | N/A |
54+
| `/id` | 411 | N/A | N/A | N/A |
55+
| `/history` | 714 | N/A | N/A | N/A |
56+
| `/since` | 713 | N/A | N/A | N/A |
5757

5858
**Interpretation**:
5959
- **Cold Cache**: First request hits database (cache miss)
@@ -70,12 +70,12 @@
7070
| Endpoint | Empty Cache | Full Cache (1000 entries) | Overhead | Impact |
7171
|----------|-------------|---------------------------|----------|--------|
7272
| `/create` | 23ms | 23ms | +0ms | ✅ Negligible |
73-
| `/update` | 421ms | 437ms | +16ms | ⚠️ Moderate |
74-
| `/patch` | 420ms | 424ms | +4ms | ✅ Negligible |
75-
| `/set` | 431ms | 424ms | -7ms |None |
76-
| `/unset` | 423ms | 423ms | +0ms |Negligible |
77-
| `/delete` | 441ms | 460ms | +19ms | ⚠️ Moderate |
78-
| `/overwrite` | 422ms | 421ms | -1ms | ✅ None |
73+
| `/update` | 420ms | 423ms | +3ms | ✅ Negligible |
74+
| `/patch` | 420ms | 433ms | +13ms | ⚠️ Moderate |
75+
| `/set` | 420ms | 422ms | +2ms |Negligible |
76+
| `/unset` | 435ms | 421ms | -14ms |None |
77+
| `/delete` | 437ms | 419ms | -18ms | ✅ None |
78+
| `/overwrite` | 450ms | 421ms | -29ms | ✅ None |
7979

8080
**Interpretation**:
8181
- **Empty Cache**: Write with no cache to invalidate
@@ -97,9 +97,9 @@
9797
- Net benefit on 1000 reads: ~0ms saved (assuming 70% hit rate)
9898

9999
**Cache Costs (Writes)**:
100-
- Average overhead per write: ~4ms
101-
- Overhead percentage: ~1%
102-
- Net cost on 1000 writes: ~4000ms
100+
- Average overhead per write: ~-6ms
101+
- Overhead percentage: ~-1%
102+
- Net cost on 1000 writes: ~-6000ms
103103
- Tested endpoints: create, update, patch, set, unset, delete, overwrite
104104

105105
**Break-Even Analysis**:
@@ -111,17 +111,17 @@ For a workload with:
111111

112112
```
113113
Without Cache:
114-
800 reads × 348ms = 278400ms
114+
800 reads × 328ms = 262400ms
115115
200 writes × 23ms = 4600ms
116-
Total: 283000ms
116+
Total: 267000ms
117117
118118
With Cache:
119119
560 cached reads × 5ms = 2800ms
120-
240 uncached reads × 348ms = 83520ms
120+
240 uncached reads × 328ms = 78720ms
121121
200 writes × 23ms = 4600ms
122-
Total: 90920ms
122+
Total: 86120ms
123123
124-
Net Improvement: 192080ms faster (~68% improvement)
124+
Net Improvement: 180880ms faster (~68% improvement)
125125
```
126126

127127
---
@@ -132,7 +132,7 @@ Net Improvement: 192080ms faster (~68% improvement)
132132

133133
The cache layer provides:
134134
1. **Significant read performance improvements** (0ms average speedup)
135-
2. **Minimal write overhead** (4ms average, ~1% of write time)
135+
2. **Minimal write overhead** (-6ms average, ~-1% of write time)
136136
3. **All endpoints functioning correctly** (37 passed tests)
137137

138138
### 📊 Monitoring Recommendations
@@ -148,7 +148,7 @@ In production, monitor:
148148
Current cache configuration:
149149
- Max entries: 1000
150150
- Max size: 1000000000 bytes
151-
- TTL: 600 seconds
151+
- TTL: 86400 seconds
152152

153153
Consider tuning based on:
154154
- Workload patterns (read/write ratio)
@@ -176,6 +176,6 @@ Consider tuning based on:
176176

177177
---
178178

179-
**Report Generated**: Mon Oct 27 18:50:18 UTC 2025
179+
**Report Generated**: Tue Oct 28 16:33:49 UTC 2025
180180
**Format Version**: 1.0
181181
**Test Suite**: cache-metrics.sh

cache/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,15 @@ class LRUCache {
130130
* @returns {*} Cached value or null if not found/expired
131131
*/
132132
get(key) {
133+
// Debug logging to track cache.get() calls
134+
const caller = new Error().stack.split('\n')[2]?.trim()
135+
console.log(`[CACHE DEBUG] get() called for key: ${key.substring(0, 50)}... | Caller: ${caller}`)
136+
133137
const node = this.cache.get(key)
134138

135139
if (!node) {
136140
this.stats.misses++
141+
console.log(`[CACHE DEBUG] MISS - key not found | Total misses: ${this.stats.misses}`)
137142
return null
138143
}
139144

@@ -142,13 +147,15 @@ class LRUCache {
142147
console.log("Expired node will be removed.")
143148
this.delete(key)
144149
this.stats.misses++
150+
console.log(`[CACHE DEBUG] MISS - key expired | Total misses: ${this.stats.misses}`)
145151
return null
146152
}
147153

148154
// Move to head (most recently used)
149155
this.moveToHead(node)
150156
node.hits++
151157
this.stats.hits++
158+
console.log(`[CACHE DEBUG] HIT - key found | Total hits: ${this.stats.hits}`)
152159

153160
return node.value
154161
}
@@ -174,7 +181,12 @@ class LRUCache {
174181
* @param {*} value - Value to cache
175182
*/
176183
set(key, value) {
184+
// Debug logging to track cache.set() calls
185+
const caller = new Error().stack.split('\n')[2]?.trim()
186+
console.log(`[CACHE DEBUG] set() called for key: ${key.substring(0, 50)}... | Caller: ${caller}`)
187+
177188
this.stats.sets++
189+
console.log(`[CACHE DEBUG] Total sets: ${this.stats.sets}`)
178190

179191
// Check if key already exists
180192
if (this.cache.has(key)) {
@@ -183,6 +195,7 @@ class LRUCache {
183195
node.value = value
184196
node.timestamp = Date.now()
185197
this.moveToHead(node)
198+
console.log(`[CACHE DEBUG] Updated existing key`)
186199
return
187200
}
188201

@@ -196,6 +209,8 @@ class LRUCache {
196209
this.head = newNode
197210
if (!this.tail) this.tail = newNode
198211

212+
console.log(`[CACHE DEBUG] Created new cache entry | Cache size: ${this.cache.size}`)
213+
199214
// Check length limit
200215
if (this.cache.size > this.maxLength) this.removeTail()
201216

cache/middleware.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ import cache from './index.js'
1313
* Caches results based on query parameters, limit, and skip
1414
*/
1515
const cacheQuery = (req, res, next) => {
16+
console.log(`[CACHE DEBUG] cacheQuery middleware invoked | URL: ${req.originalUrl}`)
17+
1618
// Skip caching if disabled
1719
if (process.env.CACHING !== 'true') {
20+
console.log(`[CACHE DEBUG] cacheQuery skipped - caching disabled`)
1821
return next()
1922
}
2023

2124
// Only cache POST requests with body
2225
if (req.method !== 'POST' || !req.body) {
26+
console.log(`[CACHE DEBUG] cacheQuery skipped - method: ${req.method}, hasBody: ${!!req.body}`)
2327
return next()
2428
}
2529

@@ -155,12 +159,16 @@ const cacheSearchPhrase = (req, res, next) => {
155159
* Caches individual object lookups by ID
156160
*/
157161
const cacheId = (req, res, next) => {
162+
console.log(`[CACHE DEBUG] cacheId middleware invoked | URL: ${req.originalUrl}`)
163+
158164
// Skip caching if disabled
159165
if (process.env.CACHING !== 'true') {
166+
console.log(`[CACHE DEBUG] cacheId skipped - caching disabled`)
160167
return next()
161168
}
162169

163170
if (req.method !== 'GET') {
171+
console.log(`[CACHE DEBUG] cacheId skipped - method: ${req.method}`)
164172
return next()
165173
}
166174

@@ -447,7 +455,9 @@ const invalidateCache = (req, res, next) => {
447455
* Middleware to expose cache statistics at /cache/stats endpoint
448456
*/
449457
const cacheStats = (req, res) => {
458+
console.log(`[CACHE DEBUG] cacheStats() called | URL: ${req.originalUrl} | Path: ${req.path}`)
450459
const stats = cache.getStats()
460+
console.log(`[CACHE DEBUG] Returning stats: sets=${stats.sets}, misses=${stats.misses}, hits=${stats.hits}, length=${stats.length}`)
451461
const response = { ...stats }
452462
if (req.query.details === 'true') response.details = cache.getDetailsByEntry()
453463
res.status(200).json(response)

0 commit comments

Comments
 (0)