OutputCacheModule.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.IO;
  30. using System.Web;
  31. using System.Web.Util;
  32. using System.Collections;
  33. namespace System.Web.Caching {
  34. internal sealed class OutputCacheModule : IHttpModule {
  35. private CacheItemRemovedCallback response_removed;
  36. public OutputCacheModule ()
  37. {
  38. }
  39. public void Dispose ()
  40. {
  41. }
  42. public void Init (HttpApplication app)
  43. {
  44. app.AddOnResolveRequestCacheAsync (
  45. new BeginEventHandler (OnBeginRequestCache),
  46. new EndEventHandler (OnEndRequestCache));
  47. app.AddOnUpdateRequestCacheAsync (
  48. new BeginEventHandler (OnBeginUpdateCache),
  49. new EndEventHandler (OnEndUpdateCache));
  50. response_removed = new CacheItemRemovedCallback (OnRawResponseRemoved);
  51. }
  52. IAsyncResult OnBeginRequestCache (object o, EventArgs args, AsyncCallback cb, object data)
  53. {
  54. HttpApplication app = (HttpApplication) o;
  55. HttpContext context = app.Context;
  56. string vary_key = context.Request.FilePath;
  57. CachedVaryBy varyby = context.Cache [vary_key] as CachedVaryBy;
  58. string key;
  59. CachedRawResponse c;
  60. if (varyby == null)
  61. goto leave;
  62. key = varyby.CreateKey (vary_key, context);
  63. c = context.Cache [key] as CachedRawResponse;
  64. if (c != null) {
  65. context.Response.ClearContent ();
  66. context.Response.BinaryWrite (c.GetData (), 0, c.ContentLength);
  67. context.Response.ClearHeaders ();
  68. c.DateHeader.Value = TimeUtil.ToUtcTimeString (DateTime.Now);
  69. context.Response.SetCachedHeaders (c.Headers);
  70. context.Response.StatusCode = c.StatusCode;
  71. context.Response.StatusDescription = c.StatusDescription;
  72. app.CompleteRequest ();
  73. }
  74. leave:
  75. HttpAsyncResult result = new HttpAsyncResult (cb,this);
  76. result.Complete (true, o, null);
  77. return result;
  78. }
  79. void OnEndRequestCache (IAsyncResult result)
  80. {
  81. }
  82. IAsyncResult OnBeginUpdateCache (object o, EventArgs args, AsyncCallback cb, object data)
  83. {
  84. HttpApplication app = (HttpApplication) o;
  85. HttpContext context = app.Context;
  86. HttpAsyncResult result;
  87. if (context.Response.IsCached && context.Response.StatusCode == 200 &&
  88. !context.Trace.IsEnabled)
  89. DoCacheInsert (context);
  90. result = new HttpAsyncResult (cb, this);
  91. result.Complete (true, o, null);
  92. return result;
  93. }
  94. void OnEndUpdateCache (IAsyncResult result)
  95. {
  96. }
  97. private void DoCacheInsert (HttpContext context)
  98. {
  99. string vary_key = context.Request.FilePath;
  100. string key;
  101. CachedVaryBy varyby = context.Cache [vary_key] as CachedVaryBy;
  102. CachedRawResponse prev = null;
  103. bool lookup = true;
  104. if (varyby == null) {
  105. string path = context.Request.MapPath (vary_key);
  106. string [] files = new string [] { path };
  107. string [] keys = new string [0];
  108. varyby = new CachedVaryBy (context.Response.Cache, vary_key);
  109. context.Cache.InsertPrivate (vary_key, varyby,
  110. new CacheDependency (files, keys),
  111. Cache.NoAbsoluteExpiration,
  112. Cache.NoSlidingExpiration,
  113. CacheItemPriority.Normal, null);
  114. lookup = false;
  115. }
  116. key = varyby.CreateKey (vary_key, context);
  117. if (lookup)
  118. prev = context.Cache [key] as CachedRawResponse;
  119. if (prev == null) {
  120. CachedRawResponse c = context.Response.GetCachedResponse ();
  121. string [] files = new string [] { };
  122. string [] keys = new string [] { vary_key };
  123. bool sliding = context.Response.Cache.Sliding;
  124. context.Cache.InsertPrivate (key, c, new CacheDependency (files, keys),
  125. (sliding ? Cache.NoAbsoluteExpiration :
  126. context.Response.Cache.Expires),
  127. (sliding ? TimeSpan.FromSeconds (
  128. context.Response.Cache.Duration) :
  129. Cache.NoSlidingExpiration),
  130. CacheItemPriority.Normal, response_removed);
  131. c.VaryBy = varyby;
  132. varyby.ItemList.Add (key);
  133. }
  134. }
  135. private void OnRawResponseRemoved (string key, object value, CacheItemRemovedReason reason)
  136. {
  137. CachedRawResponse c = (CachedRawResponse) value;
  138. c.VaryBy.ItemList.Remove (key);
  139. if (c.VaryBy.ItemList.Count != 0)
  140. return;
  141. Cache cache = HttpRuntime.Cache;
  142. cache.Remove (c.VaryBy.Key);
  143. }
  144. }
  145. }