Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

WebPart.PartCacheInvalidate method (Storage)

Marks all contents of the specified storage type in the Web Part cache as outdated.

Namespace:  Microsoft.SharePoint.WebPartPages
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
protected internal void PartCacheInvalidate(
	Storage storage
)

Parameters

storage
Type: Microsoft.SharePoint.WebPartPages.Storage

A Storagevalue that identifies the type of storage allocated in the Web Part cache. Possible values are Personal and Shared.

Calling the PartCacheInvalidate(Storage) method passing a Storage value marks all content of the specified storage type in the Web Part cache as outdated. Call the [M:Microsoft.SharePoint.WebPartPages.WebPart.PartCacheInvalidate()] method to mark all of the contents of the Web Part cache as outdated. Call the PartCacheInvalidate(Storage, String) method to mark only the specified cached value of the specified storage type in the Web Part cache as outdated.

The following code example shows a Web Part that caches and displays the time when it is first rendered, and provides a button to refresh the Web Part cache with the current time.

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;

namespace WebPartLibrary1
{
    /// <summary>
    /// Summary description for CacheSample.
    /// </summary>
    [DefaultProperty("Text"),
        ToolboxData("<{0}:CacheSample runat=server></{0}:CacheSample>"),
        XmlRoot(Namespace="WebPartLibrary1")]

    public class CacheSample : Microsoft.SharePoint.WebPartPages.WebPart
    {
        Button refreshButton;

        public CacheSample()
        {
            this.PreRender+=new EventHandler(UpdateCache);
        }

        protected override void CreateChildControls()
        {
            refreshButton = new Button();
            refreshButton.Text="Refresh Cache";
            refreshButton.Click+=new EventHandler(refreshButton_click);
            this.Controls.Add(refreshButton);
        }

        public void UpdateCache(object o, System.EventArgs e)
        {

            if(this.PartCacheRead(Storage.Shared,"cacheKey") == null)
            {
                this.PartCacheWrite(Storage.Shared,"cacheKey", fetchData(), TimeSpan.FromSeconds(10));
            }
        }

        private void refreshButton_click(object o, System.EventArgs e)
        {
            this.PartCacheInvalidate(Storage.Shared, "cacheKey");
        }

        protected override void RenderWebPart(HtmlTextWriter output)
        {
            output.Write("Cache Value: ");
            output.Write(PartCacheRead(Storage.Shared,"cacheKey")+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
            this.RenderChildren(output);
        }

        private string fetchData()
        {
            return DateTime.Now.ToLongTimeString();
        }
    }
}

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.