Skip to content

Commit d837210

Browse files
committed
refractor with code review feedbacks
1 parent 8dc6676 commit d837210

10 files changed

Lines changed: 200 additions & 443 deletions

File tree

src/OutputCacheModuleAsync/CachedVary.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public CachedVary() {
1414
}
1515

1616
public override bool Equals(object obj) {
17+
if (Object.ReferenceEquals(obj,this)) {
18+
return true;
19+
}
1720
var cv = obj as CachedVary;
1821
if (cv == null) {
1922
return false;

src/OutputCacheModuleAsync/HttpCachePolicySettings.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public bool ValidUntilExpires {
2121
return !SlidingExpiration && !GenerateLastModifiedFromFiles && !GenerateEtagFromFiles
2222
&& ValidationCallbackInfo == null;
2323
}
24-
set { }
2524
}
2625

2726
public DateTime UtcLastModified { get; set; }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Security.Policy;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Microsoft.AspNet.OutputCache {
9+
static class HttpMethods {
10+
public const string POST = "POST";
11+
public const string HEAD = "HEAD";
12+
public const string GET = "GET";
13+
}
14+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Security.Policy;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Microsoft.AspNet.OutputCache {
9+
static class HttpRequestHeaders {
10+
public const string IfModifiedSite = "If-Modified-Since";
11+
public const string IfNoneMatch = "If-None-Match";
12+
public const string AcceptEncoding = "Accept-Encoding";
13+
public const string ContentEncoding = "Content-Encoding";
14+
public const string CacheControl = "Cache-Control";
15+
public const string Pragma = "Pragma";
16+
public const string Range = "Range";
17+
}
18+
static class CacheDirectives {
19+
public const string NoCache = "no-cache";
20+
public const string NoStore = "no-store";
21+
public const string MaxAge = "max-age=";
22+
public const string MinFresh = "min-fresh=";
23+
public const string CacheControl = "Cache-Control";
24+
public const string Pragma = "Pragma";
25+
public const string Range = "Range";
26+
}
27+
}

src/OutputCacheModuleAsync/OutputCacheHelper.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@
1212
using System.Diagnostics;
1313
using System.IO;
1414
using Microsoft.AspNet.OutputCache.Resource;
15+
using System.Runtime.Caching;
1516

1617
class OutputCacheHelper {
1718
private const int MaxPostKeyLength = 15000;
1819
private const string NullVarybyValue = "+n+";
19-
internal const string TagOutputcache = "OutputCache";
20+
const string TagOutputcache = "OutputCache";
2021
private const string OutputcacheKeyprefixPost = "a1";
2122
private const string OutputcacheKeyprefixGet = "a2";
2223
private const string Identity = "identity";
2324
private const string Asterisk = "*";
2425
private const string OutputcacheKeyprefixDependencies = "Microsoft.AspNet.OutputCache.Dependencies";
26+
private MemoryCache _memoryCache;
27+
private MemoryCache MemoryCache => _memoryCache ?? (_memoryCache = new MemoryCache("MemoryCache"));
2528
private InMemoryOutputCacheProvider _inMemoryOutputCacheProvider;
2629

2730
private InMemoryOutputCacheProvider InMemoryOutputCacheProvider => _inMemoryOutputCacheProvider ??
@@ -61,7 +64,7 @@ private bool HasDependencyChanged(string depKey, string[] fileDeps, string kerne
6164
return false;
6265
}
6366
// is the file dependency already in the in-memory cache?
64-
if (InMemoryOutputCacheProvider.Get(depKey) != null) {
67+
if (MemoryCache.Get(depKey) != null) {
6568
return false;
6669
}
6770
// deserialize the file dependencies
@@ -79,11 +82,11 @@ private bool HasDependencyChanged(string depKey, string[] fileDeps, string kerne
7982
var dcew = new DependencyCacheEntryWrapper {
8083
DependencyCacheEntry = dce,
8184
Dependencies = dep,
82-
CacheItemPriority = CacheItemPriority.Normal,
85+
CacheItemPriority = System.Web.Caching.CacheItemPriority.Normal,
8386
DependencyCacheTimeSpan = Cache.NoSlidingExpiration,
8487
DependencyRemovedCallback = null
8588
};
86-
InMemoryOutputCacheProvider.Add(depKey, dcew, DateTimeOffset.MaxValue);
89+
MemoryCache.Add(depKey, dcew, DateTimeOffset.MaxValue);
8790
return false;
8891
}
8992
// file dependencies have changed
@@ -243,7 +246,7 @@ public async Task InsertResponseAsync(string cachedVaryKey,
243246
var dcew = new DependencyCacheEntryWrapper {
244247
DependencyCacheEntry = dce,
245248
Dependencies = dependencies,
246-
CacheItemPriority = CacheItemPriority.Normal,
249+
CacheItemPriority = System.Web.Caching.CacheItemPriority.Normal,
247250
DependencyCacheTimeSpan = Cache.NoSlidingExpiration,
248251
DependencyRemovedCallback = null
249252
};
@@ -341,7 +344,6 @@ public static HttpCachePolicySettings GetCurrentSettings(HttpResponse response)
341344
MaxAge = response.Cache.GetMaxAge(),
342345
SlidingExpiration = response.Cache.HasSlidingExpiration(),
343346
IgnoreRangeRequests = response.Cache.GetIgnoreRangeRequests(),
344-
ValidUntilExpires = response.Cache.IsValidUntilExpires(),
345347
UtcLastModified = response.Cache.GetUtcLastModified(),
346348
ETag = response.Cache.GetETag(),
347349
GenerateLastModifiedFromFiles = response.Cache.GetLastModifiedFromFileDependencies(),
@@ -412,7 +414,7 @@ private static void UpdateFromDependencies(HttpResponse response) {
412414
sb.Append(utcFileLastModifiedMax.Ticks.ToString(CultureInfo.InvariantCulture));
413415
response.Cache.SetETag("\"" +
414416
System.Convert.ToBase64String(
415-
CryptoUtil.ComputeSha256Hash(Encoding.UTF8.GetBytes(sb.ToString()))) + "\"");
417+
CryptoUtil.ComputeHash(Encoding.UTF8.GetBytes(sb.ToString()))) + "\"");
416418
if (!response.Cache.GetLastModifiedFromFileDependencies())
417419
return;
418420
}
@@ -469,7 +471,7 @@ private static string CreateOutputCachedItemKey(
469471
string verb,
470472
HttpContext context,
471473
CachedVary cachedVary) {
472-
StringBuilder sb = verb == "POST"
474+
StringBuilder sb = verb == HttpMethods.POST
473475
? new StringBuilder(OutputcacheKeyprefixPost, path.Length + OutputcacheKeyprefixPost.Length)
474476
: new StringBuilder(OutputcacheKeyprefixGet, path.Length + OutputcacheKeyprefixGet.Length);
475477
sb.Append(CultureInfo.InvariantCulture.TextInfo.ToLower(path));
@@ -494,8 +496,6 @@ private static string CreateOutputCachedItemKey(
494496
}
495497
break;
496498
case 1:
497-
Debug.Assert(cachedVary.Params == null || !cachedVary.VaryByAllParams,
498-
"cachedVary._params == null || !cachedVary._varyByAllParams");
499499
sb.Append("Q");
500500
a = cachedVary.Params;
501501
if ((a != null || cachedVary.VaryByAllParams)) {
@@ -504,10 +504,8 @@ private static string CreateOutputCachedItemKey(
504504
}
505505
break;
506506
default:
507-
Debug.Assert(cachedVary.Params == null || !cachedVary.VaryByAllParams,
508-
"cachedVary._params == null || !cachedVary._varyByAllParams");
509507
sb.Append("F");
510-
if (verb == "POST") {
508+
if (verb == HttpMethods.POST) {
511509
a = cachedVary.Params;
512510
if ((a != null || cachedVary.VaryByAllParams)) {
513511
col = request.Form;
@@ -516,7 +514,6 @@ private static string CreateOutputCachedItemKey(
516514
}
517515
break;
518516
}
519-
Debug.Assert(a == null || !getAllParams, "a == null || !getAllParams");
520517
/* handle all params case (VaryByParams[*] = true) */
521518
int i;
522519
if (getAllParams && col.Count > 0) {
@@ -561,7 +558,7 @@ private static string CreateOutputCachedItemKey(
561558
* part of the key.
562559
*/
563560
sb.Append("D");
564-
if (verb == "POST" &&
561+
if (verb == HttpMethods.POST &&
565562
cachedVary.VaryByAllParams &&
566563
request.Form.Count == 0) {
567564

@@ -574,7 +571,7 @@ private static string CreateOutputCachedItemKey(
574571
request.InputStream.CopyTo(ms);
575572
byte[] buf = ms.ToArray();
576573
// Use SHA256 to generate a collision-free hash of the input data
577-
value = System.Convert.ToBase64String((CryptoUtil.ComputeSha256Hash(buf)));
574+
value = System.Convert.ToBase64String((CryptoUtil.ComputeHash(buf)));
578575
sb.Append(value);
579576
}
580577
}

0 commit comments

Comments
 (0)