Leave object in cache unchanged

Crazy John

New Member
this code works "perfectly". Creates an XDocument, puts it in cache, and retrieves it the next time round.BUT i'd like the object in cache NOT to be affected by the changes made after the marked place, where I add some elements only valid in this session.The object in cache is relevant for all users on the page, so should only be genereted/altered once in cache lifetime.I COULD clone it from the cache or store it as a string I guess (not a reference type) and losd into new XDocument, but what would be the most gentle way? Seems overkill, when I can cache the XDocument as it is...Thanks in advance,Steen\[code\]{ string cachekey = "categories"; var cateGoryDoc = HttpRuntime.Cache.Get(cachekey) as XDocument; if (cateGoryDoc == null) { XDocument doc = XDocument.Parse(this.ProductList1.getXML()); IEnumerable<XElement> mainCats = from Category1 in doc.Descendants("product").Descendants("category") select Category1; var cDoc = new XDocument(new XDeclaration("1.0", "utf-8", null), new XElement("root")); cDoc.Root.Add(mainCats); cateGoryDoc = cDoc; HttpRuntime.Cache.Insert(cachekey, cDoc, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero); } // I DO NOT WANT TO CHANGE THE DOC IN CACHE!! if (HttpContext.Current.Request["tCats"] != null) { string[] tCats = HttpContext.Current.Request["tCats"].Split(Convert.ToChar(",")); foreach (string tCat in tCats) { cateGoryDoc.Root.Add(new XElement("selectedCategory", tCat)); } } this.catecoryXML.DocumentContent = cateGoryDoc.ToString(); this.catecoryXML.TransformSource = "/XSLTFile1.xslt"; }\[/code\]
 
Back
Top