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