System.Net.Cache.Rfc2616.Common.ValidateCacheByClientPolicy C# (CSharp) Method

ValidateCacheByClientPolicy() public static method

public static ValidateCacheByClientPolicy ( HttpRequestCacheValidator ctx ) : bool
ctx HttpRequestCacheValidator
return bool
            public static bool ValidateCacheByClientPolicy(HttpRequestCacheValidator ctx) {

                if (ctx.Policy.Level == HttpRequestCacheLevel.Default)
                {
                    if(Logging.On)Logging.PrintInfo(Logging.RequestCache, SR.GetString(SR.net_log_cache_age, (ctx.CacheAge != TimeSpan.MinValue ? ((int)ctx.CacheAge.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo) : SR.GetString(SR.net_log_unknown)), (ctx.CacheMaxAge != TimeSpan.MinValue? ((int)ctx.CacheMaxAge.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo): SR.GetString(SR.net_log_unknown))));

                    if (ctx.Policy.MinFresh > TimeSpan.Zero)
                    {
                        if(Logging.On)Logging.PrintInfo(Logging.RequestCache, SR.GetString(SR.net_log_cache_policy_min_fresh, ((int)ctx.Policy.MinFresh.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo)));
                        if (ctx.CacheAge + ctx.Policy.MinFresh >= ctx.CacheMaxAge) {return false;}
                    }

                    if (ctx.Policy.MaxAge != TimeSpan.MaxValue)
                    {
                        if(Logging.On)Logging.PrintInfo(Logging.RequestCache, SR.GetString(SR.net_log_cache_policy_max_age, ((int)ctx.Policy.MaxAge.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo)));
                        if (ctx.CacheAge >= ctx.Policy.MaxAge) {return false;}
                    }

                    if (ctx.Policy.InternalCacheSyncDateUtc != DateTime.MinValue)
                    {
                        if(Logging.On)Logging.PrintInfo(Logging.RequestCache, SR.GetString(SR.net_log_cache_policy_cache_sync_date, ctx.Policy.InternalCacheSyncDateUtc.ToString("r", CultureInfo.CurrentCulture), ctx.CacheEntry.LastSynchronizedUtc.ToString(CultureInfo.CurrentCulture)));
                        if (ctx.CacheEntry.LastSynchronizedUtc < ctx.Policy.InternalCacheSyncDateUtc) {
                            return false;
                        }
                    }

                    TimeSpan adjustedMaxAge = ctx.CacheMaxAge;
                    if (ctx.Policy.MaxStale > TimeSpan.Zero)
                    {
                        if(Logging.On)Logging.PrintInfo(Logging.RequestCache, SR.GetString(SR.net_log_cache_policy_max_stale, ((int)ctx.Policy.MaxStale.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo)));
                        if (adjustedMaxAge < TimeSpan.MaxValue - ctx.Policy.MaxStale)
                        {
                            adjustedMaxAge = adjustedMaxAge + ctx.Policy.MaxStale;
                        }
                        else
                        {
                            adjustedMaxAge = TimeSpan.MaxValue;
                        }

                        if (ctx.CacheAge >= adjustedMaxAge)
                            return false;
                        else
                            return true;
                    }

                }
                // not stale means "fresh enough"
                return ctx.CacheFreshnessStatus == CacheFreshnessStatus.Fresh;
            }