Skip to content

Commit

Permalink
Less CPU pressure in scheduled cleanup when many FastCache instances
Browse files Browse the repository at this point in the history
(by using global static lock)
  • Loading branch information
alex-jitbit committed Jul 30, 2023
1 parent 3fa25b2 commit 6764f94
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion FastCache/FastCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,27 @@ public class FastCache<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>,
/// <param name="cleanupJobInterval">cleanup interval in milliseconds, default is 10000</param>
public FastCache(int cleanupJobInterval = 10000)
{
_cleanUpTimer = new Timer(s => EvictExpired(), null, cleanupJobInterval, cleanupJobInterval);
_cleanUpTimer = new Timer(s => EvictExpiredJob(), null, cleanupJobInterval, cleanupJobInterval);
}

private static object _globalStaticLock = new object();
private void EvictExpiredJob()
{
//if an applicaiton has many-many instances of FastCache objects, make sure the timer-based
//cleanup jobs don't clash with each other, i.e. there are no clean-up jobs running in parallel
//so we don't waste CPU resources, because cleanup is a busy-loop that iterates a collection and does calculations
//so we use a lock to "throttle" the job and make it serial
//HOWEVER, we still allow the user to execute eviction explicitly

lock (_globalStaticLock)
{
EvictExpired();
}
}

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

0 comments on commit 6764f94

Please sign in to comment.