Procedura: Impostare criteri di cache per una richiestaHow to: Set Cache Policy for a Request

L'esempio seguente illustra l'impostazione di criteri di cache per una richiesta.The following example demonstrates setting a cache policy for a request. L'input di esempio è un URI, ad esempio http://www.contoso.com/.The example input is a URI such as http://www.contoso.com/.

EsempioExample

L'esempio di codice seguente crea un criterio di cache che consente di usare la risorsa richiesta dalla cache se non è rimasta nella cache per più di un giorno.The following code example creates a cache policy that allows the requested resource to be used from the cache if it has not been in the cache for longer than one day. L'esempio visualizza un messaggio che indica se la risorsa è stata usata dalla cache, ad esempio "The response was retrieved from the cache : False.", quindi visualizza la risorsa.The example displays a message that indicates whether the resource was used from the cache—for example, "The response was retrieved from the cache : False."—and then displays the resource. Una richiesta può essere soddisfatta da qualsiasi cache tra il client e il server.A request can be fulfilled by any cache between the client and server.

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  

Vedere ancheSee Also

Gestione della cache per le applicazioni di reteCache Management for Network Applications
Criteri di cacheCache Policy
Criteri di cache basati sulla posizioneLocation-Based Cache Policies
Criteri di cache basati sull'oraTime-Based Cache Policies
Elemento <requestCaching> (impostazioni di rete)<requestCaching> Element (Network Settings)