Commit 4f965f2
refactor(cache): optimize eviction with in-memory size tracking (Issue #5)
TDD Cycle: RED-GREEN-REFACTOR for cache size tracking optimization
RED Phase:
- Added 3 failing tests for cache size tracking accuracy
- Tests verify size tracking across add, replace, clear operations
- Tests ensure eviction check doesn't query database unnecessarily
GREEN Phase:
- Added currentSize field to track cache size in memory
- Modified initialize() to load initial size from database
- Modified set() to update currentSize on insert/replace
- Modified clear() to reset currentSize to 0
- Modified cleanupExpired() to update currentSize after deletion
- Modified evictIfNeeded() to use currentSize for O(1) check instead of stats()
REFACTOR Phase:
- Added comprehensive comments explaining the optimization
- Documented Issue #5 fix throughout the code
- All 1449 tests passing
Performance Impact:
- Before: evictIfNeeded() called stats() (COUNT(*) + SUM(size)) on every cache write
- After: O(1) in-memory check, only queries database when eviction needed
- Eliminates expensive database aggregation on fast path
CHANGELOG.md updated with detailed optimization notes.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>1 parent 500d5cc commit 4f965f2
3 files changed
Lines changed: 128 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
43 | 55 | | |
44 | 56 | | |
45 | 57 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| 42 | + | |
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
| |||
103 | 104 | | |
104 | 105 | | |
105 | 106 | | |
| 107 | + | |
106 | 108 | | |
107 | 109 | | |
108 | 110 | | |
| |||
114 | 116 | | |
115 | 117 | | |
116 | 118 | | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
117 | 123 | | |
118 | 124 | | |
119 | 125 | | |
| |||
161 | 167 | | |
162 | 168 | | |
163 | 169 | | |
| 170 | + | |
164 | 171 | | |
165 | 172 | | |
166 | 173 | | |
| |||
196 | 203 | | |
197 | 204 | | |
198 | 205 | | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
199 | 210 | | |
200 | 211 | | |
201 | 212 | | |
202 | 213 | | |
| 214 | + | |
203 | 215 | | |
204 | 216 | | |
205 | 217 | | |
| 218 | + | |
206 | 219 | | |
207 | 220 | | |
208 | 221 | | |
| |||
259 | 272 | | |
260 | 273 | | |
261 | 274 | | |
| 275 | + | |
262 | 276 | | |
263 | 277 | | |
264 | 278 | | |
265 | 279 | | |
266 | 280 | | |
267 | 281 | | |
268 | 282 | | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
269 | 290 | | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
270 | 294 | | |
271 | 295 | | |
272 | 296 | | |
273 | 297 | | |
| 298 | + | |
274 | 299 | | |
275 | 300 | | |
276 | 301 | | |
277 | | - | |
278 | | - | |
279 | | - | |
| 302 | + | |
| 303 | + | |
280 | 304 | | |
281 | 305 | | |
282 | 306 | | |
283 | 307 | | |
284 | 308 | | |
285 | | - | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
286 | 315 | | |
287 | 316 | | |
288 | 317 | | |
289 | | - | |
| 318 | + | |
290 | 319 | | |
291 | 320 | | |
292 | 321 | | |
293 | 322 | | |
294 | 323 | | |
295 | 324 | | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
296 | 338 | | |
297 | 339 | | |
298 | 340 | | |
| |||
303 | 345 | | |
304 | 346 | | |
305 | 347 | | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
306 | 351 | | |
307 | 352 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
547 | 547 | | |
548 | 548 | | |
549 | 549 | | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
550 | 616 | | |
0 commit comments