如何:设置请求的缓存策略

以下示例演示如何设置请求的缓存策略。 示例输入是 URI,例如 http://www.contoso.com/

Example

下面的代码示例创建一个缓存策略,该策略允许从缓存中使用请求的资源(如果缓存中未超过一天)。 该示例显示一条消息,该消息指示是否使用缓存中的资源(例如), "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

另请参阅