OutputCache.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // Authors:
  3. // Marek Habersack <[email protected]>
  4. //
  5. // (C) 2010 Novell, Inc (http://novell.com/)
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System.Configuration;
  28. using System.Configuration.Provider;
  29. using System.IO;
  30. using System.Runtime.Serialization.Formatters.Binary;
  31. using System.Security.Permissions;
  32. using System.Web;
  33. using System.Web.Configuration;
  34. namespace System.Web.Caching
  35. {
  36. public static class OutputCache
  37. {
  38. internal const string DEFAULT_PROVIDER_NAME = "AspNetInternalProvider";
  39. static readonly object initLock = new object ();
  40. static readonly object defaultProviderInitLock = new object();
  41. static bool initialized;
  42. static string defaultProviderName;
  43. static OutputCacheProviderCollection providers;
  44. static OutputCacheProvider defaultProvider;
  45. public static string DefaultProviderName {
  46. get {
  47. Init ();
  48. if (String.IsNullOrEmpty (defaultProviderName))
  49. return DEFAULT_PROVIDER_NAME;
  50. return defaultProviderName;
  51. }
  52. }
  53. internal static OutputCacheProvider DefaultProvider {
  54. get {
  55. if (defaultProvider == null) {
  56. string providerName = DefaultProviderName;
  57. lock (defaultProviderInitLock) {
  58. if (defaultProvider == null)
  59. defaultProvider = new InMemoryOutputCacheProvider ();
  60. }
  61. }
  62. return defaultProvider;
  63. }
  64. }
  65. public static OutputCacheProviderCollection Providers {
  66. get {
  67. Init ();
  68. return providers;
  69. }
  70. }
  71. [SecurityPermission (SecurityAction.Assert, Flags = SecurityPermissionFlag.SerializationFormatter)]
  72. public static object Deserialize (Stream stream)
  73. {
  74. if (stream == null)
  75. throw new ArgumentNullException ("stream");
  76. object o = new BinaryFormatter ().Deserialize (stream);
  77. if (o == null || IsInvalidType (o))
  78. throw new ArgumentException ("The provided parameter is not of a supported type for serialization and/or deserialization.");
  79. return o;
  80. }
  81. [SecurityPermission (SecurityAction.Assert, Flags = SecurityPermissionFlag.SerializationFormatter)]
  82. public static void Serialize (Stream stream, object data)
  83. {
  84. if (stream == null)
  85. throw new ArgumentNullException ("stream");
  86. // LAMESPEC: data == null doesn't throw ArgumentNullException
  87. if (data == null || IsInvalidType (data))
  88. throw new ArgumentException ("The provided parameter is not of a supported type for serialization and/or deserialization.");
  89. new BinaryFormatter ().Serialize (stream, data);
  90. }
  91. internal static OutputCacheProvider GetProvider (string providerName)
  92. {
  93. if (String.IsNullOrEmpty (providerName))
  94. return null;
  95. if (String.Compare (providerName, DEFAULT_PROVIDER_NAME, StringComparison.Ordinal) == 0)
  96. return DefaultProvider;
  97. OutputCacheProviderCollection providers = OutputCache.Providers;
  98. return (providers != null ? providers [providerName] : null);
  99. }
  100. static bool IsInvalidType (object data)
  101. {
  102. return !(data is MemoryResponseElement) &&
  103. !(data is FileResponseElement) &&
  104. !(data is SubstitutionResponseElement);
  105. }
  106. static void Init ()
  107. {
  108. if (initialized)
  109. return;
  110. lock (initLock) {
  111. if (initialized)
  112. return;
  113. var cfg = WebConfigurationManager.GetWebApplicationSection ("system.web/caching/outputCache") as OutputCacheSection;
  114. ProviderSettingsCollection cfgProviders = cfg.Providers;
  115. defaultProviderName = cfg.DefaultProviderName;
  116. if (cfgProviders != null && cfgProviders.Count > 0) {
  117. var coll = new OutputCacheProviderCollection ();
  118. foreach (ProviderSettings ps in cfgProviders)
  119. coll.Add (LoadProvider (ps));
  120. coll.SetReadOnly ();
  121. providers = coll;
  122. }
  123. initialized = true;
  124. }
  125. }
  126. static OutputCacheProvider LoadProvider (ProviderSettings ps)
  127. {
  128. Type type = HttpApplication.LoadType (ps.Type, false);
  129. if (type == null)
  130. throw new ConfigurationErrorsException (String.Format ("Could not load type '{0}'.", ps.Type));
  131. var ret = Activator.CreateInstance (type) as OutputCacheProvider;
  132. ret.Initialize (ps.Name, ps.Parameters);
  133. return ret;
  134. }
  135. internal static void RemoveFromProvider (string key, string providerName)
  136. {
  137. if (providerName == null)
  138. return;
  139. OutputCacheProviderCollection providers = Providers;
  140. OutputCacheProvider provider;
  141. if (providers == null || providers.Count == 0)
  142. provider = null;
  143. else
  144. provider = providers [providerName];
  145. if (provider == null)
  146. throw new ProviderException ("Provider '" + providerName + "' was not found.");
  147. provider.Remove (key);
  148. }
  149. }
  150. }