OutputCacheModule.cs 4.0 KB

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