ASP.NET

60.C# ASP.NET - OutputCache

Godffs 2009. 10. 21. 00:33
반응형
카테고리의 경우 변하지 않기 때문에 카테고리에 많이 사용됨(지정된 시간에 한번만 사용됨)

OutputCache : 웹 폼 / 웹 사용자 정의 컨트롤의 상태값을 매번 요청하지 않고, 메모리에 저장 후 동일한 요청이 들어오면 바로 처리

새로고침을 할 때마다 Connection, Command, DataBinding 요청하여 리소스와 시간을 많이 잡아먹음


속도가 가장 늦는 명령어 Connection


Caching 기능을 적용시켜 두면 페이지 레벨로 속도 향상시 가장 좋음

한 번 읽어두고 지정된 시간안에는 변경되지 않는다.


<%@ OutputCache Duration="60" VaryByParam="None" %>

60초 동안에는 새로고침 해도 다시 읽어오지 않음 Output 객체에 담아 두고 바로 출력

( 페이지 지시문에 추가 : 프로세스를 사용하지 않음 )


전체 페이지 코드 입니다.
FrmOutputCaching.aspx

<%@ Page Language="C#" AutoEventWireup="true"

         CodeFile="FrmOutputCaching.aspx.cs" Inherits="FrmOutputCaching" %>

        

<%@ OutputCache Duration="60" VaryByParam="None" %>


<!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 runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

      <asp:Label ID="lblTime" runat="server" Text="Label"></asp:Label>

    </div> 

    </form>

</body>

</html>


FrmOutputCaching.aspx.cs

public partial class FrmOutputCaching : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        //로드할 때마다 시간 출력 : DB연결해서 데이터를 1000 가여온다고 가정

        this.lblTime.Text = DateTime.Now.ToShortTimeString();

    }

}


결과화면

[그림60-1]


- 사이트에 공지사항인 경우
메인 페이지에 출력 할 때 매번 Connection 하지 않고 시간을 정하여 바로 출력 되도록 할 수 있음
실시간으로 적용 하고자 할 때에는 OutputCache 사용 안하는게 좋다.
블로그의 경우 카테고리는 변경이 잘 안되기 때문에 OutputCache 기능을 사용해서 속도 향상

위와 똑같은 기능을 코드 기반으로 작성
해당 프로젝트에 새 항목 추가 -  웹 폼 추가 ( FrmOutputCachingByCode.aspx ) 바로 코드 비하인드페이지
FrmOutputCachingByCode.aspx.cs

public partial class FrmOutputCachingByCode : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        //코드 기반으로 캐싱 기능 적용하기

        Response.Write(DateTime.Now); //현재 날짜 출력

 

        //캐싱 설정

        Response.Cache.SetCacheability(HttpCacheability.Public);

 

        //캐싱 유효기간 설정

        Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));

 

        //매개변수 방식 지정

        Response.Cache.VaryByParams["*"] = true;       

    }

}


결과확인

[그림60-2]



반응형