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

Commit e70356a

Browse files
committed
Refactor
1 parent 7372712 commit e70356a

File tree

6 files changed

+107
-104
lines changed

6 files changed

+107
-104
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.Extensions.Primitives;
5+
6+
namespace Microsoft.AspNetCore.ResponseCaching.Internal
7+
{
8+
internal static class CacheEntryHelpers
9+
{
10+
11+
internal static long EstimateCachedResponseSize(CachedResponse cachedResponse)
12+
{
13+
if (cachedResponse == null)
14+
{
15+
return 0L;
16+
}
17+
18+
checked
19+
{
20+
// StatusCode
21+
long size = sizeof(int);
22+
23+
// Headers
24+
if (cachedResponse.Headers != null)
25+
{
26+
foreach (var item in cachedResponse.Headers)
27+
{
28+
size += item.Key.Length * sizeof(char) + EstimateStringValuesSize(item.Value);
29+
}
30+
}
31+
32+
// Body
33+
if (cachedResponse.Body != null)
34+
{
35+
size += cachedResponse.Body.Length;
36+
}
37+
38+
return size;
39+
}
40+
}
41+
42+
internal static long EstimateCachedVaryByRulesySize(CachedVaryByRules cachedVaryByRules)
43+
{
44+
if (cachedVaryByRules == null)
45+
{
46+
return 0L;
47+
}
48+
49+
checked
50+
{
51+
var size = 0L;
52+
53+
// VaryByKeyPrefix
54+
if (!string.IsNullOrEmpty(cachedVaryByRules.VaryByKeyPrefix))
55+
{
56+
size = cachedVaryByRules.VaryByKeyPrefix.Length * sizeof(char);
57+
}
58+
59+
// Headers
60+
size += EstimateStringValuesSize(cachedVaryByRules.Headers);
61+
62+
// QueryKeys
63+
size += EstimateStringValuesSize(cachedVaryByRules.QueryKeys);
64+
65+
return size;
66+
}
67+
}
68+
69+
internal static long EstimateStringValuesSize(StringValues stringValues)
70+
{
71+
checked
72+
{
73+
var size = 0L;
74+
75+
for (var i = 0; i < stringValues.Count; i++)
76+
{
77+
var stringValue = stringValues[i];
78+
if (!string.IsNullOrEmpty(stringValue))
79+
{
80+
size += stringValues[i].Length * sizeof(char);
81+
}
82+
}
83+
84+
return size;
85+
}
86+
}
87+
}
88+
}

src/Microsoft.AspNetCore.ResponseCaching/Internal/Interfaces/IResponseCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface IResponseCache
1111
IResponseCacheEntry Get(string key);
1212
Task<IResponseCacheEntry> GetAsync(string key);
1313

14-
void Set(string key, IResponseCacheEntry entry, TimeSpan validFor, long size);
15-
Task SetAsync(string key, IResponseCacheEntry entry, TimeSpan validFor, long size);
14+
void Set(string key, IResponseCacheEntry entry, TimeSpan validFor);
15+
Task SetAsync(string key, IResponseCacheEntry entry, TimeSpan validFor);
1616
}
1717
}

src/Microsoft.AspNetCore.ResponseCaching/Internal/MemoryResponseCache.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public Task<IResponseCacheEntry> GetAsync(string key)
4747
return Task.FromResult(Get(key));
4848
}
4949

50-
public void Set(string key, IResponseCacheEntry entry, TimeSpan validFor, long size)
50+
public void Set(string key, IResponseCacheEntry entry, TimeSpan validFor)
5151
{
5252
var cachedResponse = entry as CachedResponse;
5353
if (cachedResponse != null)
@@ -68,7 +68,7 @@ public void Set(string key, IResponseCacheEntry entry, TimeSpan validFor, long s
6868
new MemoryCacheEntryOptions
6969
{
7070
AbsoluteExpirationRelativeToNow = validFor,
71-
Size = size
71+
Size = CacheEntryHelpers.EstimateCachedResponseSize(cachedResponse)
7272
});
7373
}
7474
else
@@ -79,14 +79,14 @@ public void Set(string key, IResponseCacheEntry entry, TimeSpan validFor, long s
7979
new MemoryCacheEntryOptions
8080
{
8181
AbsoluteExpirationRelativeToNow = validFor,
82-
Size = size
82+
Size = CacheEntryHelpers.EstimateCachedVaryByRulesySize(entry as CachedVaryByRules)
8383
});
8484
}
8585
}
8686

