WebOutputCache
Output Cache :
웹 폼 / 웹 사용자 정의 컨트롤의 상태값을 매번 요청하지 않고,
메모리에 저장 후 동일한 요청이 들어오면 바로 처리
페이지 단위 적용 --> "OutputCache 지시문" 사용
<%@ OutputCache Duration="초단위" VaryByParam="None: %>
코드 기반으로 적용
// 코드 기반으로 캐싱 기능 적용하기
Response.Write(DateTime.Now); // 현재 날짜 출력
// 캐싱 설정
Response.Cache.SetCacheability(HttpCacheability.Public);
// 캐싱 유효기간 설정
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
// 매개변수 방식 지정
Response.Cache.VaryByParams["*"] = true;
-------------------------------------------------------------------------------------
[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=""></asp:Label>
</div>
</form>
</body>
</html>
-------------------------------------------------------------------------------------
[FrmOutputCaching.aspx.cs] 소스코드
using System;
public partial class FrmOutputCaching : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 로드할 때마다 시간 출력 :
// DB연결해서 데이터를 1000건 가져온다고 가정하자.
this.lblTime.Text = DateTime.Now.ToLongTimeString();
}
}
-------------------------------------------------------------------------------------
[실행결과]
--> 60초(1분) 동안은 '새로고침'해도 웹 페이지의 화면이 바뀌지 않는다.
[FrmOutputCaching.aspx] 소스코드에서 "OutputCache 지시문" 사용했기 때문에...
<%@ OutputCache Duration="초단위" VaryByParam="None: %>
-------------------------------------------------------------------------------------
코드 기반으로 동일하게 캐싱 기능 적용하기 위해서 "FrmOutputCachingByCode.aspx"라는 이름으로 "웹 폼(Web Form)"을 하나 추가한 후 아래와 같이 "FrmOutputCachingByCode.aspx.cs"에서 코드를 작성한다.
[FrmOutputCachingByCode.aspx.cs] 소스코드
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
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;
}
}
-------------------------------------------------------------------------------------
[실행결과]
* Internet Explorer 브라우저에서 실행해야 됨.(FireFox 브라우저에서는 실행 안됨.)
--> [FrmOutputCachingByCode.aspx.cs] 소스코드에서 "코드 기반으로 캐싱 기능을 적용"하여, 60초(1분) 동안은 '새로고침'해도 웹 페이지의 화면이 바뀌지 않는다.