OutputCache.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. lock (defaultProviderInitLock) {
  57. if (defaultProvider == null)
  58. defaultProvider = new InMemoryOutputCacheProvider ();
  59. }
  60. }
  61. return defaultProvider;
  62. }
  63. }
  64. public static OutputCacheProviderCollection Providers {
  65. get {
  66. Init ();
  67. return providers;
  68. }
  69. }
  70. [SecurityPermission (SecurityAction.Assert, Flags = SecurityPermissionFlag.SerializationFormatter)]
  71. public static object Deserialize (Stream stream)
  72. {
  73. if (stream == null)
  74. throw new ArgumentNullException ("stream");
  75. object o = new BinaryFormatter ().Deserialize (stream);
  76. if (o == null || IsInvalidType (o))
  77. throw new ArgumentException ("The provided parameter is not of a supported type for serialization and/or deserialization.");
  78. return o;
  79. }
  80. [SecurityPermission (SecurityAction.Assert, Flags = SecurityPermissionFlag.SerializationFormatter)]
  81. public static void Serialize (Stream stream, object data)
  82. {
  83. if (stream == null)
  84. throw new ArgumentNullException ("stream");
  85. // LAMESPEC: data == null doesn't throw ArgumentNullException
  86. if (data == null || IsInvalidType (data))
  87. throw new ArgumentException ("The provided parameter is not of a supported type for serialization and/or deserialization.");
  88. new BinaryFormatter ().Serialize (stream, data);
  89. }
  90. internal static OutputCacheProvider GetProvider (string providerName)
  91. {
  92. if (String.IsNullOrEmpty (providerName))
  93. return null;
  94. if (String.Compare (providerName, DEFAULT_PROVIDER_NAME, StringComparison.Ordinal) == 0)
  95. return DefaultProvider;
  96. OutputCacheProviderCollection providers = OutputCache.Providers;
  97. return (providers != null ? providers [providerName] : null);
  98. }
  99. static bool IsInvalidType (object data)
  100. {
  101. return !(data is MemoryResponseElement) &&
  102. !(data is FileResponseElement) &&
  103. !(data is SubstitutionResponseElement);
  104. }
  105. static void Init ()
  106. {
  107. if (initialized)
  108. return;
  109. lock (initLock) {
  110. if (initialized)
  111. return;
  112. var cfg = WebConfigurationManager.GetWebApplicationSection ("system.web/caching/outputCache") as OutputCacheSection;
  113. ProviderSettingsCollection cfgProviders = cfg.Providers;
  114. defaultProviderName = cfg.DefaultProviderName;
  115. if (cfgProviders != null && cfgProviders.Count > 0) {
  116. var coll = new OutputCacheProviderCollection ();
  117. foreach (ProviderSettings ps in cfgProviders)
  118. coll.Add (LoadProvider (ps));
  119. coll.SetReadOnly ();
  120. providers = coll;
  121. }
  122. initialized = true;
  123. }
  124. }
  125. static OutputCacheProvider LoadProvider (ProviderSettings ps)
  126. {
  127. Type type = HttpApplication.LoadType (ps.Type, false);
  128. if (type == null)
  129. throw new ConfigurationErrorsException (String.Format ("Could not load type '{0}'.", ps.Type));
  130. var ret = Activator.CreateInstance (type) as OutputCacheProvider;
  131. ret.Initialize (ps.Name, ps.Parameters);
  132. return ret;
  133. }
  134. internal static void RemoveFromProvider (string key, string providerName)
  135. {
  136. if (providerName == null)
  137. return;
  138. OutputCacheProviderCollection providers = Providers;
  139. OutputCacheProvider provider;
  140. if (providers == null || providers.Count == 0)
  141. provider = null;
  142. else
  143. provider = providers [providerName];
  144. if (provider == null)
  145. throw new ProviderException ("Provider '" + providerName + "' was not found.");
  146. provider.Remove (key);
  147. }
  148. }
  149. }