Beispiel #1
0
        private void SetupHttpCaching(RenderModel model, LocationViewModel viewModel, IExpiryDateSource expiryDate)
        {
            var       cacheService          = new HttpCachingService();
            var       cacheExpiryProperties = new IExpiryDateSource[] { expiryDate, new ExpiryDateFromPropertyValue(model.Content, "latestUnpublishDate_Latest") };
            var       ukNow   = DateTime.Now.ToUkDateTime();
            const int oneHour = 3600;

            // Ensure we don't cache the page longer than the opening times data passed to the view remains valid.
            // In the last hour before closing there's a countdown, so it's only correct for one minute.
            // If there's no opening times data, just fall back to the default cache expiry settings.
            if (viewModel.OpenUntil.HasValue)
            {
                var secondsRemaining = Convert.ToInt32((viewModel.OpenUntil - ukNow).Value.TotalSeconds);
                if (secondsRemaining <= oneHour)
                {
                    secondsRemaining = 60;
                }
                cacheService.SetHttpCacheHeadersFromUmbracoContent(model.Content, UmbracoContext.Current.InPreviewMode, Response.Cache, cacheExpiryProperties, secondsRemaining);
            }
            else if (viewModel.NextOpen.HasValue)
            {
                var secondsRemaining = Convert.ToInt32((viewModel.NextOpen - ukNow).Value.TotalSeconds);
                if (secondsRemaining <= oneHour)
                {
                    secondsRemaining = 60;
                }
                cacheService.SetHttpCacheHeadersFromUmbracoContent(model.Content, UmbracoContext.Current.InPreviewMode, Response.Cache, cacheExpiryProperties, secondsRemaining);
            }
            else
            {
                cacheService.SetHttpCacheHeadersFromUmbracoContent(model.Content, UmbracoContext.Current.InPreviewMode, Response.Cache, cacheExpiryProperties);
            }
        }
Beispiel #2
0
        public void Setup()
        {
            _fixture = new Fixture()
                       .Customize(new AutoMoqCustomization())
                       .Customize(new MultipleCustomization());

            _cache       = new Dictionary <string, RandomObject>();
            _baseAddress = _fixture.Create <string>();
            _uri         = _fixture.Create <string>();

            _sut = _fixture.Create <HttpCachingService>();
        }
        public void Setup()
        {
            _fixture = new Fixture()
                  .Customize(new AutoMoqCustomization())
                  .Customize(new MultipleCustomization());

            _cache = new Dictionary<string, RandomObject>();
            _baseAddress = _fixture.Create<string>();
            _uri = _fixture.Create<string>();

            _sut = _fixture.Create<HttpCachingService>();
        }
        public void DefaultCachePeriodOverridesExpiryDateIfSooner()
        {
            // Expires after 30 hours
            var expiryDate = _startDate.AddHours(30);

            // Should pick the default timespan, not expiry date
            var service   = new HttpCachingService();
            var freshness = service.WorkOutCacheFreshness(_startDate, _defaultCachePeriod, new List <DateTime>()
            {
                expiryDate
            });

            Assert.AreEqual(freshness.FreshFor, _defaultCachePeriod);
            Assert.AreEqual(freshness.FreshUntil, _startDate.Add(_defaultCachePeriod));
        }
        public void ExpiryDateOverridesDefaultCachePeriodIfSooner()
        {
            // Expires after 23 hours
            var expiryDate = _startDate.AddHours(23);

            // Should pick the expiry date, not default timespan
            var service   = new HttpCachingService();
            var freshness = service.WorkOutCacheFreshness(_startDate, _defaultCachePeriod, new List <DateTime>()
            {
                expiryDate
            });

            Assert.AreEqual(freshness.FreshFor, expiryDate - _startDate);
            Assert.AreEqual(freshness.FreshUntil, expiryDate);
        }
        public void EarliestExpiryDateWins()
        {
            var expiryDate1 = _startDate.AddHours(15);
            var expiryDate2 = _startDate.AddHours(7);
            var expiryDate3 = _startDate.AddHours(11);

            // Should pick second expiry date
            var service   = new HttpCachingService();
            var freshness = service.WorkOutCacheFreshness(_startDate, _defaultCachePeriod, new List <DateTime>()
            {
                expiryDate1, expiryDate2, expiryDate3
            });

            Assert.AreEqual(freshness.FreshFor, expiryDate2 - _startDate);
            Assert.AreEqual(freshness.FreshUntil, expiryDate2);
        }
        public void ExpiryDateInPastIsIgnored()
        {
            // Expires in past
            var expiryDate = _startDate.AddHours(-5);

            // Expiry should be the default timespan
            // This is because the date likely relates to partial content, which is not part of what we'll be serving up anyway.
            // If the whole content had expired, we would be asking to cache it in the first place.
            var service   = new HttpCachingService();
            var freshness = service.WorkOutCacheFreshness(_startDate, _defaultCachePeriod, new List <DateTime>()
            {
                expiryDate
            });

            Assert.AreEqual(freshness.FreshFor, _defaultCachePeriod);
            Assert.AreEqual(freshness.FreshUntil, _startDate.Add(_defaultCachePeriod));
        }
        public void PreviewModeSetsCacheabilityToNoCache()
        {
            var content = new Mock <IPublishedContent>();
            var prop    = new Mock <IPublishedProperty>();

            content.Setup(x => x.GetProperty(It.IsAny <string>())).Returns(prop.Object);
            var cache = new Mock <HttpCachePolicyBase>();

            cache.CallBase = true;
            cache.Setup(x => x.SetCacheability(It.IsAny <HttpCacheability>()));
            cache.Setup(x => x.AppendCacheExtension(It.IsAny <string>()));
            cache.Setup(x => x.SetMaxAge(It.IsAny <TimeSpan>()));

            var service = new HttpCachingService();

            service.SetHttpCacheHeadersFromUmbracoContent(content.Object, true, cache.Object);

            cache.Verify(x => x.SetCacheability(HttpCacheability.NoCache));
        }