87-
public Task SetAsync(string key, IResponseCacheEntry entry, TimeSpan validFor, long size)
87+
public Task SetAsync(string key, IResponseCacheEntry entry, TimeSpan validFor)
8888
{
89-
Set(key, entry, validFor, size);
89+
Set(key, entry, validFor);
9090
return Task.CompletedTask;
9191
}
9292
}

src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingMiddleware.cs

Lines changed: 3 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -322,23 +322,15 @@ internal void FinalizeCacheHeaders(ResponseCachingContext context)
322322
{
323323
if (OnFinalizeCacheHeaders(context))
324324
{
325-
_cache.Set(
326-
context.BaseKey,
327-
context.CachedVaryByRules,
328-
context.CachedResponseValidFor,
329-
EstimateCachedVaryByRulesySize(context.CachedVaryByRules));
325+
_cache.Set(context.BaseKey, context.CachedVaryByRules, context.CachedResponseValidFor);
330326
}
331327
}
332328

333329
internal Task FinalizeCacheHeadersAsync(ResponseCachingContext context)
334330
{
335331
if (OnFinalizeCacheHeaders(context))
336332
{
337-
return _cache.SetAsync(
338-
context.BaseKey,
339-
context.CachedVaryByRules,
340-
context.CachedResponseValidFor,
341-
EstimateCachedVaryByRulesySize(context.CachedVaryByRules));
333+
return _cache.SetAsync(context.BaseKey, context.CachedVaryByRules, context.CachedResponseValidFor);
342334
}
343335
return Task.CompletedTask;
344336
}
@@ -361,11 +353,7 @@ internal async Task FinalizeCacheBodyAsync(ResponseCachingContext context)
361353
context.CachedResponse.Body = bufferStream;
362354
_logger.LogResponseCached();
363355

364-
await _cache.SetAsync(
365-
context.StorageVaryKey ?? context.BaseKey,
366-
context.CachedResponse,
367-
context.CachedResponseValidFor,
368-
EstimateCachedResponseSize(context.CachedResponse));
356+
await _cache.SetAsync(context.StorageVaryKey ?? context.BaseKey, context.CachedResponse, context.CachedResponseValidFor);
369357
}
370358
else
371359
{
@@ -537,72 +525,5 @@ internal static StringValues GetOrderCasingNormalizedStringValues(StringValues s
537525
return new StringValues(newArray);
538526
}
539527
}
540-
541-
internal static long EstimateCachedResponseSize(CachedResponse cachedResponse)
542-
{
543-
checked
544-
{
545-
// StatusCode
546-
long size = sizeof(int);
547-
548-
// Headers
549-
if (cachedResponse.Headers != null)
550-
{
551-
foreach (var item in cachedResponse.Headers)
552-
{
553-
size += item.Key.Length * sizeof(char) + EstimateStringValuesSize(item.Value);
554-
}
555-
}
556-
557-
// Body
558-
if (cachedResponse.Body != null)
559-
{
560-
size += cachedResponse.Body.Length;
561-
}
562-
563-
return size;
564-
}
565-
}
566-
567-
internal static long EstimateCachedVaryByRulesySize(CachedVaryByRules cachedVaryByRules)
568-
{
569-
checked
570-
{
571-
var size = 0L;
572-
573-
// VaryByKeyPrefix
574-
if (!string.IsNullOrEmpty(cachedVaryByRules.VaryByKeyPrefix))
575-
{
576-
size = cachedVaryByRules.VaryByKeyPrefix.Length * sizeof(char);
577-
}
578-
579-
// Headers
580-
size += EstimateStringValuesSize(cachedVaryByRules.Headers);
581-
582-
// QueryKeys
583-
size += EstimateStringValuesSize(cachedVaryByRules.QueryKeys);
584-
585-
return size;
586-
}
587-
}
588-
589-
internal static long EstimateStringValuesSize(StringValues stringValues)
590-
{
591-
checked
592-
{
593-
var size = 0L;
594-
595-
for (var i = 0; i < stringValues.Count; i++)
596-
{
597-
var stringValue = stringValues[i];
598-
if (!string.IsNullOrEmpty(stringValue))
599-
{
600-
size += stringValues[i].Length * sizeof(char);
601-
}
602-
}
603-
604-
return size;
605-
}
606-
}
607528
}
608529
}

