OutputCacheModule.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //
  2. // System.Web.Caching.OutputCacheModule
  3. //
  4. // Authors:
  5. // Jackson Harper ([email protected])
  6. // Marek Habersack <[email protected]>
  7. //
  8. // (C) 2003-2009 Novell, Inc (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections;
  32. using System.Collections.Generic;
  33. using System.Configuration.Provider;
  34. using System.IO;
  35. using System.Text;
  36. using System.Web;
  37. using System.Web.Hosting;
  38. using System.Web.UI;
  39. using System.Web.Util;
  40. using System.Web.Compilation;
  41. namespace System.Web.Caching
  42. {
  43. sealed class OutputCacheModule : IHttpModule
  44. {
  45. CacheItemRemovedCallback response_removed;
  46. static object keysCacheLock = new object ();
  47. Dictionary <string, string> keysCache;
  48. Dictionary <string, string> entriesToInvalidate;
  49. #if !NET_4_0
  50. OutputCacheProvider provider;
  51. internal OutputCacheProvider InternalProvider {
  52. get { return provider; }
  53. }
  54. #endif
  55. public OutputCacheModule ()
  56. {
  57. }
  58. OutputCacheProvider FindCacheProvider (HttpApplication app)
  59. {
  60. #if NET_4_0
  61. HttpContext ctx = HttpContext.Current;
  62. if (app == null) {
  63. app = ctx != null ? ctx.ApplicationInstance : null;
  64. if (app == null)
  65. throw new InvalidOperationException ("Unable to find output cache provider.");
  66. }
  67. string providerName = app.GetOutputCacheProviderName (ctx);
  68. if (String.IsNullOrEmpty (providerName))
  69. throw new ProviderException ("Invalid OutputCacheProvider name. Name must not be null or an empty string.");
  70. OutputCacheProvider ret = OutputCache.GetProvider (providerName);
  71. if (ret == null)
  72. throw new ProviderException (String.Format ("OutputCacheProvider named '{0}' cannot be found.", providerName));
  73. return ret;
  74. #else
  75. if (provider == null)
  76. provider = new InMemoryOutputCacheProvider ();
  77. return provider;
  78. #endif
  79. }
  80. public void Dispose ()
  81. {
  82. }
  83. public void Init (HttpApplication context)
  84. {
  85. context.ResolveRequestCache += new EventHandler(OnResolveRequestCache);
  86. context.UpdateRequestCache += new EventHandler(OnUpdateRequestCache);
  87. response_removed = new CacheItemRemovedCallback (OnRawResponseRemoved);
  88. }
  89. void OnBuildManagerRemoveEntry (BuildManagerRemoveEntryEventArgs args)
  90. {
  91. string entry = args.EntryName;
  92. HttpContext context = args.Context;
  93. string cacheValue;
  94. lock (keysCacheLock) {
  95. if (!keysCache.TryGetValue (entry, out cacheValue))
  96. return;
  97. keysCache.Remove (entry);
  98. if (context == null) {
  99. if (entriesToInvalidate == null) {
  100. entriesToInvalidate = new Dictionary <string, string> (StringComparer.Ordinal);
  101. entriesToInvalidate.Add (entry, cacheValue);
  102. return;
  103. } else if (!entriesToInvalidate.ContainsKey (entry)) {
  104. entriesToInvalidate.Add (entry, cacheValue);
  105. return;
  106. }
  107. }
  108. }
  109. OutputCacheProvider provider = FindCacheProvider (context != null ? context.ApplicationInstance : null);
  110. provider.Remove (entry);
  111. if (!String.IsNullOrEmpty (cacheValue))
  112. provider.Remove (cacheValue);
  113. }
  114. void OnResolveRequestCache (object o, EventArgs args)
  115. {
  116. HttpApplication app = o as HttpApplication;
  117. HttpContext context = app != null ? app.Context : null;
  118. if (context == null)
  119. return;
  120. OutputCacheProvider provider = FindCacheProvider (app);
  121. string vary_key = context.Request.FilePath;
  122. CachedVaryBy varyby = provider.Get (vary_key) as CachedVaryBy;
  123. string key;
  124. CachedRawResponse c;
  125. if (varyby == null)
  126. return;
  127. key = varyby.CreateKey (vary_key, context);
  128. c = provider.Get (key) as CachedRawResponse;
  129. if (c == null)
  130. return;
  131. lock (keysCacheLock) {
  132. string invValue;
  133. if (entriesToInvalidate != null && entriesToInvalidate.TryGetValue (vary_key, out invValue) && String.Compare (invValue, key, StringComparison.Ordinal) == 0) {
  134. provider.Remove (vary_key);
  135. provider.Remove (key);
  136. entriesToInvalidate.Remove (vary_key);
  137. return;
  138. }
  139. }
  140. ArrayList callbacks = c.Policy.ValidationCallbacks;
  141. if (callbacks != null && callbacks.Count > 0) {
  142. bool isValid = true;
  143. bool isIgnored = false;
  144. foreach (Pair p in callbacks) {
  145. HttpCacheValidateHandler validate = (HttpCacheValidateHandler)p.First;
  146. object data = p.Second;
  147. HttpValidationStatus status = HttpValidationStatus.Valid;
  148. try {
  149. validate (context, data, ref status);
  150. } catch {
  151. // MS.NET hides the exception
  152. isValid = false;
  153. break;
  154. }
  155. if (status == HttpValidationStatus.Invalid) {
  156. isValid = false;
  157. break;
  158. } else if (status == HttpValidationStatus.IgnoreThisRequest) {
  159. isIgnored = true;
  160. }
  161. }
  162. if (!isValid) {
  163. OnRawResponseRemoved (key, c, CacheItemRemovedReason.Removed);
  164. return;
  165. } else if (isIgnored)
  166. return;
  167. }
  168. HttpResponse response = context.Response;
  169. response.ClearContent ();
  170. IList cachedData = c.GetData ();
  171. if (cachedData != null) {
  172. Encoding outEnc = WebEncoding.ResponseEncoding;
  173. foreach (CachedRawResponse.DataItem d in cachedData) {
  174. if (d.Length > 0) {
  175. response.BinaryWrite (d.Buffer, 0, (int)d.Length);
  176. continue;
  177. }
  178. if (d.Callback == null)
  179. continue;
  180. string s = d.Callback (context);
  181. if (s == null || s.Length == 0)
  182. continue;
  183. byte[] bytes = outEnc.GetBytes (s);
  184. response.BinaryWrite (bytes, 0, bytes.Length);
  185. }
  186. }
  187. response.ClearHeaders ();
  188. response.SetCachedHeaders (c.Headers);
  189. response.StatusCode = c.StatusCode;
  190. response.StatusDescription = c.StatusDescription;
  191. app.CompleteRequest ();
  192. }
  193. void OnUpdateRequestCache (object o, EventArgs args)
  194. {
  195. HttpApplication app = o as HttpApplication;
  196. HttpContext context = app != null ? app.Context : null;
  197. HttpResponse response = context != null ? context.Response : null;
  198. if (response != null && response.IsCached && response.StatusCode == 200 && !context.Trace.IsEnabled)
  199. DoCacheInsert (context, app, response);
  200. }
  201. void DoCacheInsert (HttpContext context, HttpApplication app, HttpResponse response)
  202. {
  203. string vary_key = context.Request.FilePath;
  204. string key;
  205. OutputCacheProvider provider = FindCacheProvider (app);
  206. CachedVaryBy varyby = provider.Get (vary_key) as CachedVaryBy;
  207. CachedRawResponse prev = null;
  208. bool lookup = true;
  209. string cacheKey = null, cacheValue = null;
  210. HttpCachePolicy cachePolicy = response.Cache;
  211. if (varyby == null) {
  212. varyby = new CachedVaryBy (cachePolicy, vary_key);
  213. provider.Add (vary_key, varyby, Cache.NoAbsoluteExpiration);
  214. lookup = false;
  215. cacheKey = vary_key;
  216. }
  217. key = varyby.CreateKey (vary_key, context);
  218. if (lookup)
  219. prev = provider.Get (key) as CachedRawResponse;
  220. if (prev == null) {
  221. CachedRawResponse c = response.GetCachedResponse ();
  222. if (c != null) {
  223. string [] keys = new string [] { vary_key };
  224. DateTime utcExpiry, absoluteExpiration;
  225. TimeSpan slidingExpiration;
  226. c.VaryBy = varyby;
  227. varyby.ItemList.Add (key);
  228. if (cachePolicy.Sliding) {
  229. slidingExpiration = TimeSpan.FromSeconds (cachePolicy.Duration);
  230. absoluteExpiration = Cache.NoAbsoluteExpiration;
  231. utcExpiry = DateTime.UtcNow + slidingExpiration;
  232. } else {
  233. slidingExpiration = Cache.NoSlidingExpiration;
  234. absoluteExpiration = cachePolicy.Expires;
  235. utcExpiry = absoluteExpiration.ToUniversalTime ();
  236. }
  237. provider.Set (key, c, utcExpiry);
  238. HttpRuntime.InternalCache.Insert (key, c, new CacheDependency (null, keys), absoluteExpiration, slidingExpiration,
  239. CacheItemPriority.Normal, response_removed);
  240. cacheValue = key;
  241. }
  242. }
  243. if (cacheKey != null) {
  244. lock (keysCacheLock) {
  245. if (keysCache == null) {
  246. BuildManager.RemoveEntry += new BuildManagerRemoveEntryEventHandler (OnBuildManagerRemoveEntry);
  247. keysCache = new Dictionary <string, string> (StringComparer.Ordinal);
  248. keysCache.Add (cacheKey, cacheValue);
  249. } else if (!keysCache.ContainsKey (cacheKey))
  250. keysCache.Add (cacheKey, cacheValue);
  251. }
  252. }
  253. }
  254. void OnRawResponseRemoved (string key, object value, CacheItemRemovedReason reason)
  255. {
  256. CachedRawResponse c = value as CachedRawResponse;
  257. CachedVaryBy varyby = c != null ? c.VaryBy : null;
  258. if (varyby == null)
  259. return;
  260. List <string> itemList = varyby.ItemList;
  261. OutputCacheProvider provider = FindCacheProvider (null);
  262. itemList.Remove (key);
  263. provider.Remove (key);
  264. if (itemList.Count != 0)
  265. return;
  266. provider.Remove (varyby.Key);
  267. }
  268. }
  269. }