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];
        }
        else
        {
            // 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();
            xmlDoc.Load(xtr);
            // Close reader
            xtr.Close();
            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;
    }

Advertisements