Address code review feedback: deterministic cache cleanup and migration approach

Co-authored-by: alexpolo1 <14327609+alexpolo1@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-12-27 08:39:49 +00:00
parent fb78cdcdcc
commit 3329ddef7f
3 changed files with 12 additions and 9 deletions

View File

@@ -509,9 +509,7 @@ class DatabaseService {
parse_log TEXT,
is_active BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (material_id) REFERENCES materials(id),
INDEX idx_material_active (material_id, is_active),
INDEX idx_valid_from (valid_from)
FOREIGN KEY (material_id) REFERENCES materials(id)
)
`);
@@ -543,9 +541,7 @@ class DatabaseService {
parse_log TEXT,
is_active BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (labor_task_id) REFERENCES labor_tasks(id),
INDEX idx_labor_task_active (labor_task_id, is_active),
INDEX idx_valid_from (valid_from)
FOREIGN KEY (labor_task_id) REFERENCES labor_tasks(id)
)
`);

View File

@@ -15,10 +15,13 @@ class PackageService {
// PERFORMANCE: In-memory cache for material lookups (5 minute TTL)
this.materialCache = new Map();
this.CACHE_TTL = 5 * 60 * 1000; // 5 minutes
this.cacheAccessCount = 0;
this.CLEANUP_INTERVAL = 100; // Cleanup every 100 cache accesses
}
/**
* PERFORMANCE: Clear expired cache entries
* PERFORMANCE: Clear expired cache entries deterministically
* Called every CLEANUP_INTERVAL accesses to ensure consistent memory management
*/
_cleanupCache() {
const now = Date.now();
@@ -48,9 +51,11 @@ class PackageService {
return cached.value;
}
// Cleanup expired entries periodically (every 100 requests)
if (Math.random() < 0.01) {
// PERFORMANCE: Deterministic cache cleanup every N accesses
this.cacheAccessCount++;
if (this.cacheAccessCount >= this.CLEANUP_INTERVAL) {
this._cleanupCache();
this.cacheAccessCount = 0;
}
// First try Bygma products (most comprehensive)

View File

@@ -109,6 +109,8 @@ CREATE INDEX idx_bygma_active_varegrp ON bygma_products(is_active, varegrp);
CREATE INDEX idx_ocr_materials_search ON ocr_materials(name, sku, material_category);
```
**Important:** Indexes must be applied via the migration script (see Migration Guide below).
**Impact:**
- Material search queries: ~100ms ~5ms (20x improvement)
- Price lookups: ~50ms ~2ms (25x improvement)