다음을 통해 공유


방법: 요청에 대한 캐시 정책 설정

다음 예제에서는 요청에 대한 캐시 정책을 설정하는 방법을 보여 줍니다. 예제 입력은 URI(예: http://www.contoso.com/.)입니다.

예시

다음 코드 예제에서는 요청된 리소스가 하루 이상 캐시에 없는 경우 캐시에서 사용할 수 있도록 하는 캐시 정책을 만듭니다. 이 예제에서는 리소스가 캐시에서 사용되었는지 여부를 나타내는 메시지를 표시한 다음 리소스 "The response was retrieved from the cache : False."를 표시합니다. 클라이언트와 서버 간의 모든 캐시에서 요청을 처리할 수 있습니다.

using System;
using System.Net;
using System.Net.Cache;
using System.IO;

namespace Examples.System.Net.Cache
{
    public class CacheExample
    {
        public static void UseCacheForOneDay(Uri resource)
        {
            // Create a policy that allows items in the cache
            // to be used if they have been cached one day or less.
            HttpRequestCachePolicy requestPolicy =
                new HttpRequestCachePolicy (HttpCacheAgeControl.MaxAge,
                TimeSpan.FromDays(1));

            WebRequest request = WebRequest.Create (resource);
            // Set the policy for this request only.
            request.CachePolicy = requestPolicy;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            // Determine whether the response was retrieved from the cache.
            Console.WriteLine ("The response was retrieved from the cache : {0}.",
               response.IsFromCache);
            Stream s = response.GetResponseStream ();
            StreamReader reader = new StreamReader (s);
            // Display the requested resource.
            Console.WriteLine(reader.ReadToEnd());
            reader.Close ();
            s.Close();
            response.Close();
        }
        public static void Main(string[] args)
        {
            if (args == null || args.Length != 1)
            {
                Console.WriteLine ("You must specify the URI to retrieve.");
                return;
            }
            UseCacheForOneDay (new Uri(args[0]));
        }
    }
}
Imports System
Imports System.Net
Imports System.Net.Cache
Imports System.IO
Namespace Examples.System.Net.Cache
    Public Class CacheExample
        Public Shared Sub UseCacheForOneDay(ByVal resource As Uri)
            ' Create a policy that allows items in the cache
            ' to be used if they have been cached one day or less.
            Dim requestPolicy As New HttpRequestCachePolicy _
                  (HttpCacheAgeControl.MaxAge, TimeSpan.FromDays(1))
            Dim request As WebRequest = WebRequest.Create(resource)
            ' Set the policy for this request only.
            request.CachePolicy = requestPolicy
            Dim response As HttpWebResponse = _
             CType(request.GetResponse(), HttpWebResponse)
            ' Determine whether the response was retrieved from the cache.
            Console.WriteLine("The response was retrieved from the cache : {0}.", _
                response.IsFromCache)
            Dim s As Stream = response.GetResponseStream()
            Dim reader As New StreamReader(s)
            ' Display the requested resource.
            Console.WriteLine(reader.ReadToEnd())
            reader.Close()
            s.Close()
            response.Close()
        End Sub
        Public Shared Sub Main(ByVal args() As String)
            If args Is Nothing OrElse args.Length <> 1 Then
                Console.WriteLine("You must specify the URI to retrieve.")
                Return
            End If
            UseCacheForOneDay(New Uri(args(0)))
        End Sub
    End Class
End Namespace

참고하십시오