From 6dc3a5fa41643366f24078a4b254c68890aaf13c Mon Sep 17 00:00:00 2001
From: ConfusedPolarBear <33811686+ConfusedPolarBear@users.noreply.github.com>
Date: Thu, 1 Sep 2022 23:24:00 -0500
Subject: [PATCH] Lock inverted index cache
---
.../FFmpegWrapper.cs | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/FFmpegWrapper.cs b/ConfusedPolarBear.Plugin.IntroSkipper/FFmpegWrapper.cs
index 0ca19d0..4f38984 100644
--- a/ConfusedPolarBear.Plugin.IntroSkipper/FFmpegWrapper.cs
+++ b/ConfusedPolarBear.Plugin.IntroSkipper/FFmpegWrapper.cs
@@ -14,6 +14,8 @@ namespace ConfusedPolarBear.Plugin.IntroSkipper;
///
public static class FFmpegWrapper
{
+ private static readonly object InvertedIndexCacheLock = new();
+
// FFmpeg logs lines similar to the following:
// [silencedetect @ 0x000000000000] silence_start: 12.34
// [silencedetect @ 0x000000000000] silence_end: 56.123 | silence_duration: 43.783
@@ -183,9 +185,12 @@ public static class FFmpegWrapper
/// Inverted index.
public static Dictionary CreateInvertedIndex(Guid id, uint[] fingerprint)
{
- if (InvertedIndexCache.TryGetValue(id, out var cached))
+ lock (InvertedIndexCacheLock)
{
- return cached;
+ if (InvertedIndexCache.TryGetValue(id, out var cached))
+ {
+ return cached;
+ }
}
var invIndex = new Dictionary();
@@ -199,7 +204,10 @@ public static class FFmpegWrapper
invIndex[point] = i;
}
- InvertedIndexCache[id] = invIndex;
+ lock (InvertedIndexCacheLock)
+ {
+ InvertedIndexCache[id] = invIndex;
+ }
return invIndex;
}