Skip to content

Commit 6764f94

Browse files
committed
Less CPU pressure in scheduled cleanup when many FastCache instances
(by using global static lock)
1 parent 3fa25b2 commit 6764f94

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

FastCache/FastCache.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,27 @@ public class FastCache<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>,
2121
/// <param name="cleanupJobInterval">cleanup interval in milliseconds, default is 10000</param>
2222
public FastCache(int cleanupJobInterval = 10000)
2323
{
24-
_cleanUpTimer = new Timer(s => EvictExpired(), null, cleanupJobInterval, cleanupJobInterval);
24+
_cleanUpTimer = new Timer(s => EvictExpiredJob(), null, cleanupJobInterval, cleanupJobInterval);
25+
}
26+
27+
private static object _globalStaticLock = new object();
28+
private void EvictExpiredJob()
29+
{
30+
//if an applicaiton has many-many instances of FastCache objects, make sure the timer-based
31+
//cleanup jobs don't clash with each other, i.e. there are no clean-up jobs running in parallel
32+
//so we don't waste CPU resources, because cleanup is a busy-loop that iterates a collection and does calculations
33+
//so we use a lock to "throttle" the job and make it serial
34+
//HOWEVER, we still allow the user to execute eviction explicitly
35+
36+
lock (_globalStaticLock)
37+
{
38+
EvictExpired();
39+
}
2540
}
2641

2742
/// <summary>
2843
/// Cleans up expired items (dont' wait for the background job)
44+
/// There's rarely a need to execute this method, b/c getting an item checks TTL anyway.
2945
/// </summary>
3046
public void EvictExpired()
3147
{

0 commit comments

Comments
 (0)