WebPart.PartCacheInvalidate 方法 (Storage, String)
将标记指定的存储类型为过时 Web 部件缓存中的指定的缓存项。
命名空间: Microsoft.SharePoint.WebPartPages
程序集: Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)
语法
声明
Protected Sub PartCacheInvalidate ( _
storage As Storage, _
key As String _
)
用法
Dim storage As Storage
Dim key As String
Me.PartCacheInvalidate(storage, key)
protected void PartCacheInvalidate(
Storage storage,
string key
)
参数
storage
类型:Microsoft.SharePoint.WebPartPages.Storage一个Storage值,该值标识存储在 Web 部件缓存将标记为已过时分配的类型。可能值有Personal和Shared。
key
类型:System.String一个标识 Web 部件缓存中指定的存储类型的缓存条目的值。
备注
调用PartCacheInvalidate(Storage, String)方法将Storage值和密钥作为参数传递将标记为过时的 Web 部件缓存中指定的存储类型仅指定的缓存的值。调用**[M:Microsoft.SharePoint.WebPartPages.WebPart.PartCacheInvalidate()]**方法将标记为已过时的 Web 部件缓存的所有内容。调用PartCacheInvalidate(Storage)方法将标记指定的存储类型为过时 Web 部件缓存中的所有内容。
示例
下面的代码示例显示 Web 部件缓存并显示它首次呈现,并提供一个用于刷新与当前时间的 Web 部件缓存按钮的时间。
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") + " "))
Me.RenderChildren(output)
End Sub
Private Function fetchData() As String
Return DateTime.Now.ToLongTimeString()
End Function
End Class
End Namespace
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")+ " ");
this.RenderChildren(output);
}
private string fetchData()
{
return DateTime.Now.ToLongTimeString();
}
}
}