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 페이지 초기화 및 사용 하 여 변경 하는 동안 속성을 검사 합니다 SetSlidingExpirationSetExpires 메서드 일부 조건이 충족 될 경우.

<%@ 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>

다음 코드 예제는 SimpleControl Web Forms 페이지에서 사용자 정의 컨트롤입니다. 이 예제를 성공적으로 실행 하려면 사용자 정의 컨트롤 파일 (.ascx), 해당 코드 숨김 파일 (.cs 또는.vb) 및 Web Forms 페이지 (.aspx) 사용자 컨트롤을 호스팅하는 동일한 디렉터리에 있는지를 확인 합니다.

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 클래스에 출력 캐싱을 사용 하도록 설정 하는 사용자 정의 컨트롤을 나타냅니다. 액세스 하는 경우는 BasePartialCachingControl.CachePolicy 의 속성을 PartialCachingControl 컨트롤 항상 유효한 받습니다 ControlCachePolicy 개체입니다. 그러나 액세스 하는 경우는 UserControl.CachePolicy 의 속성을 UserControl 컨트롤을 유효한 ControlCachePolicy 사용자 정의 컨트롤은 이미 래핑한 경우에 개체를 BasePartialCachingControl 컨트롤입니다. 래핑되지 않은 경우는 ControlCachePolicy 속성에서 반환 된 개체는 연결 된 없기 때문에를 조작 하려고 할 때 예외를 throw BasePartialCachingControl합니다. 결정할 여부를 UserControl 인스턴스는 예외를 생성 하지 않고 캐싱을 지원, 검사를 SupportsCaching 속성입니다.

사용 하 여 ControlCachePolicy 클래스는 여러 가지 방법으로 출력 캐싱을 사용할 수 있습니다. 다음 목록에서는 출력 캐싱을 사용 하도록 설정 하 여 메서드를 설명 합니다.

  • @ OutputCache 지시문을 사용하여 선언적 시나리오에서 출력 캐싱을 사용하도록 설정합니다.

  • 사용 된 PartialCachingAttribute 특성을 코드 숨김 파일에 사용자 정의 컨트롤에 대 한 캐싱을 사용 하도록 설정 합니다.

  • 사용 하 여 합니다 ControlCachePolicy 클래스를 사용 하는 프로그래밍 시나리오에서 캐시 설정을 지정할 BasePartialCachingControl 이전 방법 중 하나를 사용 하 여 캐시 활성화 되 고 사용 하 여 동적으로 로드 된 인스턴스는 TemplateControl.LoadControl 메서드. A ControlCachePolicy 인스턴스가 성공적으로 조작할 수 사이 에서만 합니다 InitPreRender 컨트롤 수명 주기의 단계입니다. 수정 하는 경우는 ControlCachePolicy 후 개체를 PreRender 단계에서는 ASP.NET 예외를 throw 컨트롤이 렌더링 되 면 모든 변경 내용이 없습니다 실제로 설정에 영향을 캐시 (컨트롤 하는 동안 캐시 됩니다는 Render 단계). 사용자 컨트롤 인스턴스를 마지막으로, (및 따라서 해당 ControlCachePolicy 개체)은 프로그래밍 방식으로 조작에 사용할 수 있는 경우에 실제로 렌더링 됩니다.

속성

Cached

사용자 정의 컨트롤에서 부분 캐싱을 사용할지 여부를 나타내는 값을 가져오거나 설정합니다.

Dependency

캐시된 사용자 정의 컨트롤 출력과 관련된 CacheDependency 클래스의 인스턴스를 가져오거나 설정합니다.

Duration

출력 캐시에 캐시된 항목을 보관해야 하는 기간을 가져오거나 설정합니다.

ProviderName

컨트롤 인스턴스와 연결된 출력 캐시된 공급자의 이름을 가져오거나 설정합니다.

SupportsCaching

사용자 정의 컨트롤이 캐싱을 지원하는지 여부를 나타내는 값을 가져옵니다.

VaryByControl

출력 캐시를 변경하는 데 사용할 컨트롤 식별자 목록을 가져오거나 설정합니다.

VaryByParams

출력 캐시를 변경하는 데 사용할 GET 또는 POST 매개 변수 이름 목록을 가져오거나 설정합니다.

메서드

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
SetExpires(DateTime)

지정한 날짜 및 시간에 캐시 엔트리를 만료하도록 사용자 정의 컨트롤을 래핑하는 BasePartialCachingControl 컨트롤에 지시합니다.

SetSlidingExpiration(Boolean)

사용자 정의 컨트롤의 캐시 엔트리에 상대(sliding) 만료 또는 절대 만료를 사용하도록 사용자 정의 컨트롤을 래핑하는 BasePartialCachingControl 컨트롤에 지시합니다.

SetVaryByCustom(String)

사용자 정의 컨트롤의 출력 캐시를 변경하는 데 사용되는 사용자 지정 문자열의 목록을 설정합니다.

ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

적용 대상

추가 정보