WebPart.PartCacheInvalidate method (Storage, String)

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

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

'Declaration
Protected Sub PartCacheInvalidate ( _
	storage As Storage, _
	key As String _
)
'Usage
Dim storage As Storage
Dim key As String

Me.PartCacheInvalidate(storage, key)

Parameters

storage
Type: Microsoft.SharePoint.WebPartPages.Storage

A Storage value that identifies the type of storage allocated in the Web Part cache to mark as outdated. Possible values are Personal and Shared.

key
Type: System.String

A value that identifies the cache entry of the specified storage type in the Web Part cache.

Calling the PartCacheInvalidate(Storage, String) method passing a Storage value and a key as arguments marks only the specified cached value of the specified storage type in the Web Part cache as outdated. Call the [M:Microsoft.SharePoint.WebPartPages.WebPart.PartCacheInvalidate()] method to mark all content of the Web Part cache as outdated. Call the PartCacheInvalidate(Storage) method to mark all content 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.

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

Namespace WebPartLibrary1
    <DefaultProperty("Text"), _
    ToolboxData("<{0}:CacheSample runat=server></{0}:CacheSample>"), _
    XmlRoot([Namespace] := "WebPartLibrary1")>
   Public Class CacheSample

      Inherits Microsoft.SharePoint.WebPartPages.WebPart
      Private refreshButton As Button

      Public Sub New()
         AddHandler Me.PreRender, AddressOf UpdateCache
      End Sub

      Protected Overrides Sub CreateChildControls()
         refreshButton = New Button()
         refreshButton.Text = "Refresh Cache"
         AddHandler refreshButton.Click, AddressOf refreshButton_click
         Me.Controls.Add(refreshButton)
      End Sub

      Public Sub UpdateCache(o As Object, e As System.EventArgs)

         If Me.PartCacheRead(Storage.Shared, "cacheKey") Is Nothing Then
            Me.PartCacheWrite(Storage.Shared, "cacheKey", fetchData(), TimeSpan.FromSeconds(10))
         End If
      End Sub

      Private Sub refreshButton_click(o As Object, e As System.EventArgs)
         Me.PartCacheInvalidate(Storage.Shared, "cacheKey")
      End Sub

      Protected Overrides Sub RenderWebPart(output As HtmlTextWriter)
         output.Write("Cache Value: ")
         output.Write((PartCacheRead(Storage.Shared, "cacheKey") + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"))
         Me.RenderChildren(output)
      End Sub

      Private Function fetchData() As String
         Return DateTime.Now.ToLongTimeString()
      End Function
   End Class
End Namespace

Show: