Пример #1
0
        /// <summary>
        /// Works out how long to cache based on a default period and any expiry dates, relative to a start date.
        /// </summary>
        /// <param name="relativeToDate">The start date cache time spans are relative to, usually <see cref="DateTime.UtcNow"/>.</param>
        /// <param name="defaultCachePeriod">The default cache period.</param>
        /// <param name="contentExpiryDates">Expiry dates for any content, either part of a page or the whole page.</param>
        /// <returns>An absolute time and relative timespan representing how long to cache the content for.</returns>
        public CacheFreshness WorkOutCacheFreshness(DateTime relativeToDate, TimeSpan defaultCachePeriod, IList <DateTime> contentExpiryDates)
        {
            if (contentExpiryDates == null)
            {
                throw new ArgumentNullException("contentExpiryDates");
            }

            // Convert date to UTC so that it can be compared on an equal basis
            relativeToDate = relativeToDate.ToUniversalTime();

            // How long is this page fresh for?
            var freshness = new CacheFreshness()
            {
                FreshFor   = defaultCachePeriod,
                FreshUntil = relativeToDate.Add(defaultCachePeriod)
            };

            for (var i = 0; i < contentExpiryDates.Count; i++)
            {
                // Convert date to UTC so that it can be compared on an equal basis
                contentExpiryDates[i] = contentExpiryDates[i].ToUniversalTime();

                // Check if some or all of the content expires sooner than the default cache period?
                OverrideDueToContentExpiry(freshness, relativeToDate, contentExpiryDates[i]);
            }

            return(freshness);
        }
Пример #2
0
 private static void OverrideDueToContentExpiry(CacheFreshness freshness, DateTime relativeToDate, DateTime expiryDate)
 {
     if (expiryDate > relativeToDate && expiryDate < freshness.FreshUntil)
     {
         freshness.FreshFor   = expiryDate.Subtract(relativeToDate);
         freshness.FreshUntil = relativeToDate.Add(freshness.FreshFor);
     }
 }