OutputCacheModule.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. }
  54. leave:
  55. HttpAsyncResult result = new HttpAsyncResult (cb,this);
  56. result.Complete (true, o, null);
  57. return result;
  58. }
  59. void OnEndRequestCache (IAsyncResult result)
  60. {
  61. }
  62. IAsyncResult OnBeginUpdateCache (object o, EventArgs args, AsyncCallback cb, object data)
  63. {
  64. HttpApplication app = (HttpApplication) o;
  65. HttpContext context = app.Context;
  66. HttpAsyncResult result;
  67. if (context.Response.IsCached && context.Response.StatusCode == 200 &&
  68. !context.Trace.IsEnabled)
  69. DoCacheInsert (context);
  70. result = new HttpAsyncResult (cb, this);
  71. result.Complete (true, o, null);
  72. return result;
  73. }
  74. void OnEndUpdateCache (IAsyncResult result)
  75. {
  76. }
  77. private void DoCacheInsert (HttpContext context)
  78. {
  79. string vary_key = context.Request.FilePath;
  80. string key;
  81. CachedVaryBy varyby = context.Cache [vary_key] as CachedVaryBy;
  82. CachedRawResponse prev = null;
  83. bool lookup = true;
  84. if (varyby == null) {
  85. string path = context.Request.MapPath (vary_key);
  86. string [] files = new string [] { path };
  87. string [] keys = new string [0];
  88. varyby = new CachedVaryBy (context.Response.Cache, vary_key);
  89. context.Cache.InsertPrivate (vary_key, varyby,
  90. new CacheDependency (files, keys),
  91. Cache.NoAbsoluteExpiration,
  92. Cache.NoSlidingExpiration,
  93. CacheItemPriority.Normal, null);
  94. lookup = false;
  95. }
  96. key = varyby.CreateKey (vary_key, context);
  97. if (lookup)
  98. prev = context.Cache [key] as CachedRawResponse;
  99. if (prev == null) {
  100. CachedRawResponse c = context.Response.GetCachedResponse ();
  101. string [] files = new string [] { };
  102. string [] keys = new string [] { vary_key };
  103. bool sliding = context.Response.Cache.Sliding;
  104. context.Cache.InsertPrivate (key, c, new CacheDependency (files, keys),
  105. (sliding ? Cache.NoAbsoluteExpiration :
  106. context.Response.Cache.Expires),
  107. (sliding ? TimeSpan.FromSeconds (
  108. context.Response.Cache.Duration) :
  109. Cache.NoSlidingExpiration),
  110. CacheItemPriority.Normal, response_removed);
  111. c.VaryBy = varyby;
  112. varyby.ItemList.Add (key);
  113. }
  114. }
  115. private void OnRawResponseRemoved (string key, object value, CacheItemRemovedReason reason)
  116. {
  117. CachedRawResponse c = (CachedRawResponse) value;
  118. c.VaryBy.ItemList.Remove (key);
  119. if (c.VaryBy.ItemList.Count != 0)
  120. return;
  121. Cache cache = HttpRuntime.Cache;
  122. cache.Remove (c.VaryBy.Key);
  123. }
  124. }
  125. }