Fixed LRUCache TryGetValue not updating the list

This commit is contained in:
2026-01-03 18:22:30 +01:00
parent 027a9244ad
commit 88d1b27394

View File

@@ -154,13 +154,26 @@ public class EnumerableLruCache<TKey, TValue> where TKey : notnull
public bool TryGetValue(TKey key, out TValue? value) public bool TryGetValue(TKey key, out TValue? value)
{ {
_readerWriterLock.EnterReadLock(); _readerWriterLock.EnterUpgradeableReadLock();
try try
{ {
return _cache.TryGetValue(key, out value); if (_cache.TryGetValue(key, out value))
{
return false;
}
_readerWriterLock.EnterWriteLock();
try
{
_keys.Remove(key);
_keys.AddFirst(key);
} finally
{
_readerWriterLock.ExitWriteLock();
}
return true;
} finally } finally
{ {
_readerWriterLock.ExitReadLock(); _readerWriterLock.ExitUpgradeableReadLock();
} }
} }