OutputCacheModule.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. DoCacheInsert (context);
  67. result = new HttpAsyncResult (cb, this);
  68. result.Complete (true, o, null);
  69. return result;
  70. }
  71. void OnEndUpdateCache (IAsyncResult result)
  72. {
  73. }
  74. private void DoCacheInsert (HttpContext context)
  75. {
  76. string vary_key = context.Request.FilePath;
  77. string key;
  78. CachedVaryBy varyby = context.Cache [vary_key] as CachedVaryBy;
  79. CachedRawResponse prev = null;
  80. bool lookup = true;
  81. if (varyby == null) {
  82. varyby = new CachedVaryBy (context.Response.Cache);
  83. context.Cache.InsertPrivate (vary_key, varyby, null,
  84. Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
  85. CacheItemPriority.Normal, null);
  86. lookup = false;
  87. }
  88. key = varyby.CreateKey (vary_key, context);
  89. if (lookup)
  90. prev = context.Cache [key] as CachedRawResponse;
  91. if (IsExpired (context, prev)) {
  92. CachedRawResponse c = context.Response.GetCachedResponse ();
  93. string [] files = new string [0];
  94. string [] keys = new string [] { vary_key };
  95. context.Cache.InsertPrivate (key, c, new CacheDependency (files, keys),
  96. context.Response.Cache.Expires,
  97. Cache.NoSlidingExpiration,
  98. CacheItemPriority.Normal, null);
  99. }
  100. }
  101. private bool IsExpired (HttpContext context, CachedRawResponse crr)
  102. {
  103. if (crr == null || context.Timestamp > crr.Policy.Expires)
  104. return true;
  105. return false;
  106. }
  107. }
  108. }