caching of generated xml in asp.net
I am generating XML using linq to XML (XElements etc) from the database and with specific times. The biggest issue is that this XML will not change that often so i am trying to cache it in the code behind. Basically my code looks something like this:
XDocument x = new XDocument(
new XElement(ns + "SomeRandomDate", DateTime.Now())
);
Response.Clear();
Response.ContentType = "appli开发者_JAVA技巧cation/xml";
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(false);
Response.Cache.VaryByParams["Category"] = true;
x.Save(Response.Output);
Response.End();
My biggest issue is that this does not seem to be working. Any ideas?
Why not use the HTTP cache?
XDocument x = (XDocument)HttpContext.Current.Cache[ns + "SomeRandomDate"];
if (x == null)
{
x = new XDocument(new XElement(ns + "SomeRandomDate", DateTime.Now()));
HttpContext.Current.Cache.Insert(ns + "SomeRandomDate", x, null,
DateTime.Now.AddHours(12d), Cache.NoSlidingExpiration);
}
精彩评论