OutputCacheModule.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // System.Web.Caching.OutputCacheModule
  3. //
  4. // Authors:
  5. // Jackson Harper ([email protected])
  6. //
  7. // (C) 2003 Novell, Inc (http://www.novell.com)
  8. //
  9. using System.IO;
  10. using System.Web;
  11. using System.Web.Util;
  12. using System.Collections;
  13. namespace System.Web.Caching {
  14. internal sealed class OutputCacheModule : IHttpModule {
  15. private CacheItemRemovedCallback response_removed;
  16. public OutputCacheModule ()
  17. {
  18. }
  19. public void Dispose ()
  20. {
  21. }
  22. public void Init (HttpApplication app)
  23. {
  24. app.AddOnResolveRequestCacheAsync (
  25. new BeginEventHandler (OnBeginRequestCache),
  26. new EndEventHandler (OnEndRequestCache));
  27. app.AddOnUpdateRequestCacheAsync (
  28. new BeginEventHandler (OnBeginUpdateCache),
  29. new EndEventHandler (OnEndUpdateCache));
  30. response_removed = new CacheItemRemovedCallback (OnRawResponseRemoved);
  31. }
  32. IAsyncResult OnBeginRequestCache (object o, EventArgs args, AsyncCallback cb, object data)
  33. {
  34. HttpApplication app = (HttpApplication) o;
  35. HttpContext context = app.Context;
  36. string vary_key = context.Request.FilePath;
  37. CachedVaryBy varyby = context.Cache [vary_key] as CachedVaryBy;
  38. string key;
  39. CachedRawResponse c;
  40. if (varyby == null)
  41. goto leave;
  42. key = varyby.CreateKey (vary_key, context);
  43. c = context.Cache [key] as CachedRawResponse;
  44. if (c != null) {
  45. context.Response.ClearContent ();
  46. context.Response.BinaryWrite (c.GetData (), 0, c.ContentLength);
  47. context.Response.ClearHeaders ();
  48. c.DateHeader.Value = TimeUtil.ToUtcTimeString (DateTime.Now);
  49. context.Response.SetCachedHeaders (c.Headers);
  50. context.Response.StatusCode = c.StatusCode;
  51. context.Response.StatusDescription = c.StatusDescription;
  52. app.CompleteRequest ();
  53. } else if (c != null) {
  54. context.Cache.Remove (key);
  55. }
  56. leave:
  57. HttpAsyncResult result = new HttpAsyncResult (cb,this);
  58. result.Complete (true, o, null);
  59. return result;
  60. }
  61. void OnEndRequestCache (IAsyncResult result)
  62. {
  63. }
  64. IAsyncResult OnBeginUpdateCache (object o, EventArgs args, AsyncCallback cb, object data)
  65. {
  66. HttpApplication app = (HttpApplication) o;
  67. HttpContext context = app.Context;
  68. HttpAsyncResult result;
  69. if (context.Response.IsCached && context.Response.StatusCode == 200 &&
  70. !context.Trace.IsEnabled)
  71. DoCacheInsert (context);
  72. result = new HttpAsyncResult (cb, this);
  73. result.Complete (true, o, null);
  74. return result;
  75. }
  76. void OnEndUpdateCache (IAsyncResult result)
  77. {
  78. }
  79. private void DoCacheInsert (HttpContext context)
  80. {
  81. string vary_key = context.Request.FilePath;
  82. string key;
  83. CachedVaryBy varyby = context.Cache [vary_key] as CachedVaryBy;
  84. CachedRawResponse prev = null;
  85. bool lookup = true;
  86. if (varyby == null) {
  87. string path = context.Request.MapPath (vary_key);
  88. string [] files = new string [] { path };
  89. string [] keys = new string [0];
  90. varyby = new CachedVaryBy (context.Response.Cache, vary_key);
  91. context.Cache.InsertPrivate (vary_key, varyby,
  92. new CacheDependency (files, keys),
  93. Cache.NoAbsoluteExpiration,
  94. Cache.NoSlidingExpiration,
  95. CacheItemPriority.Normal, null);
  96. lookup = false;
  97. }
  98. key = varyby.CreateKey (vary_key, context);
  99. if (lookup)
  100. prev = context.Cache [key] as CachedRawResponse;
  101. if (prev == null) {
  102. CachedRawResponse c = context.Response.GetCachedResponse ();
  103. string [] files = new string [] { };
  104. string [] keys = new string [] { vary_key };
  105. bool sliding = context.Response.Cache.Sliding;
  106. context.Cache.InsertPrivate (key, c, new CacheDependency (files, keys),
  107. (sliding ? Cache.NoAbsoluteExpiration :
  108. context.Response.Cache.Expires),
  109. (sliding ? TimeSpan.FromSeconds (
  110. context.Response.Cache.Duration) :
  111. Cache.NoSlidingExpiration),
  112. CacheItemPriority.Normal, response_removed);
  113. c.VaryBy = varyby;
  114. varyby.ItemList.Add (key);
  115. }
  116. }
  117. private void OnRawResponseRemoved (string key, object value, CacheItemRemovedReason reason)
  118. {
  119. CachedRawResponse c = (CachedRawResponse) value;
  120. c.VaryBy.ItemList.Remove (key);
  121. if (c.VaryBy.ItemList.Count != 0)
  122. return;
  123. Cache cache = HttpRuntime.Cache;
  124. cache.Remove (c.VaryBy.Key);
  125. }
  126. }
  127. }