Skip to content

Commit 296a3cd

Browse files
Added lock
1 parent 0fc3957 commit 296a3cd

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/shared/Z.EF.Plus.QueryCache.Shared/QueryCacheManager.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,13 @@ internal static void AddCacheTag(string cacheKey, params string[] tags)
230230
{
231231
CacheTags.AddOrUpdate(CachePrefix + tag, x => new List<string> {cacheKey}, (x, list) =>
232232
{
233-
if (!list.Contains(cacheKey))
233+
lock (list)
234234
{
235-
list.Add(cacheKey);
235+
// never lock something related to this list elsewhere or ensure we don't create a deadlock
236+
if (!list.Contains(cacheKey))
237+
{
238+
list.Add(cacheKey);
239+
}
236240
}
237241

238242
return list;
@@ -272,10 +276,14 @@ public static void ExpireTag(params string[] tags)
272276
{
273277
List<string> list;
274278
if (CacheTags.TryRemove(CachePrefix + tag, out list))
275-
{
276-
foreach (var item in list)
279+
{
280+
// never lock something related to this list elsewhere or ensure we don't create a deadlock
281+
lock (list)
277282
{
278-
Cache.Remove(item);
283+
foreach (var item in list)
284+
{
285+
Cache.Remove(item);
286+
}
279287
}
280288
}
281289
}

0 commit comments

Comments
 (0)