ControlCachePolicy クラス

定義

ASP.NET ユーザー コントロールの出力キャッシュ設定に対する、プログラムによるアクセス手段を提供します。

public ref class ControlCachePolicy sealed
public sealed class ControlCachePolicy
type ControlCachePolicy = class
Public NotInheritable Class ControlCachePolicy
継承
ControlCachePolicy

次のコード例は、実行時にユーザー コントロールを動的に読み込み、プログラムで操作する方法を示しています。 属性は PartialCachingAttribute 、 という名前 SimpleControlのユーザー コントロールに適用されます。つまり、実行時にユーザー コントロールがコントロールによって PartialCachingControl ラップされます。 オブジェクトのキャッシュ設定は SimpleControl 、関連付けられた ControlCachePolicy オブジェクトを使用してプログラムで操作できます。これは、それをラップするコントロールへの PartialCachingControl 参照を通じて使用できます。 この例では、 Duration プロパティはページの初期化中に調べられ、一部の条件が満たされた場合は SetSlidingExpiration および SetExpires メソッドを使用して変更されます。

<%@ Page Language="C#" %>
<%@ Reference Control="SimpleControl.ascx" %>
<script language="C#" runat="server">

// The following example demonstrates how to load a user control dynamically at run time, and
// work with the ControlCachePolicy object associated with it.

// Loads and displays a UserControl defined in a seperate Logonform.ascx file.
// You need to have "SimpleControl.ascx" file in 
// the same directory as the aspx file. 

void Page_Init(object sender, System.EventArgs e) {
    
    // Obtain a PartialCachingControl object which wraps the 'LogOnControl' user control.
    PartialCachingControl pcc = LoadControl("SimpleControl.ascx") as PartialCachingControl;        
    
    // If the control is slated to expire in greater than 60 Seconds
    if (pcc.CachePolicy.Duration > TimeSpan.FromSeconds(60) ) 
    {        
        // Make it expire faster. Set a new expiration time to 30 seconds, and make it
        // an absolute expiration if it isnt already.        
        pcc.CachePolicy.SetExpires(DateTime.Now.Add(TimeSpan.FromSeconds(30)));
        pcc.CachePolicy.SetSlidingExpiration(false);
    }                    
    Controls.Add(pcc);
}
</script>
<%@ Page Language="VB" %>
<%@ Reference Control="SimpleControl.ascx" %>
<script language="VB" runat="server">

    ' The following example demonstrates how to load a user control dynamically at run time, and
    ' work with the ControlCachePolicy object associated with it.

    ' Loads and displays a UserControl defined in a seperate Logonform.ascx file.
    ' You need to have "SimpleControl.ascx" file in 
    ' the same directory as the aspx file. 

    Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
    
        ' Obtain a PartialCachingControl object which wraps the 'LogOnControl' user control.
        Dim pcc As PartialCachingControl
        pcc = LoadControl("SimpleControl.ascx")
    
        ' If the control is slated to expire in greater than 60 Seconds
        If (pcc.CachePolicy.Duration > TimeSpan.FromSeconds(60)) Then
            ' Make it expire faster. Set a new expiration time to 30 seconds, and make it
            ' an absolute expiration if it isnt already.        
            pcc.CachePolicy.SetExpires(DateTime.Now.Add(TimeSpan.FromSeconds(30)))
            pcc.CachePolicy.SetSlidingExpiration(False)
        End If
        Controls.Add(pcc)
    End Sub
</script>

次のコード例では、Web Forms ページからユーザー コントロールを使用SimpleControlする方法を示します。 この例を正常に実行するには、ユーザー コントロール ファイル (.ascx)、その分離コード ファイル (.cs または .vb)、およびユーザー コントロール (.aspx) をホストするWeb Forms ページが同じディレクトリ内にあることを確認します。

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

[PartialCaching(100)]
public partial class SimpleControl : System.Web.UI.UserControl
{    
    protected void Page_Load(object sender, EventArgs e)
    {
        ItemsRemaining.Text = GetAvailableItems().ToString();
        CacheTime.Text = DateTime.Now.ToLongTimeString();
    }

    private int GetAvailableItems()
    {
        SqlConnection sqlConnection = new SqlConnection
            ("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;");
        SqlCommand sqlCommand = sqlConnection.CreateCommand();
        sqlCommand.CommandType = CommandType.StoredProcedure;
        sqlCommand.CommandText = "GetRemainingItems";
        sqlConnection.Open();
        int items = (int)sqlCommand.ExecuteScalar();
        sqlConnection.Close();
        return items;
    }
}
Imports System.Data.SqlClient

Partial Class SimpleControl
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        ItemsRemaining.Text = GetAvailableItems().ToString()
        CacheTime.Text = DateTime.Now.ToLongTimeString()
    End Sub

    Private Function GetAvailableItems() As Integer
        Dim sqlConnection As SqlConnection
        Dim sqlCommand As SqlCommand
        Dim items As Integer

        sqlConnection = New SqlConnection("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;")
        sqlCommand = sqlConnection.CreateCommand()
        sqlCommand.CommandType = Data.CommandType.StoredProcedure
        sqlCommand.CommandText = "GetRemainingItems"
        sqlConnection.Open()
        items = CInt(sqlCommand.ExecuteScalar())
        sqlConnection.Close()
        Return items
    End Function
