tags:

views:

18

answers:

1

Hey there.

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 = "application/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?

+2  A: 

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);
}
Christian Nesmark
@leppie, Response.Cache controls cache headers for browser.
VinayC
@VinayC: Oh my, you are correct :o)
leppie