다음을 통해 공유


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 에서 속성은 페이지 초기화 중에 검사되고 일부 조건이 충족되는 경우 및 SetExpires 메서드를 사용하여 SetSlidingExpiration 변경됩니다.

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

클래스를 ControlCachePolicy 사용하는 것은 출력 캐싱을 사용하도록 설정할 수 있는 여러 가지 방법 중 하나입니다. 다음 목록에서는 출력 캐싱을 사용하도록 설정하는 데 사용할 수 있는 방법을 설명합니다.

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

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

  • 클래스를 ControlCachePolicy 사용하여 이전 메서드 중 하나를 사용하여 캐시를 사용하도록 설정하고 메서드를 사용하여 동적으로 로드된 인스턴스로 BasePartialCachingControl 작업하는 프로그래밍 방식 시나리오에서 TemplateControl.LoadControl 캐시 설정을 지정할 수 있습니다. 인스턴스는 ControlCachePolicy 컨트롤 수명 주기의 Init 단계 간에 PreRender 만 성공적으로 조작할 수 있습니다. 단계 후 PreRender 개체를 ControlCachePolicy 수정하는 경우 컨트롤이 렌더링된 후 변경된 내용이 실제로 캐시 설정에 영향을 줄 수 없으므로 ASP.NET 예외를 Render throw합니다(단계에서 컨트롤이 캐시됨). 마지막으로 사용자 컨트롤 인스턴스(따라서 해당 ControlCachePolicy 개체)는 실제로 렌더링될 때만 프로그래밍 방식 조작에 사용할 수 있습니다.

속성

Name Description
Cached

사용자 컨트롤에 대해 조각 캐싱을 사용할 수 있는지 여부를 나타내는 값을 가져오거나 설정합니다.

Dependency

캐시된 사용자 제어 출력과 연결된 클래스의 CacheDependency 인스턴스를 가져오거나 설정합니다.

Duration

캐시된 항목이 출력 캐시에 남아 있는 시간을 가져오거나 설정합니다.

ProviderName

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

SupportsCaching

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

VaryByControl

캐시된 출력을 변경할 컨트롤 식별자 목록을 가져오거나 설정합니다.

VaryByParams

캐시된 출력을 GET 변경할 매개 변수 이름 목록을 가져오거나 POST 설정합니다.

메서드

Name Description
Equals(Object)

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

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

기본 해시 함수로 사용됩니다.

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

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

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

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

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

BasePartialCachingControl 지정된 날짜 및 시간에 캐시 항목을 만료하도록 사용자 컨트롤을 래핑하는 컨트롤에 지시합니다.

SetSlidingExpiration(Boolean)

BasePartialCachingControl 슬라이딩 또는 절대 만료를 사용하도록 사용자 컨트롤의 캐시 항목을 설정하도록 사용자 컨트롤을 래핑하는 컨트롤에 지시합니다.

SetVaryByCustom(String)

출력 캐시에서 사용자 정의 컨트롤을 변경하기 위해 사용할 사용자 지정 문자열 목록을 설정합니다.

ToString()

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

(다음에서 상속됨 Object)

적용 대상

추가 정보