End Class
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SimpleControl.ascx.cs" Inherits="SimpleControl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" runat="server">
<table cellspacing="15">
<tr>
<td><b>Available items: </b></td>
<td>
    <asp:Label ID="ItemsRemaining" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td><b>As of: </b></td>
<td>
    <asp:Label ID="CacheTime" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ Control Language="VB" AutoEventWireup="true" CodeFile="SimpleControl.ascx.vb" Inherits="SimpleControl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" runat="server">
<table cellspacing="15">
<tr>
<td><b>Available items: </b></td>
<td>
    <asp:Label ID="ItemsRemaining" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td><b>As of: </b></td>
<td>
    <asp:Label ID="CacheTime" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>

注釈

クラスは ControlCachePolicy 、プログラムによるユーザー コントロール シナリオで開発者がユーザー コントロール (.ascx ファイル) の出力キャッシュ設定を指定するために使用されます。 ASP.NET インスタンス内にユーザー コントロールを BasePartialCachingControl 埋め込みます。 クラスは BasePartialCachingControl 、出力キャッシュが有効になっているユーザー コントロールを表します。 コントロールの PartialCachingControl プロパティにBasePartialCachingControl.CachePolicyアクセスすると、常に有効なControlCachePolicyオブジェクトを受け取ります。 ただし、コントロールの プロパティにUserControl.CachePolicyアクセスする場合は、ユーザー コントロールが既にコントロールによってラップされている場合にのみ、有効なControlCachePolicyオブジェクトをBasePartialCachingControl受け取UserControlります。 ラップされていない場合、 プロパティによって返されるオブジェクトは、 ControlCachePolicy が関連付けられていない BasePartialCachingControlため、操作しようとすると例外をスローします。 インスタンスが例外を UserControl 生成せずにキャッシュをサポートしているかどうかを判断するには、 プロパティを SupportsCaching 調べます。

クラスの ControlCachePolicy 使用は、出力キャッシュを有効にするいくつかの方法の 1 つです。 次の一覧では、出力キャッシュを有効にするために使用できるメソッドについて説明します。

  • @ OutputCache宣言型のシナリオで出力キャッシュを有効にするには、 ディレクティブを使用します。

  • 属性を PartialCachingAttribute 使用して、分離コード ファイル内のユーザー コントロールのキャッシュを有効にします。

  • クラスを ControlCachePolicy 使用して、前のいずれかのメソッドを使用してキャッシュが有効になっており、 メソッドを使用して動的に読み込まれたインスタンスを操作 BasePartialCachingControl するプログラムシナリオでキャッシュ設定を TemplateControl.LoadControl 指定します。 インスタンスは ControlCachePolicy 、コントロール ライフ サイクルの Init ステージと PreRender ステージの間でのみ正常に操作できます。 フェーズ後PreRenderにオブジェクトをControlCachePolicy変更すると、ASP.NET は例外をスローします。これは、コントロールのレンダリング後に行われた変更がキャッシュ設定に実際に影響を与えることはできません (ステージ中にRenderコントロールがキャッシュされるため)。 最後に、ユーザー コントロール インスタンス (したがって、その ControlCachePolicy オブジェクト) は、実際にレンダリングされる場合にのみプログラムによる操作に使用できます。

プロパティ

Cached

ユーザー コントロールに対してフラグメント キャッシュが有効かどうかを示す値を取得または設定します。

Dependency

キャッシュされたユーザー コントロール出力に関連付けられた CacheDependency クラスのインスタンスを取得または設定します。

Duration

キャッシュされたアイテムが出力キャッシュ内に存続する時間を取得または設定します。

ProviderName

コントロール インスタンスに関連付けられている出力キャッシュ プロバイダー名を取得または設定します。

SupportsCaching

ユーザー コントロールがキャッシュをサポートしているかどうかを示す値を取得します。

VaryByControl

キャッシュされた出力の変更に使用するコントロール ID のリストを取得または設定します。

VaryByParams

キャッシュされた出力の変更に使用する GET パラメーター名または POST パラメーター名のリストを取得または設定します。

メソッド

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
SetExpires(DateTime)

ユーザー コントロールをラップする BasePartialCachingControl コントロールに対して、指定された日時にキャッシュ エントリの有効期限が切れるように指示します。

SetSlidingExpiration(Boolean)

ユーザー コントロールをラップする BasePartialCachingControl コントロールに対して、ユーザー コントロールのキャッシュ エントリで変化する有効期限または絶対有効期限が使用されるように設定するよう指示します。

SetVaryByCustom(String)

出力キャッシュがユーザー コントロールを変更するために使用するカスタム文字列のリストを設定します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください