Skip to content

Commit 566bd3c

Browse files
committed
Cache fix
1 parent 4384b90 commit 566bd3c

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

StringExtensions/CacheExtensions.cs

+19-5
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,26 @@ public static Task<T> AtomicGetOrCreateAsync<T>(
1616
string key,
1717
Func<ICacheEntry, Task<T>> factory)
1818
{
19-
lock (lockObject)
19+
return cache.GetOrCreate<Task<T>>(key, (entry) =>
2020
{
21-
return cache.GetOrCreate<Task<T>>(key, (entry) => {
22-
return factory(entry);
23-
});
24-
}
21+
lock (lockObject)
22+
{
23+
Func<ICacheEntry, Task<T>> fx = async (e2) => {
24+
try {
25+
return await factory(e2);
26+
} catch
27+
{
28+
// it is stale...
29+
cache.Remove(key);
30+
throw;
31+
}
32+
};
33+
return cache.GetOrCreate<Task<T>>(key, (e1) =>
34+
{
35+
return fx(e1);
36+
});
37+
}
38+
});
2539
}
2640
}
2741
}

0 commit comments

Comments
 (0)