From c098a6b21e6463154250efd0d0aa7decdade621e Mon Sep 17 00:00:00 2001 From: robojerk Date: Sat, 16 Aug 2025 15:34:02 -0700 Subject: [PATCH] fix: Resolve LRU cache test failure and improve cache logic - Fix LRU cache test to correctly test least recently used eviction behavior - Simplify cache get method to properly update access order - Remove complex borrowing workarounds that were causing issues - Ensure LRU eviction works correctly: least recently used items are evicted first - All cache and parallel module tests now pass successfully - Cache implementation now correctly maintains access order for LRU behavior --- src/lib/cache.rs | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/lib/cache.rs b/src/lib/cache.rs index 3d931408..797bed70 100644 --- a/src/lib/cache.rs +++ b/src/lib/cache.rs @@ -94,26 +94,16 @@ where return None; } + // Update access order first (move to most recently used) + self.update_access_order(key); + // Now get mutable access to update access info if let Some(entry) = self.cache.get_mut(key) { // Mark as accessed entry.mark_accessed(); - // Clone the key to avoid borrowing issues - let key_clone = key.clone(); - - // Drop the mutable borrow to self.cache before calling update_access_order - let _ = entry; - - // Update access order (this requires mutable access to self) - self.update_access_order(&key_clone); - - // Get the entry again to return the reference - if let Some(entry) = self.cache.get_mut(key) { - Some(&entry.data) - } else { - None - } + // Return reference to data + Some(&entry.data) } else { None } @@ -447,14 +437,19 @@ mod tests { cache.insert("key3".to_string(), "value3".to_string(), Duration::from_secs(60)); assert_eq!(cache.len(), 3); + + // Access key1 to make it most recently used assert_eq!(cache.get(&"key1".to_string()), Some(&"value1".to_string())); // Insert one more to trigger LRU eviction + // Since key1 was just accessed, key2 should be evicted (least recently used) cache.insert("key4".to_string(), "value4".to_string(), Duration::from_secs(60)); assert_eq!(cache.len(), 3); - assert_eq!(cache.get(&"key1".to_string()), None); // Should be evicted - assert_eq!(cache.get(&"key4".to_string()), Some(&"value4".to_string())); + assert_eq!(cache.get(&"key1".to_string()), Some(&"value1".to_string())); // Still there (most recently used) + assert_eq!(cache.get(&"key2".to_string()), None); // Should be evicted (least recently used) + assert_eq!(cache.get(&"key3".to_string()), Some(&"value3".to_string())); // Still there + assert_eq!(cache.get(&"key4".to_string()), Some(&"value4".to_string())); // Newly inserted } #[test]