This repository was archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Use private instance of MemoryCache and impose size limit #132
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
src/Microsoft.AspNetCore.ResponseCaching/Internal/CacheEntry/CacheEntryHelpers .cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace Microsoft.AspNetCore.ResponseCaching.Internal | ||
{ | ||
internal static class CacheEntryHelpers | ||
{ | ||
|
||
internal static long EstimateCachedResponseSize(CachedResponse cachedResponse) | ||
{ | ||
if (cachedResponse == null) | ||
{ | ||
return 0L; | ||
} | ||
|
||
checked | ||
{ | ||
// StatusCode | ||
long size = sizeof(int); | ||
|
||
// Headers | ||
if (cachedResponse.Headers != null) | ||
{ | ||
foreach (var item in cachedResponse.Headers) | ||
{ | ||
size += item.Key.Length * sizeof(char) + EstimateStringValuesSize(item.Value); | ||
} | ||
} | ||
|
||
// Body | ||
if (cachedResponse.Body != null) | ||
{ | ||
size += cachedResponse.Body.Length; | ||
} | ||
|
||
return size; | ||
} | ||
} | ||
|
||
internal static long EstimateCachedVaryByRulesySize(CachedVaryByRules cachedVaryByRules) | ||
{ | ||
if (cachedVaryByRules == null) | ||
{ | ||
return 0L; | ||
} | ||
|
||
checked | ||
{ | ||
var size = 0L; | ||
|
||
// VaryByKeyPrefix | ||
if (!string.IsNullOrEmpty(cachedVaryByRules.VaryByKeyPrefix)) | ||
{ | ||
size = cachedVaryByRules.VaryByKeyPrefix.Length * sizeof(char); | ||
} | ||
|
||
// Headers | ||
size += EstimateStringValuesSize(cachedVaryByRules.Headers); | ||
|
||
// QueryKeys | ||
size += EstimateStringValuesSize(cachedVaryByRules.QueryKeys); | ||
|
||
return size; | ||
} | ||
} | ||
|
||
internal static long EstimateStringValuesSize(StringValues stringValues) | ||
{ | ||
checked | ||
{ | ||
var size = 0L; | ||
|
||
for (var i = 0; i < stringValues.Count; i++) | ||
{ | ||
var stringValue = stringValues[i]; | ||
if (!string.IsNullOrEmpty(stringValue)) | ||
{ | ||
size += stringValues[i].Length * sizeof(char); | ||
} | ||
} | ||
|
||
return size; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,11 @@ namespace Microsoft.AspNetCore.ResponseCaching | |
{ | ||
public class ResponseCachingOptions | ||
{ | ||
/// <summary> | ||
/// The size limit for the response cache middleware in bytes. The default is set to 100 MB. | ||
/// </summary> | ||
public long SizeLimit { get; set; } = 100 * 1024 * 1024; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we have ResponseCachingOptions extend MemoryCacheOptions so other options can be set? |
||
|
||
/// <summary> | ||
/// The largest cacheable size for the response body in bytes. The default is set to 64 MB. | ||
/// </summary> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, we can add the MemoryResponseCache to DI but have it resolve ResponseCachingOptions to figure out the size.