The Labs \ Source Viewer \ SSCLI \ System.Net.Cache \ RequestCacheBinding

  1. //------------------------------------------------------------------------------
  2. // <copyright file="RequestCacheManager.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. /*++   
  16. Abstract:
  17.     The class implements the app domain wide configuration
  18.     for request cache aware clients
  19. Author:
  20.     Alexei Vopilov    21-Dec-2002
  21. Revision History:
  22.     Jan 25 2004 - Changed the visibility of the class from public to internal, compressed unused logic.
  23. --*/
  24. namespace System.Net.Cache
  25. {
  26.     using System;
  27.     using System.Collections;
  28.     using System.Net.Configuration;
  29.     using System.Configuration;
  30.    
  31.    
  32.     /// <summary> Specifies app domain-wide default settings for request caching </summary>
  33.     internal sealed class RequestCacheManager
  34.     {
  35.         private RequestCacheManager()
  36.         {
  37.         }
  38.        
  39.         private static RequestCachingSectionInternal s_CacheConfigSettings;
  40.        
  41.         private static readonly RequestCacheBinding s_BypassCacheBinding = new RequestCacheBinding(null, null, new RequestCachePolicy(RequestCacheLevel.BypassCache));
  42.        
  43.         private static RequestCacheBinding s_DefaultGlobalBinding;
  44.         private static RequestCacheBinding s_DefaultHttpBinding;
  45.         private static RequestCacheBinding s_DefaultFtpBinding;
  46.        
  47.         //
  48.         // ATN: the caller MUST use uri.Scheme as a parameter, otheriwse cannot do schme ref comparision
  49.         //
  50.         static internal RequestCacheBinding GetBinding(string internedScheme)
  51.         {
  52.             if (internedScheme == null)
  53.                 throw new ArgumentNullException("uriScheme");
  54.            
  55.             if (s_CacheConfigSettings == null)
  56.                 LoadConfigSettings();
  57.            
  58.             if (s_CacheConfigSettings.DisableAllCaching)
  59.                 return s_BypassCacheBinding;
  60.            
  61.             if (internedScheme.Length == 0)
  62.                 return s_DefaultGlobalBinding;
  63.            
  64.             if ((object)internedScheme == (object)Uri.UriSchemeHttp || (object)internedScheme == (object)Uri.UriSchemeHttps)
  65.                 return s_DefaultHttpBinding;
  66.            
  67.             if ((object)internedScheme == (object)Uri.UriSchemeFtp)
  68.                 return s_DefaultFtpBinding;
  69.            
  70.             return s_BypassCacheBinding;
  71.         }
  72.        
  73.         static internal bool IsCachingEnabled {
  74.             get {
  75.                 if (s_CacheConfigSettings == null)
  76.                     LoadConfigSettings();
  77.                
  78.                 return !s_CacheConfigSettings.DisableAllCaching;
  79.             }
  80.         }
  81.        
  82.         //
  83.         static internal void SetBinding(string uriScheme, RequestCacheBinding binding)
  84.         {
  85.             if (uriScheme == null)
  86.                 throw new ArgumentNullException("uriScheme");
  87.            
  88.             if (s_CacheConfigSettings == null)
  89.                 LoadConfigSettings();
  90.            
  91.             if (s_CacheConfigSettings.DisableAllCaching)
  92.                 return;
  93.            
  94.             if (uriScheme.Length == 0)
  95.                 s_DefaultGlobalBinding = binding;
  96.             else if (uriScheme == Uri.UriSchemeHttp || uriScheme == Uri.UriSchemeHttps)
  97.                 s_DefaultHttpBinding = binding;
  98.             else if (uriScheme == Uri.UriSchemeFtp)
  99.                 s_DefaultFtpBinding = binding;
  100.         }
  101.         //
  102.         private static void LoadConfigSettings()
  103.         {
  104.             // Any concurent access shall go here and block until we've grabbed the config settings
  105.             lock (s_BypassCacheBinding) {
  106.                 if (s_CacheConfigSettings == null) {
  107.                     RequestCachingSectionInternal settings = RequestCachingSectionInternal.GetSection();
  108.                    
  109.                     s_DefaultGlobalBinding = new RequestCacheBinding(settings.DefaultCache, settings.DefaultHttpValidator, settings.DefaultCachePolicy);
  110.                     s_DefaultHttpBinding = new RequestCacheBinding(settings.DefaultCache, settings.DefaultHttpValidator, settings.DefaultHttpCachePolicy);
  111.                     s_DefaultFtpBinding = new RequestCacheBinding(settings.DefaultCache, settings.DefaultFtpValidator, settings.DefaultFtpCachePolicy);
  112.                    
  113.                     s_CacheConfigSettings = settings;
  114.                 }
  115.             }
  116.         }
  117.     }
  118.    
  119.     //
  120.     //
  121.     internal class RequestCacheBinding
  122.     {
  123.         private RequestCache m_RequestCache;
  124.         private RequestCacheValidator m_CacheValidator;
  125.         private RequestCachePolicy m_Policy;
  126.        
  127.        
  128.         internal RequestCacheBinding(RequestCache requestCache, RequestCacheValidator cacheValidator, RequestCachePolicy policy)
  129.         {
  130.             m_RequestCache = requestCache;
  131.             m_CacheValidator = cacheValidator;
  132.             m_Policy = policy;
  133.         }
  134.        
  135.         internal RequestCache Cache {
  136.             get { return m_RequestCache; }
  137.         }
  138.        
  139.         internal RequestCacheValidator Validator {
  140.             get { return m_CacheValidator; }
  141.         }
  142.        
  143.         internal RequestCachePolicy Policy {
  144.             get { return m_Policy; }
  145.         }
  146.        
  147.     }
  148. }

Developer Fusion