0

このコードは「完全に」機能します。XDocument を作成してキャッシュに入れ、次回に取得します。

しかし、キャッシュ内のオブジェクトが、このセッションでのみ有効な要素を追加するマークされた場所の後に行われた変更の影響を受けないようにしたいと思います。

キャッシュ内のオブジェクトは、ページ上のすべてのユーザーに関連するため、キャッシュの有効期間中に一度だけ生成/変更する必要があります。

キャッシュから複製するか、推測する文字列 (参照型ではない) として保存し、新しい XDocument に失う可能性がありますが、最も穏やかな方法は何でしょうか? XDocumentをそのままキャッシュできると、やり過ぎのようです...

前もって感謝します、

スティーン

{
            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";
        }
4

2 に答える 2

2

それを複製することが道です。

これは次のように行います。

XDocument cachedDocument = ... get it from the cache ...
XDocument clone = new XDocument(cachedDocument);
... modify the clone
于 2012-05-16T12:10:28.230 に答える
2

新しい XDocument を作成します。キャッシュから取得した内容を、作成した内容にコピーします。

于 2012-05-16T12:08:37.753 に答える