OutputCacheModule.cs 4.2 KB

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