Fix errorre rmoving entries from the cache (#1167)

This commit is contained in:
Mbucari 2025-03-02 10:49:09 -07:00
parent efbefa2784
commit c9c5bbb687

View File

@ -55,8 +55,9 @@ namespace LibationFileManager
{ {
if (!File.Exists(matchingFiles[i].Path)) if (!File.Exists(matchingFiles[i].Path))
{ {
var entryToRemove = matchingFiles[i];
matchingFiles.RemoveAt(i); matchingFiles.RemoveAt(i);
cacheChanged |= Remove(matchingFiles[i]); cacheChanged |= Remove(entryToRemove);
} }
} }
if (cacheChanged) if (cacheChanged)
@ -79,8 +80,9 @@ namespace LibationFileManager
return matchingFiles[i].Path; return matchingFiles[i].Path;
else else
{ {
var entryToRemove = matchingFiles[i];
matchingFiles.RemoveAt(i); matchingFiles.RemoveAt(i);
cacheChanged |= Remove(matchingFiles[i]); cacheChanged |= Remove(entryToRemove);
} }
} }
return null; return null;
@ -141,6 +143,7 @@ namespace LibationFileManager
{ {
[JsonProperty] [JsonProperty]
private readonly ConcurrentDictionary<string, List<TEntry>> Dictionary = new(); private readonly ConcurrentDictionary<string, List<TEntry>> Dictionary = new();
private static object lockObject = new();
public List<TEntry> GetIdEntries(string id) public List<TEntry> GetIdEntries(string id)
{ {
@ -164,7 +167,21 @@ namespace LibationFileManager
} }
public bool Remove(string id, TEntry entry) public bool Remove(string id, TEntry entry)
=> Dictionary.TryGetValue(id, out List<TEntry>? entries) && entries.Remove(entry); {
lock (lockObject)
{
if (Dictionary.TryGetValue(id, out List<TEntry>? entries))
{
var removed = entries?.Remove(entry) ?? false;
if (removed && entries?.Count == 0)
{
Dictionary.Remove(id, out _);
}
return removed;
}
else return false;
}
}
} }
} }
} }