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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="RequestCachePolicy.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 caching policy paradigms that is used by all webrequest cache-aware clients
  18. Author:
  19.     Alexei Vopilov    21-Dec-2002
  20. Revision History:
  21.     4  Dec 2003  - Reworked as per design review.
  22.     30 Jul 2004  - Updated to accomodate FTP caching feature
  23. --*/
  24. namespace System.Net.Cache
  25. {
  26.     using System.Globalization;
  27.    
  28.    
  29.     // The enum describes cache settings applicable for any webrequest
  30.     public enum RequestCacheLevel
  31.     {
  32.         // Default cache behavior determined by the protocol class
  33.         Default = 0,
  34.         // Bypass the cache completely
  35.         BypassCache = 1,
  36.         // Only serve requests from cache, an exception is thrown if not found
  37.         CacheOnly = 2,
  38.         // Serve from the cache, but will sync up with the server if not found
  39.         CacheIfAvailable = 3,
  40.         // Attempt to revalidate cache with the server, reload if unable to
  41.         Revalidate = 4,
  42.         // Reload the data from the origin server
  43.         Reload = 5,
  44.         // Bypass the cache and removing existing entries in the cache
  45.         NoCacheNoStore = 6
  46.     }
  47.    
  48.    
  49.     //
  50.     // Common request cache policy used for caching of Http, FTP, etc.
  51.     //
  52.     public class RequestCachePolicy
  53.     {
  54.         private RequestCacheLevel m_Level;
  55.        
  56.         //
  57.         // Public stuff
  58.         //
  59.         public RequestCachePolicy() : this(RequestCacheLevel.Default)
  60.         {
  61.         }
  62.        
  63.         public RequestCachePolicy(RequestCacheLevel level)
  64.         {
  65.             if (level < RequestCacheLevel.Default || level > RequestCacheLevel.NoCacheNoStore)
  66.                 throw new ArgumentOutOfRangeException("level");
  67.            
  68.             m_Level = level;
  69.         }
  70.         //
  71.         public RequestCacheLevel Level {
  72.             get { return m_Level; }
  73.         }
  74.         //
  75.         public override string ToString()
  76.         {
  77.             return "Level:" + m_Level.ToString();
  78.         }
  79.         //
  80.         // Internal stuff
  81.         //
  82.         //
  83.        
  84.     }
  85.    
  86.     // The enum describes cache settings for http
  87.     public enum HttpRequestCacheLevel
  88.     {
  89.         // Default cache behavior, server fresh response fomr cache, otherwise attempt
  90.         // to revalidate with the server or reload
  91.         Default = 0,
  92.         // Bypass the cache completely
  93.         BypassCache = 1,
  94.         // Only serve requests from cache, an exception is thrown if not found
  95.         CacheOnly = 2,
  96.         // Serve from the cache, but will sync up with the server if not found
  97.         CacheIfAvailable = 3,
  98.         // Validate cached data with the server even if it looks fresh
  99.         Revalidate = 4,
  100.         // Reload the data from the origin server
  101.         Reload = 5,
  102.         // Bypass the cache and removing existing entries in the cache
  103.         NoCacheNoStore = 6,
  104.         // Serve from cache, or the next cache along the path
  105.         CacheOrNextCacheOnly = 7,
  106.         // Reload the data either from the origin server or from an uplevel cache
  107.         //This is equvalent to Cache-Control:MaxAge=0 HTTP semantic
  108.         Refresh = 8
  109.     }
  110.     //
  111.     // CacheAgeControl is used to specify preferences with respect of cached item age and freshness.
  112.     //
  113.     public enum HttpCacheAgeControl
  114.     {
  115.         // Invalid value. Indicates the enum is not initialized
  116.         None = 0,
  117.         // Cached item must be at least fresh for specified period since now
  118.         MinFresh = 1,
  119.         // Cached item must be fresh and it's age must not exceed specified period
  120.         MaxAge = 2,
  121.         // Cached item may be not fresh but it's expiration must not exceed specified period
  122.         MaxStale = 4,
  123.         // Cached item must fresh for some period in future and it's age must be less than specified
  124.         MaxAgeAndMinFresh = 3,
  125.         // MaxAge|MinFresh,
  126.         // Cached item may be found as stale for some period but it's age must be less than specified
  127.         MaxAgeAndMaxStale = 6
  128.         // MaxAge|MaxStale,
  129.     }
  130.    
  131.     //
  132.     // HTTP cache policy that expresses RFC2616 HTTP caching semantic
  133.     //
  134.     public class HttpRequestCachePolicy : RequestCachePolicy
  135.     {
  136.        
  137.         static internal readonly HttpRequestCachePolicy BypassCache = new HttpRequestCachePolicy(HttpRequestCacheLevel.BypassCache);
  138.        
  139.         //Private members
  140.         private HttpRequestCacheLevel m_Level = HttpRequestCacheLevel.Default;
  141.         private DateTime m_LastSyncDateUtc = DateTime.MinValue;
  142.         private TimeSpan m_MaxAge = TimeSpan.MaxValue;
  143.         private TimeSpan m_MinFresh = TimeSpan.MinValue;
  144.         private TimeSpan m_MaxStale = TimeSpan.MinValue;
  145.        
  146.         //
  147.         // Public stuff
  148.         //
  149.         public HttpRequestCachePolicy() : this(HttpRequestCacheLevel.Default)
  150.         {
  151.         }
  152.         //
  153.         public HttpRequestCachePolicy(HttpRequestCacheLevel level) : base(MapLevel(level))
  154.         {
  155.             m_Level = level;
  156.         }
  157.         //
  158.         // Creates an automatic cache policy that is bound to a simples age control
  159.         //
  160.         public HttpRequestCachePolicy(HttpCacheAgeControl cacheAgeControl, TimeSpan ageOrFreshOrStale) : this(HttpRequestCacheLevel.Default)
  161.         {
  162.            
  163.             switch (cacheAgeControl) {
  164.                 case HttpCacheAgeControl.MinFresh:
  165.                     m_MinFresh = ageOrFreshOrStale;
  166.                     break;
  167.                 case HttpCacheAgeControl.MaxAge:
  168.                     m_MaxAge = ageOrFreshOrStale;
  169.                     break;
  170.                 case HttpCacheAgeControl.MaxStale:
  171.                     m_MaxStale = ageOrFreshOrStale;
  172.                     break;
  173.                 default:
  174.                     throw new ArgumentException(SR.GetString(SR.net_invalid_enum, "HttpCacheAgeControl"), "cacheAgeControl");
  175.                     break;
  176.             }
  177.         }
  178.         //
  179.         // Creates an automatic cache policy that is bound to a complex age control
  180.         //
  181.         public HttpRequestCachePolicy(HttpCacheAgeControl cacheAgeControl, TimeSpan maxAge, TimeSpan freshOrStale) : this(HttpRequestCacheLevel.Default)
  182.         {
  183.            
  184.             switch (cacheAgeControl) {
  185.                 case HttpCacheAgeControl.MinFresh:
  186.                     m_MinFresh = freshOrStale;
  187.                     break;
  188.                 case HttpCacheAgeControl.MaxAge:
  189.                     m_MaxAge = maxAge;
  190.                     break;
  191.                 case HttpCacheAgeControl.MaxStale:
  192.                     m_MaxStale = freshOrStale;
  193.                     break;
  194.                 case HttpCacheAgeControl.MaxAgeAndMinFresh:
  195.                     m_MaxAge = maxAge;
  196.                     m_MinFresh = freshOrStale;
  197.                     break;
  198.                 case HttpCacheAgeControl.MaxAgeAndMaxStale:
  199.                     m_MaxAge = maxAge;
  200.                     m_MaxStale = freshOrStale;
  201.                     break;
  202.                 default:
  203.                     throw new ArgumentException(SR.GetString(SR.net_invalid_enum, "HttpCacheAgeControl"), "cacheAgeControl");
  204.                     break;
  205.             }
  206.         }
  207.         //
  208.         // Creates an automatic cache policy with the Date Synchronization requirement
  209.         //
  210.         public HttpRequestCachePolicy(DateTime cacheSyncDate) : this(HttpRequestCacheLevel.Default)
  211.         {
  212.             m_LastSyncDateUtc = cacheSyncDate.ToUniversalTime();
  213.         }
  214.         //
  215.         //
  216.         //
  217.         public HttpRequestCachePolicy(HttpCacheAgeControl cacheAgeControl, TimeSpan maxAge, TimeSpan freshOrStale, DateTime cacheSyncDate) : this(cacheAgeControl, maxAge, freshOrStale)
  218.         {
  219.             m_LastSyncDateUtc = cacheSyncDate.ToUniversalTime();
  220.         }
  221.         //
  222.         // Properties
  223.         //
  224.         public new HttpRequestCacheLevel Level {
  225.             get { return m_Level; }
  226.         }
  227.         //
  228.         // Requires revalidation of items stored before lastSyncDate
  229.         //
  230.         public DateTime CacheSyncDate {
  231.             get {
  232.                 if (m_LastSyncDateUtc == DateTime.MinValue || m_LastSyncDateUtc == DateTime.MaxValue) {
  233.                     return m_LastSyncDateUtc;
  234.                 }
  235.                 return m_LastSyncDateUtc.ToLocalTime();
  236.             }
  237.         }
  238.         //
  239.         internal DateTime InternalCacheSyncDateUtc {
  240.             get { return m_LastSyncDateUtc; }
  241.         }
  242.         //
  243.         // Specifies age policy according to HTTP 1.1 RFC caching semantic
  244.         //
  245.         public TimeSpan MaxAge {
  246.             get { return m_MaxAge; }
  247.         }
  248.         //
  249.         // Specifies age policy according to HTTP 1.1 RFC caching semantic
  250.         //
  251.         public TimeSpan MinFresh {
  252.             get { return m_MinFresh; }
  253.         }
  254.         //
  255.         // Specifies age policy according to HTTP 1.1 RFC caching semantic
  256.         //
  257.         public TimeSpan MaxStale {
  258.             get { return m_MaxStale; }
  259.         }
  260.         //
  261.         //
  262.         //
  263.         public override string ToString()
  264.         {
  265.             return "Level:" + m_Level.ToString() + (m_MaxAge == TimeSpan.MaxValue ? string.Empty : " MaxAge:" + m_MaxAge.ToString()) + (m_MinFresh == TimeSpan.MinValue ? string.Empty : " MinFresh:" + m_MinFresh.ToString()) + (m_MaxStale == TimeSpan.MinValue ? string.Empty : " MaxStale:" + m_MaxStale.ToString()) + (CacheSyncDate == DateTime.MinValue ? string.Empty : " CacheSyncDate:" + CacheSyncDate.ToString(CultureInfo.CurrentCulture));
  266.         }
  267.         //
  268.         //
  269.         //
  270.         private static RequestCacheLevel MapLevel(HttpRequestCacheLevel level)
  271.         {
  272.            
  273.             if (level <= HttpRequestCacheLevel.NoCacheNoStore)
  274.                 return (RequestCacheLevel)level;
  275.            
  276.             if (level == HttpRequestCacheLevel.CacheOrNextCacheOnly)
  277.                 return RequestCacheLevel.CacheOnly;
  278.            
  279.             if (level == HttpRequestCacheLevel.Refresh)
  280.                 return RequestCacheLevel.Reload;
  281.            
  282.             throw new ArgumentOutOfRangeException("level");
  283.         }
  284.     }
  285. }

Developer Fusion