WebPart.PartCacheRead method

Reads and returns a value from the specified location in the specified type of storage in the Web Part cache.

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

'Declaration
Protected Function PartCacheRead ( _
	storage As Storage, _
	key As String _
) As Object
'Usage
Dim storage As Storage
Dim key As String
Dim returnValue As Object

returnValue = Me.PartCacheRead(storage, _
	key)

Parameters

storage
Type: Microsoft.SharePoint.WebPartPages.Storage

A Storage value that specifies the type of storage in the Web Part cache where a value is read. Possible values are Personal and Shared. Specifying None returns a null reference (Nothing in Visual Basic).

key
Type: System.String

The name of the value to be read from the Web Part cache.

Return value

Type: System.Object
An object that returns the value that is read.

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: