Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Caching blocks

Vote:
 

We use quite a lot of blocks spread across our site.

These blocks are basically outputting title, image and link based on the child nodes of a pagereference property set in the block's properties.

I'd like to cache each instance of these blocks, and have been playing around with the asp.net outputcache (I've also tried caching just the data, e.g. the pagecollection fetched from a block's pagereference property, but the performance gain was a lot smaller compared to caching the entire usercontrol).

Since a block type may be used on multiple pages and also multiple times on the same page, I figured VaryByControl was the best suited OutputCacheParameters to use. That way, I were hoping that a cached version of the block based on its block properties would be created. 

So I did like this:

    [PartialCaching(50000, null, "NewsBlock.AllParams", null, false)]
    public partial class NewsBlock: SiteBlockControlBase
    {
        public string AllParams
        {
            get { return String.Format("{0};{1}",
                CurrentBlock.ListLink.ID,
                CurrentBlock.SkipCount.HasValue ? CurrentBlock.SkipCount.Value : 0
                );
            }
        }
...
}

And in my SiteBlockControlBase base class, I invalidate the cache when content has been changed on the site (via DataFactoryCache.VersionKey):

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            BasePartialCachingControl pcc = Parent as BasePartialCachingControl;
            if (pcc != null) pcc.Dependency = new CacheDependency(null, new[]
            {                
                DataFactoryCache.VersionKey
            });
        }

My problem is - that sporadically, a block cached for one page, may show up on another page (where the same block type, but not instance, is used). So it seems like the block properties are being ignored, and the cached version of the block/usercontrol are served regardless.

I'm questioning if maybe there is some EPiServer "magic" happening behind the scenes that is does not support caching of blocks? Maybe outputcaching of blocks is not supported for this kind of use-case?

Any suggestions are appreciated!

#113614
Nov 25, 2014 16:23
Vote:
 

Hi! Shouldn't "NewsBlock.AllParams" be just "AllParams"?

#113740
Nov 27, 2014 10:25
Vote:
 

Hi Mattias,

I've tried that as well, with the same result. I think you can use both variants.

#113741
Nov 27, 2014 10:28
Vote:
 

Ok. Hmm, I'm curious, could you try to add a HiddenField control with the same value as your AllParams property in the user control markup and set an ID on it and use that ID in the varyByControls parameter? :)

#113742
Nov 27, 2014 10:34
Vote:
 

Sorry for my late reply. I tried out your suggestion. Unfortunatly, it did not help. The first cached version of block is still showing up on other pages using the same block type.

#114242
Dec 08, 2014 12:25
Vote:
 

I am facing a similar problem with MVC shared blocks. Although I am using MVC OutputCache with VaryByParam and not PartialCaching, I (almost) solved my problem by overriding the ToString() function in my BlockData Object. Maybe you could try the same for your NewsBlock controller, in the ToString function I would then return the result you got in AllParams.

#114248
Dec 08, 2014 14:13
Vote:
 

@Rene

Thank you. I tried your approach, unfortunately with the same result as before.

However, I figured out that this issue is limited to similar type blocks on the same page type. 
In my case this means that blocks cached on my StartPageType will only occur on that page type. But the same cached blocks (of the same type) will not show on my SectionPageType. But the 1st cached block cached on a page of type SectionPageType will be shared given that both the current page and block is of the same type as cached, regardless of what I've specified in VaryByControl.

As a side note I'll mention that I've also tested this in a vanilla Alloy site, with the same issues.

#114295
Edited, Dec 09, 2014 13:13
Vote:
 

Here's an update for future reference.

I ended up caching the actual data to be used in my newslists instead of using output cache. Initially I experienced small improvements by using this approach, but after some tweaking I'm pretty satisfied with the result. 

#114793
Dec 22, 2014 14:39
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.