OutputCacheModule.cs 3.5 KB

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