开发者

Customizing sublayout caching in Sitecore

开发者 https://www.devze.com 2023-02-24 13:47 出处:网络
When using WebControls in Sitecore, there is a way to customize caching behavior - override GetCachingID method.

When using WebControls in Sitecore, there is a way to customize caching behavior - override GetCachingID method. Is there a way to achieve something like this with Sublayouts(UserControls)? I'd like to add custom "VaryBy" options(example - "Vary By Moon P开发者_开发百科osition").


Yes, sublayout caching can vary by several different criteria by default. You can leverage varying by parameters to do this. The vary-by's are:

  • Vary by Data
  • Vary by Device
  • Vary by Login
  • Vary by Parameters
  • Vary by Query String
  • Vary by User

The approach for you to customize here is Vary by Parameters and YOU define what the parameters are. You can do this in Presentation Details where you dynamically assign a sublayout to an item (there is a section at the bottom of the control properties to define parameters) or you can set this via C# code. Here an example using C# code to statically assign a sublayout into my layout:

<h1>My website</h1>
<h2>My site is great</h2>
<sc:Sublayout ID="slMyControl" path="~/path/to/my/control.ascx" VaryByParm="true" Cachable="true" runat="server" />

(One thing to note in the above code, the attribute for VaryByParam is actually VaryByParm in Sitecore, which is obviously a typo in their code.)

Now in the C#, set the parameters programatically:

slMyControl.Parameters = "myKey1=MyVal1&myKey2=myVal2";

If you can get a Moon Position in C#, then convert it to a string and assign it to the parameters:

slMyControl.Parameters = "position=" + getMoonPosition().ToString();

I recently cached a calendar by the month and year which appear in the query string. Simple example with no error handling:

slEventCalendar.Parameters = string.Format("m={0}&y={1}", Request.QueryString["m"], Request.QueryString["y"]);

The parameter string you end up with eventually becomes part of the actual cache key. Coupling this with other vary by options just making a more complex cache key with more criteria and thus more cached instances. The general rule is, cache by the least amount of criteria you need to which will result in the most amount of use from that cached instance.

0

精彩评论

暂无评论...
验证码 换一张
取 消