File tree Expand file tree Collapse file tree 1 file changed +8
-5
lines changed
src/Caching/SqlServer/src Expand file tree Collapse file tree 1 file changed +8
-5
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ public class SqlServerCache : IDistributedCache
25
25
private DateTimeOffset _lastExpirationScan ;
26
26
private readonly Action _deleteExpiredCachedItemsDelegate ;
27
27
private readonly TimeSpan _defaultSlidingExpiration ;
28
+ private readonly Object _mutex = new Object ( ) ;
28
29
29
30
public SqlServerCache ( IOptions < SqlServerCacheOptions > options )
30
31
{
@@ -227,12 +228,14 @@ public async Task SetAsync(
227
228
// If sufficient time has elapsed then a scan is initiated on a background task.
228
229
private void ScanForExpiredItemsIfRequired ( )
229
230
{
230
- var utcNow = _systemClock . UtcNow ;
231
- // TODO: Multiple threads could trigger this scan which leads to multiple calls to database.
232
- if ( ( utcNow - _lastExpirationScan ) > _expiredItemsDeletionInterval )
231
+ lock ( _mutex )
233
232
{
234
- _lastExpirationScan = utcNow ;
235
- Task . Run ( _deleteExpiredCachedItemsDelegate ) ;
233
+ var utcNow = _systemClock . UtcNow ;
234
+ if ( ( utcNow - _lastExpirationScan ) > _expiredItemsDeletionInterval )
235
+ {
236
+ _lastExpirationScan = utcNow ;
237
+ Task . Run ( _deleteExpiredCachedItemsDelegate ) ;
238
+ }
236
239
}
237
240
}
238
241
You can’t perform that action at this time.
0 commit comments