OutputCacheModule.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.ResolveRequestCache += OnResolveRequestCache;
  45. app.UpdateRequestCache += OnUpdateRequestCache;
  46. response_removed = new CacheItemRemovedCallback (OnRawResponseRemoved);
  47. }
  48. void OnResolveRequestCache (object o, EventArgs args)
  49. {
  50. HttpApplication app = (HttpApplication) o;
  51. HttpContext context = app.Context;
  52. string vary_key = context.Request.FilePath;
  53. CachedVaryBy varyby = context.Cache [vary_key] as CachedVaryBy;
  54. string key;
  55. CachedRawResponse c;
  56. if (varyby == null)
  57. return;
  58. key = varyby.CreateKey (vary_key, context);
  59. c = context.Cache [key] as CachedRawResponse;
  60. if (c != null) {
  61. context.Response.ClearContent ();
  62. context.Response.BinaryWrite (c.GetData (), 0, c.ContentLength);
  63. context.Response.ClearHeaders ();
  64. c.DateHeader.Value = TimeUtil.ToUtcTimeString (DateTime.Now);
  65. context.Response.SetCachedHeaders (c.Headers);
  66. context.Response.StatusCode = c.StatusCode;
  67. context.Response.StatusDescription = c.StatusDescription;
  68. app.CompleteRequest ();
  69. }
  70. }
  71. void OnUpdateRequestCache (object o, EventArgs args)
  72. {
  73. HttpApplication app = (HttpApplication) o;
  74. HttpContext context = app.Context;
  75. if (context.Response.IsCached && context.Response.StatusCode == 200 &&
  76. !context.Trace.IsEnabled)
  77. DoCacheInsert (context);
  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. }