OutputCacheModule.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. OutputCacheProvider provider;
  46. CacheItemRemovedCallback response_removed;
  47. static object keysCacheLock = new object ();
  48. Dictionary <string, string> keysCache;
  49. Dictionary <string, string> entriesToInvalidate;
  50. public OutputCacheModule ()
  51. {
  52. }
  53. OutputCacheProvider FindCacheProvider (HttpApplication app)
  54. {
  55. #if NET_4_0
  56. HttpContext ctx = HttpContext.Current;
  57. if (app == null) {
  58. app = ctx != null ? ctx.ApplicationInstance : null;
  59. if (app == null)
  60. throw new InvalidOperationException ("Unable to find output cache provider.");
  61. }
  62. string providerName = app.GetOutputCacheProviderName (ctx);
  63. if (String.IsNullOrEmpty (providerName))
  64. throw new ProviderException ("Invalid OutputCacheProvider name. Name must not be null or an empty string.");
  65. if (String.Compare (providerName, OutputCache.DEFAULT_PROVIDER_NAME, StringComparison.Ordinal) == 0) {
  66. if (provider == null)
  67. provider = new InMemoryOutputCacheProvider ();
  68. return provider;
  69. }
  70. OutputCacheProviderCollection providers = OutputCache.Providers;
  71. OutputCacheProvider ret = providers != null ? providers [providerName] : null;
  72. if (ret == null)
  73. throw new ProviderException (String.Format ("OutputCacheProvider named '{0}' cannot be found.", providerName));
  74. return ret;
  75. #else
  76. if (provider == null)
  77. provider = new InMemoryOutputCacheProvider ();
  78. return provider;
  79. #endif
  80. }
  81. public void Dispose ()
  82. {
  83. }
  84. public void Init (HttpApplication context)
  85. {
  86. context.ResolveRequestCache += new EventHandler(OnResolveRequestCache);
  87. context.UpdateRequestCache += new EventHandler(OnUpdateRequestCache);
  88. response_removed = new CacheItemRemovedCallback (OnRawResponseRemoved);
  89. }
  90. void OnBuildManagerRemoveEntry (BuildManagerRemoveEntryEventArgs args)
  91. {
  92. string entry = args.EntryName;
  93. HttpContext context = args.Context;
  94. string cacheValue;
  95. lock (keysCacheLock) {
  96. if (!keysCache.TryGetValue (entry, out cacheValue))
  97. return;
  98. keysCache.Remove (entry);
  99. if (context == null) {
  100. if (entriesToInvalidate == null) {
  101. entriesToInvalidate = new Dictionary <string, string> (StringComparer.Ordinal);
  102. entriesToInvalidate.Add (entry, cacheValue);
  103. return;
  104. } else if (!entriesToInvalidate.ContainsKey (entry)) {
  105. entriesToInvalidate.Add (entry, cacheValue);
  106. return;
  107. }
  108. }
  109. }
  110. OutputCacheProvider provider = FindCacheProvider (context != null ? context.ApplicationInstance : null);
  111. provider.Remove (entry);
  112. if (!String.IsNullOrEmpty (cacheValue))
  113. provider.Remove (cacheValue);
  114. }
  115. void OnResolveRequestCache (object o, EventArgs args)
  116. {
  117. HttpApplication app = o as HttpApplication;
  118. HttpContext context = app != null ? app.Context : null;
  119. if (context == null)
  120. return;
  121. OutputCacheProvider provider = FindCacheProvider (app);
  122. string vary_key = context.Request.FilePath;
  123. CachedVaryBy varyby = provider.Get (vary_key) as CachedVaryBy;
  124. string key;
  125. CachedRawResponse c;
  126. if (varyby == null)
  127. return;
  128. key = varyby.CreateKey (vary_key, context);
  129. c = provider.Get (key) as CachedRawResponse;
  130. if (c == null)
  131. return;
  132. lock (keysCacheLock) {
  133. string invValue;
  134. if (entriesToInvalidate != null && entriesToInvalidate.TryGetValue (vary_key, out invValue) && String.Compare (invValue, key, StringComparison.Ordinal) == 0) {
  135. provider.Remove (vary_key);
  136. provider.Remove (key);
  137. entriesToInvalidate.Remove (vary_key);
  138. return;
  139. }
  140. }
  141. ArrayList callbacks = c.Policy.ValidationCallbacks;
  142. if (callbacks != null && callbacks.Count > 0) {
  143. bool isValid = true;
  144. bool isIgnored = false;
  145. foreach (Pair p in callbacks) {
  146. HttpCacheValidateHandler validate = (HttpCacheValidateHandler)p.First;
  147. object data = p.Second;
  148. HttpValidationStatus status = HttpValidationStatus.Valid;
  149. try {
  150. validate (context, data, ref status);
  151. } catch {
  152. // MS.NET hides the exception
  153. isValid = false;
  154. break;
  155. }
  156. if (status == HttpValidationStatus.Invalid) {
  157. isValid = false;
  158. break;
  159. } else if (status == HttpValidationStatus.IgnoreThisRequest) {
  160. isIgnored = true;
  161. }
  162. }
  163. if (!isValid) {
  164. OnRawResponseRemoved (key, c, CacheItemRemovedReason.Removed);
  165. return;
  166. } else if (isIgnored)
  167. return;
  168. }
  169. HttpResponse response = context.Response;
  170. response.ClearContent ();
  171. IList cachedData = c.GetData ();
  172. if (cachedData != null) {
  173. Encoding outEnc = WebEncoding.ResponseEncoding;
  174. foreach (CachedRawResponse.DataItem d in cachedData) {
  175. if (d.Length > 0) {
  176. response.BinaryWrite (d.Buffer, 0, (int)d.Length);
  177. continue;
  178. }
  179. if (d.Callback == null)
  180. continue;
  181. string s = d.Callback (context);
  182. if (s == null || s.Length == 0)
  183. continue;
  184. byte[] bytes = outEnc.GetBytes (s);
  185. response.BinaryWrite (bytes, 0, bytes.Length);
  186. }
  187. }
  188. response.ClearHeaders ();
  189. response.SetCachedHeaders (c.Headers);
  190. response.StatusCode = c.StatusCode;
  191. response.StatusDescription = c.StatusDescription;
  192. app.CompleteRequest ();
  193. }
  194. void OnUpdateRequestCache (object o, EventArgs args)
  195. {
  196. HttpApplication app = o as HttpApplication;
  197. HttpContext context = app != null ? app.Context : null;
  198. HttpResponse response = context != null ? context.Response : null;
  199. if (response != null && response.IsCached && response.StatusCode == 200 && !context.Trace.IsEnabled)
  200. DoCacheInsert (context, app, response);
  201. }
  202. void DoCacheInsert (HttpContext context, HttpApplication app, HttpResponse response)
  203. {
  204. string vary_key = context.Request.FilePath;
  205. string key;
  206. OutputCacheProvider provider = FindCacheProvider (app);
  207. CachedVaryBy varyby = provider.Get (vary_key) as CachedVaryBy;
  208. CachedRawResponse prev = null;
  209. bool lookup = true;
  210. string cacheKey = null, cacheValue = null;
  211. HttpCachePolicy cachePolicy = response.Cache;
  212. if (varyby == null) {
  213. varyby = new CachedVaryBy (cachePolicy, vary_key);
  214. provider.Add (vary_key, varyby, Cache.NoAbsoluteExpiration);
  215. lookup = false;
  216. cacheKey = vary_key;
  217. }
  218. key = varyby.CreateKey (vary_key, context);
  219. if (lookup)
  220. prev = provider.Get (key) as CachedRawResponse;
  221. if (prev == null) {
  222. CachedRawResponse c = response.GetCachedResponse ();
  223. if (c != null) {
  224. string [] keys = new string [] { vary_key };
  225. DateTime utcExpiry, absoluteExpiration;
  226. TimeSpan slidingExpiration;
  227. c.VaryBy = varyby;
  228. varyby.ItemList.Add (key);
  229. if (cachePolicy.Sliding) {
  230. slidingExpiration = TimeSpan.FromSeconds (cachePolicy.Duration);
  231. absoluteExpiration = Cache.NoAbsoluteExpiration;
  232. utcExpiry = DateTime.UtcNow + slidingExpiration;
  233. } else {
  234. slidingExpiration = Cache.NoSlidingExpiration;
  235. absoluteExpiration = cachePolicy.Expires;
  236. utcExpiry = absoluteExpiration.ToUniversalTime ();
  237. }
  238. provider.Set (key, c, utcExpiry);
  239. HttpRuntime.InternalCache.Insert (key, c, new CacheDependency (null, keys), absoluteExpiration, slidingExpiration,
  240. CacheItemPriority.Normal, response_removed);
  241. cacheValue = key;
  242. }
  243. }
  244. if (cacheKey != null) {
  245. lock (keysCacheLock) {
  246. if (keysCache == null) {
  247. BuildManager.RemoveEntry += new BuildManagerRemoveEntryEventHandler (OnBuildManagerRemoveEntry);
  248. keysCache = new Dictionary <string, string> (StringComparer.Ordinal);
  249. keysCache.Add (cacheKey, cacheValue);
  250. } else if (!keysCache.ContainsKey (cacheKey))
  251. keysCache.Add (cacheKey, cacheValue);
  252. }
  253. }
  254. }
  255. void OnRawResponseRemoved (string key, object value, CacheItemRemovedReason reason)
  256. {
  257. CachedRawResponse c = value as CachedRawResponse;
  258. CachedVaryBy varyby = c != null ? c.VaryBy : null;
  259. if (varyby == null)
  260. return;
  261. List <string> itemList = varyby.ItemList;
  262. OutputCacheProvider provider = FindCacheProvider (null);
  263. itemList.Remove (key);
  264. provider.Remove (key);
  265. if (itemList.Count != 0)
  266. return;
  267. provider.Remove (varyby.Key);
  268. }
  269. }
  270. }