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
This commit is contained in:
robojerk 2025-08-16 15:34:02 -07:00
parent 306a68b89a
commit c098a6b21e

View file

@ -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]