WebPart.PartCacheWrite method

Stores the specified value of the specified type of storage in the Web Part cache.

Namespace:  Microsoft.SharePoint.WebPartPages
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

protected void PartCacheWrite(
	Storage storage,
	string key,
	Object value,
	TimeSpan timeout


Type: Microsoft.SharePoint.WebPartPages.Storage

A Storage value that specifies the type of storage in the Web Part cache where a value is stored. Possible values are Personal and Shared. Specifying None stores nothing.

Type: System.String

The name to be used as the key for the value to be stored in the Web Part cache.

Type: System.Object

A System.Object that contains the value to be stored.

Type: System.TimeSpan

A System.TimeSpan that specifies the period during which PartCacheWrite attempts to write a value.

If the CacheType is CacheObject, any object can be stored in the cache. If the CacheType is Database, then the object must be marked as serializable (System.SerializableAttribute).

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>
        ToolboxData("<{0}:CacheSample runat=server></{0}:CacheSample>"),

    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);

        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;");

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