Page Caching In ASP.NET

public static string GetStockXMLFromCache()
        // Get reference to calling page
        Page page = (Page)HttpContext.Current.CurrentHandler;
        string cacheKey = "StockXML";
        string xml = "";

        if (page.Cache[cacheKey] != null)
            // Return the cached version
            return (string)page.Cache[cacheKey];
            // Fill cache
            // Get file path to stock xml
            string xmlPath = HttpContext.Current.Server.MapPath("/path/to/stock.xml");

            // XML Reader
            XmlTextReader xtr = new XmlTextReader(xmlPath);
            XmlDocument xmlDoc = new XmlDocument();
            // Close reader
            xml = xmlDoc.OuterXml;

            // Create cache dependency based on the xml file
            CacheDependency dependency = new CacheDependency(xmlPath);
            // Add xml to cache with dependency
            page.Cache.Insert(cacheKey, xml, dependency);

        return xml;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s