test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCachingMiddlewareTests.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ await cache.SetAsync(
6565
Headers = new HeaderDictionary(),
6666
Body = new SegmentReadStream(new List<byte[]>(0), 0)
6767
},
68-
TimeSpan.Zero,
69-
0);
68+
TimeSpan.Zero);
7069

7170
Assert.True(await middleware.TryServeFromCacheAsync(context));
7271
Assert.Equal(1, cache.GetCount);
@@ -94,8 +93,7 @@ await cache.SetAsync(
9493
},
9594
Body = new SegmentReadStream(new List<byte[]>(0), 0)
9695
},
97-
TimeSpan.Zero,
98-
0);
96+
TimeSpan.Zero);
9997

10098
Assert.True(await middleware.TryServeFromCacheAsync(context));
10199
Assert.Equal("NewValue", context.HttpContext.Response.Headers["MyHeader"]);
@@ -116,8 +114,7 @@ public async Task TryServeFromCacheAsync_VaryByRuleFound_CachedResponseNotFound_
116114
await cache.SetAsync(
117115
"BaseKey",
118116
new CachedVaryByRules(),
119-
TimeSpan.Zero,
120-
0);
117+
TimeSpan.Zero);
121118

122119
Assert.False(await middleware.TryServeFromCacheAsync(context));
123120
Assert.Equal(2, cache.GetCount);
@@ -137,17 +134,15 @@ public async Task TryServeFromCacheAsync_VaryByRuleFound_CachedResponseFound_Suc
137134
await cache.SetAsync(
138135
"BaseKey",
139136
new CachedVaryByRules(),
140-
TimeSpan.Zero,
141-
0);
137+
TimeSpan.Zero);
142138
await cache.SetAsync(
143139
"BaseKeyVaryKey2",
144140
new CachedResponse()
145141
{
146142
Headers = new HeaderDictionary(),
147143
Body = new SegmentReadStream(new List<byte[]>(0), 0)
148144
},
149-
TimeSpan.Zero,
150-
0);
145+
TimeSpan.Zero);
151146

152147
Assert.True(await middleware.TryServeFromCacheAsync(context));
153148
Assert.Equal(3, cache.GetCount);
@@ -171,8 +166,7 @@ await cache.SetAsync(
171166
{
172167
Body = new SegmentReadStream(new List<byte[]>(0), 0)
173168
},
174-
TimeSpan.Zero,
175-
0);
169+
TimeSpan.Zero);
176170

177171
Assert.True(await middleware.TryServeFromCacheAsync(context));
178172
Assert.Equal(1, cache.GetCount);

test/Microsoft.AspNetCore.ResponseCaching.Tests/TestUtils.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,15 +373,15 @@ public Task<IResponseCacheEntry> GetAsync(string key)
373373
return Task.FromResult(Get(key));
374374
}
375375

376-
public void Set(string key, IResponseCacheEntry entry, TimeSpan validFor, long size)
376+
public void Set(string key, IResponseCacheEntry entry, TimeSpan validFor)
377377
{
378378
SetCount++;
379379
_storage[key] = entry;
380380
}
381381

382-
public Task SetAsync(string key, IResponseCacheEntry entry, TimeSpan validFor, long size)
382+
public Task SetAsync(string key, IResponseCacheEntry entry, TimeSpan validFor)
383383
{
384-
Set(key, entry, validFor, size);
384+
Set(key, entry, validFor);
385385
return Task.CompletedTask;
386386
}
387387
}

0 commit comments

Comments
 (0)