Skip to content

Commit 4de8975

Browse files
committed
fixing Nuget package build for the provider, fix some xml comments and code feedback
1 parent fb5ef50 commit 4de8975

11 files changed

Lines changed: 117 additions & 26 deletions

src/CustomOutputCacheProvider/CustomOutputCacheProvider.cs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,25 @@
55

66
namespace Microsoft.AspNet.OutputCache.CustomOutputCacheProvider {
77

8-
public class CustomOutputCacheItem {
8+
internal class CustomOutputCacheItem {
99
public object Obj;
1010
public DateTime UtcExpiry;
11-
1211
public CustomOutputCacheItem(object entry, DateTime utcExpiryIn) {
1312
Obj = entry;
1413
UtcExpiry = utcExpiryIn;
1514
}
1615
}
1716

17+
/// <summary>
18+
/// This is just a proof of concept Async OutputCache Provider. It is used for testing purpose.
19+
/// </summary>
1820
public class CustomOutputCacheProvider : OutputCacheProviderAsync {
1921

2022
private readonly Dictionary<string, CustomOutputCacheItem> _dict;
2123

24+
/// <summary>
25+
/// Async OutputCache Provider
26+
/// </summary>
2227
public CustomOutputCacheProvider() {
2328
_dict = new Dictionary<string, CustomOutputCacheItem>();
2429
}
@@ -27,6 +32,11 @@ private static async Task FooAsync() {
2732
await Task.Delay(1);
2833
}
2934

35+
/// <summary>
36+
/// Override method for the Async OutputCache Provider
37+
/// </summary>
38+
/// <param name="key"></param>
39+
/// <returns></returns>
3040
public override async Task<object> GetAsync(string key) {
3141
await FooAsync();
3242
if (!_dict.ContainsKey(key)) {
@@ -40,6 +50,13 @@ public override async Task<object> GetAsync(string key) {
4050
}
4151

4252

53+
/// <summary>
54+
/// Override method for the Async OutputCache Provider
55+
/// </summary>
56+
/// <param name="key"></param>
57+
/// <param name="entry"></param>
58+
/// <param name="utcExpiry"></param>
59+
/// <returns></returns>
4360
public override async Task<object> AddAsync(string key, object entry, DateTime utcExpiry) {
4461
await FooAsync();
4562
if (_dict.ContainsKey(key) && _dict[key].UtcExpiry > DateTime.Now.ToUniversalTime()) {
@@ -54,6 +71,13 @@ public override async Task<object> AddAsync(string key, object entry, DateTime u
5471
return entry;
5572
}
5673

74+
/// <summary>
75+
/// Override method for the Async OutputCache Provider
76+
/// </summary>
77+
/// <param name="key"></param>
78+
/// <param name="entry"></param>
79+
/// <param name="utcExpiry"></param>
80+
/// <returns></returns>
5781
public override async Task SetAsync(string key, object entry, DateTime utcExpiry) {
5882
await FooAsync();
5983
if (_dict.ContainsKey(key)) {
@@ -64,11 +88,21 @@ public override async Task SetAsync(string key, object entry, DateTime utcExpiry
6488
}
6589
}
6690

91+
/// <summary>
92+
/// Override method for the Async OutputCache Provider
93+
/// </summary>
94+
/// <param name="key"></param>
95+
/// <returns></returns>
6796
public override async Task RemoveAsync(string key) {
6897
await FooAsync();
6998
_dict.Remove(key);
7099
}
71100

101+
/// <summary>
102+
/// Override method for the Async OutputCache Provider
103+
/// </summary>
104+
/// <param name="key"></param>
105+
/// <returns></returns>
72106
public override object Get(string key) {
73107
if (!_dict.ContainsKey(key)) {
74108
return null;
@@ -80,6 +114,13 @@ public override object Get(string key) {
80114
return null;
81115
}
82116

117+
/// <summary>
118+
/// Override method for the Async OutputCache Provider
119+
/// </summary>
120+
/// <param name="key"></param>
121+
/// <param name="entry"></param>
122+
/// <param name="utcExpiry"></param>
123+
/// <returns></returns>
83124
public override object Add(string key, object entry, DateTime utcExpiry) {
84125
if (_dict.ContainsKey(key) && _dict[key].UtcExpiry > DateTime.Now.ToUniversalTime()) {
85126
return _dict[key].Obj;
@@ -93,6 +134,12 @@ public override object Add(string key, object entry, DateTime utcExpiry) {
93134
return entry;
94135
}
95136

137+
/// <summary>
138+
/// Override method for the Async OutputCache Provider
139+
/// </summary>
140+
/// <param name="key"></param>
141+
/// <param name="entry"></param>
142+
/// <param name="utcExpiry"></param>
96143
public override void Set(string key, object entry, DateTime utcExpiry) {
97144
if (_dict.ContainsKey(key)) {
98145
_dict[key] = new CustomOutputCacheItem(entry, utcExpiry);
@@ -102,6 +149,10 @@ public override void Set(string key, object entry, DateTime utcExpiry) {
102149
}
103150
}
104151

152+
/// <summary>
153+
/// Override method for the Async OutputCache Provider
154+
/// </summary>
155+
/// <param name="key"></param>
105156
public override void Remove(string key) {
106157
_dict.Remove(key);
107158
}

src/OutputCacheModuleAsync/InMemoryOutputCacheProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ internal class InMemoryOutputCacheProvider : OutputCacheProviderAsync {
88
private readonly MemoryCache _cache = new MemoryCache("Microsoft.AspNet.OutputCache Default In-Memory Provider");
99

1010
public override Task<object> GetAsync(string key) {
11-
return Task.Run(() => Get(key));
11+
return Task.FromResult(Get(key)); //.Run(() => Get(key));
1212
}
1313

1414
public override Task<object> AddAsync(string key, object entry, DateTime utcExpiry) {
15-
return Task.Run(() => Add(key, entry, utcExpiry));
15+
return Task.FromResult(Add(key,entry,utcExpiry));//.Run(() => Add(key, entry, utcExpiry));
1616
}
1717

1818
public override Task SetAsync(string key, object entry, DateTime utcExpiry) {

src/OutputCacheModuleAsync/OutputCacheHelper.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Microsoft.AspNet.OutputCache {
1+
using System.IO;
2+
3+
namespace Microsoft.AspNet.OutputCache {
24
using System.Collections.Generic;
35
using System.Linq;
46
using System;
@@ -388,7 +390,7 @@ public static void UpdateCachedHeaders(HttpResponse response) {
388390
}
389391
}
390392

391-
public static async Task<string> CreateOutputCachedItemKeyAsync(
393+
public static string CreateOutputCachedItemKey(
392394
string path,
393395
string verb,
394396
HttpContext context,
@@ -494,13 +496,18 @@ public static async Task<string> CreateOutputCachedItemKeyAsync(
494496
return null;
495497
}
496498
if (contentLength > 0) {
497-
await request.InputStream.ReadAsync(null, 0, System.Convert.ToInt32(request.InputStream.Length));
498-
return null;
499+
using (var ms = new MemoryStream()) {
500+
request.InputStream.CopyTo(ms);
501+
byte[] buf = ms.ToArray();
502+
// Use SHA256 to generate a collision-free hash of the input data
503+
value = System.Convert.ToBase64String((CryptoUtil.ComputeSha256Hash(buf)));
504+
sb.Append(value);
505+
}
499506
}
500507
}
501508
/*
502-
* VaryByContentEncoding
503-
*/
509+
* VaryByContentEncoding
510+
*/
504511
sb.Append("E");
505512
string[] contentEncodings = cachedVary.ContentEncodings;
506513
if (contentEncodings == null) {
@@ -522,9 +529,8 @@ public static async Task<string> CreateOutputCachedItemKeyAsync(
522529
* and form posted data.
523530
*/
524531

525-
public static async Task<string> CreateOutputCachedItemKeyAsync(HttpContext context, CachedVary cachedVary) {
526-
return
527-
await CreateOutputCachedItemKeyAsync(context.Request.Path, context.Request.HttpMethod, context, cachedVary);
532+
public static string CreateOutputCachedItemKeyAsync(HttpContext context, CachedVary cachedVary) {
533+
return CreateOutputCachedItemKey(context.Request.Path, context.Request.HttpMethod, context, cachedVary);
528534
}
529535

530536
/*
@@ -553,7 +559,7 @@ public static int GetAcceptableEncoding(string[] contentEncodings, int startInde
553559
}
554560
acceptEncodingWithoutWeight = acceptEncoding.Substring(0, tokenEnd);
555561
if (Math.Abs(ParseWeight(acceptEncoding, tokenEnd)) < 0) {
556-
// WOS 1985352 & WOS 1985353: weight is 0, use "identity" only if it is acceptable
562+
//weight is 0, use "identity" only if it is acceptable
557563
bool identityIsAcceptable = acceptEncodingWithoutWeight != Identity &&
558564
acceptEncodingWithoutWeight != Asterisk;
559565
return (identityIsAcceptable) ? -1 : -2;

src/OutputCacheModuleAsync/OutputCacheModuleAsync.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
using System.Diagnostics;
1212
using System.Configuration;
1313

14+
/// <summary>
15+
/// OutputCache Async Module, this Module is able to use Async type of OutputCache Providers
16+
/// </summary>
1417
public class OutputCacheModuleAsync : IHttpModule {
1518
private const string Asterisk = "*";
1619
private static readonly char[] s_fieldSeparators = {',', ' '};
@@ -27,6 +30,9 @@ void IHttpModule.Init(HttpApplication app) {
2730
app.AddOnUpdateRequestCacheAsync(BeginOnUpdateRequestCache, EndOnUpdateRequestCache);
2831
}
2932

33+
/// <summary>
34+
/// Implement the IHTTPModule interface
35+
/// </summary>
3036
public void Dispose() {}
3137

3238
private IAsyncResult BeginOnResolveRequestCache(object source, EventArgs e, AsyncCallback cb, object extraData) {

src/OutputCacheModuleAsync/Util.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,35 @@ public int Compare(object a, object b) {
2222
}
2323
}
2424

25+
internal class CryptoUtil {
26+
/// <summary>
27+
/// Computes the SHA256 hash of a given input.
28+
/// </summary>
29+
/// <param Name="input">The input over which to compute the hash.</param>
30+
/// <param name="input"></param>
31+
/// <returns>The binary hash (32 bytes) of the input.</returns>
32+
public static byte[] ComputeSha256Hash(byte[] input) {
33+
return ComputeSha256Hash(input, 0, input.Length);
34+
}
35+
36+
/// <summary>
37+
/// Computes the SHA256 hash of a given segment in a buffer.
38+
/// </summary>
39+
/// <param Name="buffer">The buffer over which to compute the hash.</param>
40+
/// <param Name="offset">The offset at which to begin computing the hash.</param>
41+
/// <param Name="count">The number of bytes in the buffer to include in the hash.</param>
42+
/// <param name="buffer"></param>
43+
/// <param name="offset"></param>
44+
/// <param name="count"></param>
45+
/// <returns>The binary hash (32 bytes) of the buffer segment.</returns>
46+
public static byte[] ComputeSha256Hash(byte[] buffer, int offset, int count) {
47+
48+
using (SHA256 sha256 = new SHA256Cng()) {
49+
return sha256.ComputeHash(buffer, offset, count);
50+
}
51+
}
52+
}
53+
2554
internal class HttpDate {
2655
private static readonly int[] s_tensDigit = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90};
2756

src/packages/CustomOutputCacheProvider.nupkg/Microsoft.AspNet.OutputCache.CustomOutputCacheProvider.nuspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<copyright>&#169; Microsoft Corporation. All rights reserved.</copyright>
99
<dependencies>
1010
<dependency id="Microsoft.AspNet.OutputCache.OutputCacheModuleAsync" version="$OutputCacheModuleAsyncNuGetPackageVersion$" />
11-
<dependency id="EntityFramework" version="$EntityFrameworkNuGetPackageVersion$" />
1211
</dependencies>
1312
<title>Microsoft ASP.NET Custom OutputCache Providers</title>
1413
<description>Async version Custom OutputCache provider</description>

src/packages/CustomOutputCacheProvider.nupkg/content/Net462/web.config.install.xdt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
33
<system.web>
4-
<outputCache xdt:Transform="Remove">
5-
</outputCache>
4+
<caching>
5+
<outputCache xdt:Transform="Remove">
6+
</outputCache>
7+
</caching>
68
</system.web>
79
<system.web>
8-
<caching>
9-
<outputCache defaultProvider="CustomOutputCacheProvider" xdt:Transform="Insert">
10+
<caching xdt:Transform="InsertIfMissing">
11+
<outputCache defaultProvider="CustomOutputCacheProvider" xdt:Transform="InsertIfMissing">
1012
<providers>
1113
<add name="CustomOutputCacheProvider"
12-
type="Microsoft.AspNet.OutputCache.CustomOutputCacheProvider, CustomOutputCacheProvider"
14+
type="Microsoft.AspNet.OutputCache.CustomOutputCacheProvider.CustomOutputCacheProvider, Microsoft.AspNet.OutputCache.CustomOutputCacheProvider"
1315
xdt:Transform="InsertIfMissing" />
1416
</providers>
1517
</outputCache>

src/packages/CustomOutputCacheProvider.nupkg/content/Net462/web.config.uninstall.xdt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<outputCache defaultProvider="CustomOutputCacheProvider" xdt:Transform="Remove">
66
<providers>
77
<add name="CustomOutputCacheProvider"
8-
type="Microsoft.AspNet.OutputCache.CustomOutputCacheProvider, CustomOutputCacheProvider"
8+
type="Microsoft.AspNet.OutputCache.CustomOutputCacheProvider.CustomOutputCacheProvider, Microsoft.AspNet.OutputCache.CustomOutputCacheProvider"
99
xdt:Transform="Remove" xdt:Locator="Match(type)" />
1010
</providers>
1111
</outputCache>

tools/MicrosoftAspNetOutputCache.Extensions.settings.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<AuthCodeCert>10006</AuthCodeCert>
5555
<StrongNameCert>72</StrongNameCert>
5656
</CodeSign>
57-
<CodeSign Include="$(OutputPath)Microsoft.AspNet.OutputCache.CustomOutputCacheProvider.dll">
57+
<CodeSign Include="$(OutputPath)Microsoft.AspNet.OutputCache.CustomOutputCacheProvider.dll">
5858
<AuthCodeCert>10006</AuthCodeCert>
5959
<StrongNameCert>72</StrongNameCert>
6060
</CodeSign>

tools/MicrosoftAspNetOutputCache.settings.targets

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
</PropertyGroup>
1717

1818
<PropertyGroup Label="NuGet package dependencies">
19-
<OutputCacheAsyncModuleNuGetPackageVersion>1.0.0</OutputCacheAsyncModuleNuGetPackageVersion>
20-
<EntityFrameworkNuGetPackageVersion>6.1.3</EntityFrameworkNuGetPackageVersion>
19+
<OutputCacheModuleAsyncNuGetPackageVersion>1.0.0</OutputCacheModuleAsyncNuGetPackageVersion>
2120
</PropertyGroup>
2221

2322
<!-- Default properties -->

0 commit comments

Comments
 (0)