ObjectDataSource.CacheKeyDependency 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定使用者定義的索引鍵相依性,連結至資料來源控制項所建立的所有資料快取物件。
public:
virtual property System::String ^ CacheKeyDependency { System::String ^ get(); void set(System::String ^ value); };
public virtual string CacheKeyDependency { get; set; }
member this.CacheKeyDependency : string with get, set
Public Overridable Property CacheKeyDependency As String
屬性值
索引鍵,識別 ObjectDataSource 建立的所有快取物件。
範例
下列三個範例顯示網頁、程式碼後置頁面類別,以及從 Northwind 資料庫中的 Employees 資料表擷取記錄的資料存取類別。
第一個範例顯示包含兩 ObjectDataSource 個控制項、一個控制項和一個 DropDownList 控制項的 DetailsView 網頁。 第一個 ObjectDataSource 控制項和 DropDownList 控制項是用來從資料庫擷取和顯示員工名稱。 第二 ObjectDataSource 個 DetailsView 控制項和 控制項是用來擷取及顯示使用者所選取的員工記錄。
控制項已啟用 ObjectDataSource 快取。 因此,每個記錄只會從資料庫擷取一次。 屬性 CacheKeyDependency 設定為 「EmployeeDetails」,但任何字串值都可以當做索引鍵運作。 網頁也包含一個 Button 控制項,使用者可以按一下以過期快取的資料。
<form id="form1" runat="server">
<div>
<asp:objectdatasource
ID="ObjectDataSource1"
runat="server"
SelectMethod="GetFullNamesAndIDs"
TypeName="Samples.AspNet.CS.EmployeeLogic" />
<p>
<asp:dropdownlist
ID="DropDownList1"
runat="server"
DataSourceID="ObjectDataSource1"
DataTextField="FullName"
DataValueField="EmployeeID"
AutoPostBack="True"
AppendDataBoundItems="true">
<asp:ListItem Text="Select One" Value=""></asp:ListItem>
</asp:dropdownlist>
</p>
<asp:objectdatasource
ID="ObjectDataSource2"
runat="server"
SelectMethod="GetEmployee"
TypeName="Samples.AspNet.CS.EmployeeLogic"
EnableCaching="true"
CacheKeyDependency="EmployeeDetails" >
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" DefaultValue="-1" Name="empID" />
</SelectParameters>
</asp:objectdatasource>
<asp:DetailsView
ID="DetailsView1"
runat="server"
DataSourceID="ObjectDataSource2"
AutoGenerateRows="false">
<Fields>
<asp:BoundField HeaderText="Address" DataField="Address" />
<asp:BoundField HeaderText="City" DataField="City" />
<asp:BoundField HeaderText="Postal Code" DataField="PostalCode" />
</Fields>
</asp:DetailsView>
<asp:Button
ID="Button1"
runat="server"
Text="Check for latest data"
OnClick="Button1_Click" />
</div>
</form>
<form id="form1" runat="server">
<div>
<asp:objectdatasource
ID="ObjectDataSource1"
runat="server"
SelectMethod="GetFullNamesAndIDs"
TypeName="Samples.AspNet.CS.EmployeeLogic" />
<p>
<asp:dropdownlist
ID="DropDownList1"
runat="server"
DataSourceID="ObjectDataSource1"
DataTextField="FullName"
DataValueField="EmployeeID"
AutoPostBack="True"
AppendDataBoundItems="true">
<asp:ListItem Text="Select One" Value=""></asp:ListItem>
</asp:dropdownlist>
</p>
<asp:objectdatasource
ID="ObjectDataSource2"
runat="server"
SelectMethod="GetEmployee"
TypeName="Samples.AspNet.CS.EmployeeLogic"
EnableCaching="true"
CacheKeyDependency="EmployeeDetails" >
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" DefaultValue="-1" Name="empID" />
</SelectParameters>
</asp:objectdatasource>
<asp:DetailsView
ID="DetailsView1"
runat="server"
DataSourceID="ObjectDataSource2"
AutoGenerateRows="false">
<Fields>
<asp:BoundField HeaderText="Address" DataField="Address" />
<asp:BoundField HeaderText="City" DataField="City" />
<asp:BoundField HeaderText="Postal Code" DataField="PostalCode" />
</Fields>
</asp:DetailsView>
<asp:Button
ID="Button1"
runat="server"
Text="Check for latest data"
OnClick="Button1_Click" />
</div>
</form>
第二個範例顯示 事件的處理常式 Load ,以及 控制項事件的 Button 處理常式 Click 。 Load事件處理常式會建立快取專案,並將索引鍵設定為 CacheKeyDependency 值。 Click事件處理常式會移除索引鍵等於 CacheKeyDependency 值的快取專案。 移除快取專案時,所有相依于金鑰的快取資料都會過期。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Cache[ObjectDataSource2.CacheKeyDependency] = "CacheExample";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Cache.Remove(ObjectDataSource2.CacheKeyDependency);
Cache[ObjectDataSource2.CacheKeyDependency] = "CacheExample";
DetailsView1.DataBind();
}
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not (IsPostBack) Then
Cache(ObjectDataSource2.CacheKeyDependency) = "CacheExample"
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Cache.Remove(ObjectDataSource2.CacheKeyDependency)
Cache(ObjectDataSource2.CacheKeyDependency) = "CacheExample"
DetailsView1.DataBind()
End Sub
第三個範例顯示與 Northwind 資料庫互動的資料存取類別。 類別會使用 LINQ 查詢 Employees 資料表。 此範例需要代表 Northwind 資料庫和 Employees 資料表的 LINQ to SQL 類別。 如需詳細資訊,請參閱如何:在 Web 專案中建立LINQ to SQL類別。
public class EmployeeLogic
{
public static Array GetFullNamesAndIDs()
{
NorthwindDataContext ndc = new NorthwindDataContext();
var employeeQuery =
from e in ndc.Employees
orderby e.LastName
select new { FullName = e.FirstName + " " + e.LastName, EmployeeID = e.EmployeeID };
return employeeQuery.ToArray();
}
public static Employee GetEmployee(int empID)
{
if (empID < 0)
{
return null;
}
else
{
NorthwindDataContext ndc = new NorthwindDataContext();
var employeeQuery =
from e in ndc.Employees
where e.EmployeeID == empID
select e;
return employeeQuery.Single();
}
}
public static void UpdateEmployeeAddress(Employee originalEmployee, string address, string city, string postalcode)
{
NorthwindDataContext ndc = new NorthwindDataContext();
ndc.Employees.Attach(originalEmployee, false);
originalEmployee.Address = address;
originalEmployee.City = city;
originalEmployee.PostalCode = postalcode;
ndc.SubmitChanges();
}
}
Public Class EmployeeLogic
Public Shared Function GetFullNamesAndIDs() As Array
Dim ndc As New NorthwindDataContext()
Dim employeeQuery = _
From e In ndc.Employees _
Order By e.LastName _
Select FullName = e.FirstName + " " + e.LastName, EmployeeID = e.EmployeeID
Return employeeQuery.ToArray()
End Function
Public Shared Function GetEmployee(ByVal empID As Integer) As Employee
If (empID < 0) Then
Return Nothing
Else
Dim ndc As New NorthwindDataContext()
Dim employeeQuery = _
From e In ndc.Employees _
Where e.EmployeeID = empID _
Select e
Return employeeQuery.Single()
End If
End Function
Public Shared Sub UpdateEmployeeAddress(ByVal originalEmployee As Employee, ByVal address As String, ByVal city As String, ByVal postalcode As String)
Dim ndc As New NorthwindDataContext()
ndc.Employees.Attach(originalEmployee, False)
originalEmployee.Address = address
originalEmployee.City = city
originalEmployee.PostalCode = postalcode
ndc.SubmitChanges()
End Sub
End Class
備註
屬性 CacheKeyDependency 可以設定為任何任一字元串值。
當索引鍵過期時,所有快取的物件也會明確過期。 這可讓您以程式設計方式使 所建立的 ObjectDataSource 快取專案失效。
控制項 ObjectDataSource 支援資料快取。 快取資料時,呼叫 方法會 Select 從快取擷取資料,而不是從 所使用的商務物件 ObjectDataSource 擷取資料。 當快取過期時, Select 方法會從商務物件擷取資料,然後再次快取資料。
當 ObjectDataSource 屬性設定 true
為 且 CacheDuration 屬性設定為大於 0 的值時 EnableCaching ,控制項會自動快取資料,這表示快取在捨棄快取專案之前儲存資料的秒數。 值為 0 表示無限長快取。
您可以設定 CacheKeyDependency 屬性,在控制項和索引鍵所 ObjectDataSource 建立的所有快取專案之間建立相依性。 您可以隨時過期金鑰,以程式設計方式將所有快取專案到期。 使用 方法搭配 Cache.Remove 目前 CacheKeyDependency 值做為 參數,讓索引鍵過期。
針對 、、、 SelectMethod 和 SelectParameters 屬性的每個組合,都會建立唯一的 CacheDurationTypeName 快取專案。 CacheExpirationPolicy 在使用相同的類型、方法和參數載入資料的案例中,多個 ObjectDataSource 控制項可以使用相同的快取專案。
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應