Partilhar via


How to: Cache Page Output with File Dependencies

At times you might want to remove a page from the output cache when a file changes. For example, you might have a page that gets its contents from a process-intensive report that produces an XML file as output. The page needs to be reprocessed only if the XML file changes. To limit reprocessing to just those times when it is necessary, you can use the make the page's cache policy dependent on a single file. If required, you can make the cached page dependent on more than one file.

Note

You can explicitly remove any page from the output cache by calling the RemoveOutputCacheItem method. You can do this from the Global.asax file, from a custom ASP.NET server control, or from a page, depending on the needs of your application.

To make cached page output dependent upon a file

  1. Specify the settings for caching page output either declaratively or programmatically. For more information, see How to: Set Expiration Values for ASP.NET Page Caching, Setting the Cacheability of a Page, and Caching Multiple Versions of a Page.

  2. In the page code, call the AddFileDependency method. As the method's filename parameter, pass the path of the file on which you are creating a dependency.

    The following code example sets a file dependency on the TextFile1.txt file. When the file changes, the page output will be removed from the cache.

    protected void Page_Load(object sender, EventArgs e)
    {
        string fileDependencyPath = Server.MapPath("TextFile1.txt");
        Response.AddFileDependency(fileDependencyPath);
    
        // Set additional properties to enable caching.
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
        Response.Cache.SetCacheability(HttpCacheability.Public);
        Response.Cache.SetValidUntilExpires(true);
    }
    
    Protected Sub Page_Load(ByVal sender As Object, _
            ByVal e As EventArgs) Handles Me.Load
        Dim fileDependencyPath As String = _
            Server.MapPath("TextFile1.txt")
        Response.AddFileDependency(fileDependencyPath)
    
        ' Set additional properties to enable caching.
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(60))
        Response.Cache.SetCacheability(HttpCacheability.Public)
        Response.Cache.SetValidUntilExpires(True)
    End Sub
    

    Note

    You cannot use these methods from an ASP.NET user control. However, in any user control that specifies the @ OutputCache directive you can create a file dependency and assign it to the Dependency property.

To make cached page output dependent on a group of files

  1. Specify the settings for caching page output either declaratively or programmatically. For more information, see How to: Set Expiration Values for ASP.NET Page Caching, Setting the Cacheability of a Page, and Caching Multiple Versions of a Page.

  2. In the page code, create a String array or an ArrayList that contains the paths of the files to make the page dependent on.

  3. Call the AddFileDependencies method and as the filenames parameter, pass the array.

    The following code example creates a string array of the file paths for the TextFile1.txt and XMLFile1.xml files and makes the page output dependent on the two files. If either one of the files is modified, the page output will be removed from the cache.

    protected void Page_Load(object sender, EventArgs e)
    {
        string[] fileDependencies;
        string fileDependency1 = Server.MapPath("TextFile1.txt");
        string fileDependency2 = Server.MapPath("XMLFile1.xml");
        fileDependencies = new String[] { fileDependency1, 
            fileDependency2 };
        Response.AddFileDependencies(fileDependencies);
    }
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim fileDependencies() As String
        Dim fileDependency1 As String = Server.MapPath("TextFile1.txt")
        Dim fileDependency2 As String = Server.MapPath("XMLFile1.xml")
        fileDependencies = New String() {fileDependency1, _
            fileDependency2}
        Response.AddFileDependencies(fileDependencies)
    End Sub
    

See Also

Tasks

How to: Set a Page's Cacheability Programmatically

Concepts

Caching ASP.NET Pages

Setting the Cacheability of a Page