private void Invalidate(ItemLevelCachePart part)
        {
            InitializeSettings(part);

            foreach (var settings in part.ItemLevelCacheSettings)
            {
                var displayType = settings.Key;

                switch (settings.Value.InvalidationAction)
                {
                case ItemLevelCacheInvalidationAction.Evict:

                    mCacheService.RemoveByTag(ItemLevelCacheTag.For(part, displayType));
                    break;

                case ItemLevelCacheInvalidationAction.PreRender:

                    var shape = mContentManager.BuildDisplay(part.ContentItem, displayType);
                    shape.BypassDonutTokenization = true;

                    var markup    = (string)mShapeDisplay.Display(shape);
                    var cacheItem = new ItemLevelCacheItem(markup);

                    mCacheService.RemoveByTag(ItemLevelCacheTag.For(part, displayType));

                    // Give the markup to the item-level cache service which will cache it if required.
                    mItemLevelCacheService.CacheItem(part.ContentItem, displayType, cacheItem);
                    break;
                }
            }
        }
        public ItemLevelCacheItem EndCapture(string markup)
        {
            var cacheItem = mCacheItem ?? new ItemLevelCacheItem();

            cacheItem.Markup = markup;

            mCacheItem = null;

            return(cacheItem);
        }
        public void CacheItem(IContent contentItem, string displayType, ItemLevelCacheItem itemLevelCacheItem)
        {
            if (contentItem == null)
            {
                return;
            }

            var itemLevelCachePart = contentItem.As <ItemLevelCachePart>();

            if (itemLevelCachePart == null || !itemLevelCachePart.ItemLevelCacheSettings.ContainsKey(displayType) || itemLevelCachePart.ItemLevelCacheSettings[displayType].Mode != ItemLevelCacheMode.CacheItem)
            {
                return;
            }

            var settings = itemLevelCachePart.ItemLevelCacheSettings[displayType];
            var cacheKey = GetCacheKey(contentItem, displayType);

            var cachedItem = mOutputCacheStorageProvider.GetCacheItem(cacheKey);

            if (cachedItem == null || cachedItem.IsInGracePeriod(mClock.UtcNow)) // TODO: Should this method check the cache first, or just blindly insert?
            {
                var serializedCacheItem = mJsonConverter.Serialize(itemLevelCacheItem);

                var cacheItem = new CacheItem()
                {
                    CachedOnUtc = mClock.UtcNow,
                    Duration    = settings.CacheDurationSeconds,
                    GraceTime   = settings.CacheGraceTimeSeconds,
                    Output      = mHttpContextAccessor.Current().Request.ContentEncoding.GetBytes(serializedCacheItem),
                    Tags        = new[]
                    {
                        ItemLevelCacheTag.GenericTag,
                        ItemLevelCacheTag.For(contentItem),
                        ItemLevelCacheTag.For(contentItem, displayType),
                        ItemLevelCacheTag.For(contentItem.ContentItem.TypeDefinition)
                    },
                    Tenant            = mShellSettings.Name,
                    CacheKey          = cacheKey,
                    InvariantCacheKey = cacheKey,
                    Url = ""
                };

                mOutputCacheStorageProvider.Set(cacheKey, cacheItem);

                // Also add the item tags to the tag cache.
                foreach (var tag in cacheItem.Tags)
                {
                    mTagCache.Tag(tag, cacheKey);
                }
            }
        }
Ejemplo n.º 4
0
        void ReinstateResources(ItemLevelCacheItem cacheItem)
        {
            foreach (var resource in cacheItem.IncludedResources)
            {
                mResourceManager.Include(resource.ResourceType, resource.ResourcePath, resource.ResourceDebugPath, resource.RelativeFromPath);
            }

            foreach (var resource in cacheItem.RequiredResources)
            {
                mResourceManager.Require(resource.ResourceType, resource.ResourceName);
            }

            foreach (var script in cacheItem.HeadScripts)
            {
                mResourceManager.RegisterHeadScript(script);
            }

            foreach (var script in cacheItem.FootScripts)
            {
                mResourceManager.RegisterFootScript(script);
            }
        }
 public void BeginCapture()
 {
     mCacheItem = new ItemLevelCacheItem();
 }