WebMethodAttribute.CacheDuration 속성

정의

응답이 캐시에 보유되어야 하는 시간(초)을 가져오거나 설정합니다.

public:
 property int CacheDuration { int get(); void set(int value); };
public int CacheDuration { get; set; }
member this.CacheDuration : int with get, set
Public Property CacheDuration As Integer

속성 값

응답이 캐시에 보유되어야 하는 시간(초)입니다. 기본값은 0으로, 응답을 캐시하지 않음을 의미합니다.

예제

다음 예제에서는 XML 웹 서비스 메서드에 ServiceUsage 대한 호출 결과를 캐시에 60초 동안 배치합니다. XML 웹 서비스 클라이언트가 해당 시간 동안 XML 웹 서비스 메서드를 실행할 ServiceUsage 때마다 동일한 결과가 반환됩니다.

<%@ WebService Language="C#" Class="Counter" %>

using System.Web.Services;
using System;
using System.Web;

public class Counter : WebService {
     
     [ WebMethod(Description="Number of times this service has been accessed",
     CacheDuration=60,MessageName="ServiceUsage") ]
     public int ServiceUsage() {
          // If the XML Web service has not been accessed, initialize it to 1.
          if (Application["MyServiceUsage"] == null) {
              Application["MyServiceUsage"] = 1;
          }
          else {
              // Increment the usage count.
              Application["MyServiceUsage"] = ((int) Application["MyServiceUsage"]) + 1;
          }

          // Return the usage count.     
          return  (int) Application["MyServiceUsage"];
     }
}
<%@ WebService Language="VB" Class="Counter" %>

Imports System.Web.Services
Imports System
Imports System.Web

Public Class Counter
    Inherits WebService  

    <WebMethod(Description := "Number of times this service has been accessed", _
        CacheDuration := 60, _
        MessageName := "ServiceUsage")> _
    Public Function ServiceUsage() As Integer
        
        ' If the XML Web service has not been accessed, initialize it to 1.
        If Application("MyServiceUsage") Is Nothing Then
            Application("MyServiceUsage") = 1
        Else
            ' Increment the usage count.
            Application("MyServiceUsage") = CInt(Application("MyServiceUsage")) + 1
        End If
        
        ' Return the usage count.
        Return CInt(Application("MyServiceUsage"))
    End Function
End Class

설명

캐싱을 사용하는 경우 요청 및 응답이 적어도 캐시 기간 동안 서버의 메모리에 보관되므로 요청 또는 응답이 매우 크거나 요청이 크게 달라질 것으로 예상하는 경우 주의해야 합니다.

ASP.NET 2.0 웹 서비스 애플리케이션에서 출력 캐싱에 영향을 미칠 수 있는 두 가지 문제가 있습니다.

ASP.NET 2.0에서 테스트 페이지의 HTTP 메서드가 GET에서 POST로 변경되었습니다. 그러나 POST는 일반적으로 캐시되지 않습니다. ASP.NET 2.0 웹 서비스 애플리케이션에서 테스트 페이지를 변경하여 GET을 사용하면 캐싱이 제대로 작동합니다.

또한 HTTP는 사용자 에이전트(브라우저 또는 호출 애플리케이션)가 "Cache-Control"을 "no-cache"로 설정하여 서버 캐싱을 재정의할 수 있어야 함을 나타냅니다. 따라서 ASP.NET 애플리케이션에서는 "no-cache" 헤더를 발견하는 경우 캐시된 결과를 무시합니다.

적용 대상