@@ -96,12 +96,9 @@ private void SetEntry(CacheEntry entry)
96
96
return ;
97
97
}
98
98
99
- if ( _options . SizeLimit != null )
99
+ if ( _options . SizeLimit . HasValue && ! entry . Size . HasValue )
100
100
{
101
- if ( ! entry . Size . HasValue )
102
- {
103
- throw new InvalidOperationException ( $ "Cache entry must specify a value for { nameof ( entry . Size ) } when { nameof ( _options . SizeLimit ) } is set.") ;
104
- }
101
+ throw new InvalidOperationException ( $ "Cache entry must specify a value for { nameof ( entry . Size ) } when { nameof ( _options . SizeLimit ) } is set.") ;
105
102
}
106
103
107
104
var utcNow = _options . Clock . UtcNow ;
@@ -175,7 +172,7 @@ private void SetEntry(CacheEntry entry)
175
172
}
176
173
else
177
174
{
178
- if ( _options . SizeLimit != null )
175
+ if ( _options . SizeLimit . HasValue )
179
176
{
180
177
// Entry could not be added, reset cache size
181
178
Interlocked . Add ( ref _cacheSize , - sizeUpdate ) ;
@@ -191,7 +188,7 @@ private void SetEntry(CacheEntry entry)
191
188
}
192
189
else
193
190
{
194
- if ( _options . SizeLimit != null && updatedCacheSize > _options . SizeLimit )
191
+ if ( _options . SizeLimit . HasValue && updatedCacheSize > _options . SizeLimit )
195
192
{
196
193
// The entry was not added due to overcapacity
197
194
entry . SetExpired ( EvictionReason . Capacity ) ;
@@ -265,7 +262,7 @@ public void Remove(object key)
265
262
CacheEntry entry ;
266
263
if ( _entries . TryRemove ( key , out entry ) )
267
264
{
268
- if ( _options . SizeLimit != null )
265
+ if ( _options . SizeLimit . HasValue )
269
266
{
270
267
Interlocked . Add ( ref _cacheSize , - entry . Size . Value ) ;
271
268
}
@@ -281,7 +278,7 @@ private void RemoveEntry(CacheEntry entry)
281
278
{
282
279
if ( EntriesCollection . Remove ( new KeyValuePair < object , CacheEntry > ( entry . Key , entry ) ) )
283
280
{
284
- if ( _options . SizeLimit != null )
281
+ if ( _options . SizeLimit . HasValue )
285
282
{
286
283
Interlocked . Add ( ref _cacheSize , - entry . Size . Value ) ;
287
284
}
@@ -323,7 +320,7 @@ private static void ScanForExpiredItems(MemoryCache cache)
323
320
324
321
private bool UpdateCacheSizeExceedsCapacity ( CacheEntry entry , CacheEntry priorEntry , out long sizeUpdate , out long updatedCacheSize )
325
322
{
326
- if ( _options . SizeLimit == null )
323
+ if ( ! _options . SizeLimit . HasValue )
327
324
{
328
325
sizeUpdate = 0 ;
329
326
updatedCacheSize = 0 ;
@@ -342,9 +339,10 @@ private void TriggerOvercapacityCompaction()
342
339
ThreadPool . QueueUserWorkItem ( _ =>
343
340
{
344
341
var currentSize = Interlocked . Read ( ref _cacheSize ) ;
345
- if ( currentSize > _options . SizeLimit * ( 1 - _options . RemovalPercentageOnOvercapacityCompaction ) )
342
+ var lowWatermark = _options . SizeLimit * ( 1 - _options . RemovalPercentageOnOvercapacityCompaction ) ;
343
+ if ( currentSize > lowWatermark )
346
344
{
347
- Compact ( currentSize - ( long ) ( _options . SizeLimit * ( 1 - _options . RemovalPercentageOnOvercapacityCompaction ) ) ) ;
345
+ Compact ( currentSize - ( long ) lowWatermark , entry => entry . Size . Value ) ;
348
346
}
349
347
} ) ;
350
348
}
@@ -362,11 +360,6 @@ public void Compact(double percentage)
362
360
Compact ( removalCountTarget , _ => 1 ) ;
363
361
}
364
362
365
- private void Compact ( long removalSizeTarget )
366
- {
367
- Compact ( removalSizeTarget , entry => entry . Size . Value ) ;
368
- }
369
-
370
363
private void Compact ( long removalSizeTarget , Func < CacheEntry , long > computeEntrySize )
371
364
{
372
365
var entriesToRemove = new List < CacheEntry > ( ) ;
0 commit comments