예제 #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);
            }
        }
        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));
        }