2
0

OutputCacheModule.cs 9.7 KB

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