Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit b411284

Browse files
committed
quick feedback items
1 parent 6e93664 commit b411284

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

Diff for: src/Microsoft.Extensions.Caching.Memory/MemoryCache.cs

+10-17
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,9 @@ private void SetEntry(CacheEntry entry)
9696
return;
9797
}
9898

99-
if (_options.SizeLimit != null)
99+
if (_options.SizeLimit.HasValue && !entry.Size.HasValue)
100100
{
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.");
105102
}
106103

107104
var utcNow = _options.Clock.UtcNow;
@@ -175,7 +172,7 @@ private void SetEntry(CacheEntry entry)
175172
}
176173
else
177174
{
178-
if (_options.SizeLimit != null)
175+
if (_options.SizeLimit.HasValue)
179176
{
180177
// Entry could not be added, reset cache size
181178
Interlocked.Add(ref _cacheSize, -sizeUpdate);
@@ -191,7 +188,7 @@ private void SetEntry(CacheEntry entry)
191188
}
192189
else
193190
{
194-
if (_options.SizeLimit != null && updatedCacheSize > _options.SizeLimit)
191+
if (_options.SizeLimit.HasValue && updatedCacheSize > _options.SizeLimit)
195192
{
196193
// The entry was not added due to overcapacity
197194
entry.SetExpired(EvictionReason.Capacity);
@@ -265,7 +262,7 @@ public void Remove(object key)
265262
CacheEntry entry;
266263
if (_entries.TryRemove(key, out entry))
267264
{
268-
if (_options.SizeLimit != null)
265+
if (_options.SizeLimit.HasValue)
269266
{
270267
Interlocked.Add(ref _cacheSize, -entry.Size.Value);
271268
}
@@ -281,7 +278,7 @@ private void RemoveEntry(CacheEntry entry)
281278
{
282279
if (EntriesCollection.Remove(new KeyValuePair<object, CacheEntry>(entry.Key, entry)))
283280
{
284-
if (_options.SizeLimit != null)
281+
if (_options.SizeLimit.HasValue)
285282
{
286283
Interlocked.Add(ref _cacheSize, -entry.Size.Value);
287284
}
@@ -323,7 +320,7 @@ private static void ScanForExpiredItems(MemoryCache cache)
323320

324321
private bool UpdateCacheSizeExceedsCapacity(CacheEntry entry, CacheEntry priorEntry, out long sizeUpdate, out long updatedCacheSize)
325322
{
326-
if (_options.SizeLimit == null)
323+
if (!_options.SizeLimit.HasValue)
327324
{
328325
sizeUpdate = 0;
329326
updatedCacheSize = 0;
@@ -342,9 +339,10 @@ private void TriggerOvercapacityCompaction()
342339
ThreadPool.QueueUserWorkItem(_ =>
343340
{
344341
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)
346344
{
347-
Compact(currentSize - (long)(_options.SizeLimit * (1 - _options.RemovalPercentageOnOvercapacityCompaction)));
345+
Compact(currentSize - (long)lowWatermark, entry => entry.Size.Value);
348346
}
349347
});
350348
}
@@ -362,11 +360,6 @@ public void Compact(double percentage)
362360
Compact(removalCountTarget, _ => 1);
363361
}
364362

365-
private void Compact(long removalSizeTarget)
366-
{
367-
Compact(removalSizeTarget, entry => entry.Size.Value);
368-
}
369-
370363
private void Compact(long removalSizeTarget, Func<CacheEntry, long> computeEntrySize)
371364
{
372365
var entriesToRemove = new List<CacheEntry>();

0 commit comments

Comments
 (0)