开发者

Disable Sitecore's Html cache for one item

开发者 https://www.devze.com 2023-03-12 00:00 出处:网络
I want to disable the html cache for 1 item so it is always rendered. Background: I need to show information for companies stored in a separate database. In sitecore I have 1 item which has the use

I want to disable the html cache for 1 item so it is always rendered.

Background:

I need to show information for companies stored in a separate database. In sitecore I have 1 item which has the user control that shows the required information and depending on a Context parameter I figure out which company to show.

The sitecore tree look like this:

/sitecore
  /content
    /home
      /company-information

The url is: /show-company-information/[company-name]-[company-id]. I have a pipeline module that parses the url and sets the company information as the current item and adds the company id to HttpContext.Current.Items. That's how my user control figures out which company information to render.

It all works fine in development but once you deploy it to a Content Delivery server it stops working correctly. The first time the page is accessed it gets cached and every consecutive request returns the company information that was cached the first time.

My current workaround is to clear the HTML cache for the company-info item in the same pipeline step that parses the company-information but it seems a really dirty solution.

Is there a better way to achieve the same result?


EDIT

Here is how the site is setup in web.config and also the web database configuration:

<site name="website" virtualFolder="/" physicalFolder="/" rootPath="/sitecore/content/Home/" startItem="/home" language="en-GB" database="web" domain="extranet" loginPage="/user-login.aspx" allowDebug="true" cacheHtml="true" htmlCacheSize="400MB" registryCacheSize="500KB" viewStateCacheSize="500KB" xslCacheSize="20MB" filteredItemsCacheSize="20MB" enablePreview="true" enableWebEdit="true" enableDebugger="true" disableClientData="false" />

<!-- CACHE SIZES -->
    <cacheSizes>
      <sites>
        <website>
          <html>500MB</html>
          <registry&开发者_Go百科gt;500KB</registry>
          <viewState>500KB</viewState>
          <xsl>200MB</xsl>
        </website>
      </sites>
    </cacheSizes>

<database id="web" singleInstance="true" type="Sitecore.Data.Database, Sitecore.Kernel">
    <param desc="name">$(id)</param>
    <icon>Network/16x16/earth.png</icon>
    <securityEnabled>true</securityEnabled>
    <dataProviders hint="list:AddDataProvider">
      <dataProvider ref="dataProviders/main" param1="$(id)">
        <disableGroup>publishing</disableGroup>
        <prefetch hint="raw:AddPrefetch">
          <sc.include file="/App_Config/Prefetch/Common.config" />
          <sc.include file="/App_Config/Prefetch/Web.config" />
        </prefetch>
      </dataProvider>
    </dataProviders>
    <indexes hint="list:AddIndex">
      <index path="indexes/index[@id='articleIndex']" />
    </indexes>
    <proxiesEnabled>false</proxiesEnabled>
    <proxyDataProvider ref="proxyDataProviders/main" param1="$(id)" />
    <archives hint="raw:AddArchive">
      <archive name="archive" />
      <archive name="recyclebin" />
    </archives>
    <Engines.HistoryEngine.Storage>
      <obj type="Sitecore.Data.$(database).$(database)HistoryStorage, Sitecore.Kernel">
        <param connectionStringName="$(id)" />
        <EntryLifeTime>30.00:00:00</EntryLifeTime>
      </obj>
    </Engines.HistoryEngine.Storage>
    <cacheSizes hint="setting">
      <data>400MB</data>
      <items>400MB</items>
      <paths>10MB</paths>
      <standardValues>1MB</standardValues>
    </cacheSizes>
  </database>
</databases>

Page layouts structure:

layout - no output caching
  - sublayout - no caching options ticked
    - offending sublayout - no caching options ticked

Are we doing anything wrong for this to be cached so aggressively?


You can use the CacheManager to turn off caching during the Page Load event then turn it back on in PreRender. This will ensure that the page's html / xslt controls are not cached.

The CacheManager Class has the following method used by all controls when retrieving HTML caches

/// <summary>
/// Gets the HTML cache.
/// </summary>
/// <param name="site">The site.</param>
/// <returns>The HTML cache.</returns>
public static HtmlCache GetHtmlCache(SiteContext site)
{
  if (_enabled && (site != null))
  {
    return site.Caches.HtmlCache;
  }
  return null;
}

So setting the Enabled Property to false will stop the controls being rendered from the cache:

CacheManager.Enabled = false;

Its brutal but it works.


If your HTML is being cached, its from a specific rendering, e.g. a specific sublayout. If you have a sublayout set to cache and vary by anything in Layout Details, undo that. Or, you may be doing it in code, in which case you can change your C# to not cache it.

Here's a sublayout in code that is set to cache, you may have something like this that you'd want to not cache:

<sc:Sublayout Path="~/layouts/sublayouts/mycontrol.ascx" Cacheable="true" runat="server" />


If this cache problem is cleared by just publishing any sitecore item, then its safe to assume its the sitecore HTML cache.

Any layout or sublayout with caching enabled will also cache the output any control added within it. So if any parent sublayout of this control or rendering has caching enabled you'll need to disable it.

An easy way to view the cache settings for your page is via the debugger [Start] > [Debug] Hovering over the controls will allow you to view their resultant cache settings.


If your item is a sublayout, you can disable its rendering cache in the page editor component properties or through the definition of the sublayout item in the Cache section.

For example, I have a search results sublayout with cache disabled.

0

精彩评论

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