2014-09-04 85 views
0

我已经在我的类库中实现了缓存,并且遇到问题。我所做的是,我传递了一个值,我需要从XML文件读取,并将返回节点和值。我已经使用字典来存储子节点作为键值对。请参阅我的XML例子缓存值被覆盖?

<?xml version="1.0" encoding="utf-8"?> 
<Settings> 
    <Test1> 
     <CompanyDetails> 
     <Name>Test1</Name> 
     <Address1>Add1</Address1> 
     <Address2>Add2</Address2> 
     <Address3>Add3</Address3> 
     <Phone>1111</Phone> 
     </CompanyDetails> 
     <Email> 
     <Contact>[email protected]</Contact> 
     </Email> 
     <AdminUsers> 
     <User1>user1</User1> 
     <User2>user2</User2> 
     </AdminUsers> 
    </Test1> 
    </Settings> 

请参考下面

using System.Web; 
public Dictionary<string, string> GetDataFromSettings(string strValues) 
{ 
Dictionary<string, string> dictValus = new Dictionary<string, string>(); 
    XmlDocument xmlDoc = new XmlDocument(); 
    string xmlFilePath = "D:\\Test.xml"; 
    xmlDoc.Load(xmlFilePath); 
    if (!(xmlDoc == null)) 
    { 
     string[] splitValues = strValues.Split(new char[] { ',' }); 
     if (splitValues.Length > 1) 
     { 
      Dictionary<string, string> strDictValues = new Dictionary<string, string>(); 
      foreach (string strName in splitValues) 
      { 
       if (HttpRuntime.Cache[strName.Trim()] == null) 
       { 
        strDictValues.Clear(); 
        XmlNodeList xmlList = xmlDoc.SelectNodes("Settings/Test1/" + strName.Trim()); 

        foreach (XmlNode node in xmlList) 
        { 
         if (node.FirstChild.Name.ToString() != node.LastChild.Name.ToString()) 
         { 
          foreach (XmlNode childNode in node.ChildNodes) 
          { 
           strDictValues.Add(strName.Trim() + "." + childNode.LocalName, childNode.InnerText); 
          } 
         } 
         else 
         { 
          strDictValues.Add(strName.Trim() + "." + node.FirstChild.LocalName, node.InnerText); 
         } 
        } 
        HttpRuntime.Cache.Insert(strName.Trim(), strDictValues,new System.Web.Caching.CacheDependency(xmlFilePath)); 
       } 
       else 
       { 
        Dictionary<string, string> tmpDict = new Dictionary<string, string>(); 
        tmpDict = (Dictionary<string, string>)HttpRuntime.Cache[strName.Trim()]; 

        foreach (var tmpVal in tmpDict) 
        { 
         strDictValues.Add(tmpVal.Key, tmpVal.Value); 
        } 
       } 

       foreach (var tmpStrDict in strDictValues) 
       { 
        dictValus.Add(tmpStrDict.Key, tmpStrDict.Value); 
       } 
      } 
     } 
    } 
    return dictValus; 
    } 

我的代码在我的XML上述例子中,我将传递字符串为“CompanyDetails,通过电子邮件将”在功能上面,我分裂字符串,然后将每个字符串的值分别存储在缓存中。当我第一次运行时,“CompanyDetails”下的值被存储在名为“CompanyDetails”的缓存中,并且当它用于节点“Email”时,缓存[“CompanyDetails”]被覆盖,并且缓存[“Email “]被创建并且它们都具有相同的值。这是不正确的。我无法找到它发生的原因。

谢谢
Jollyguy

+0

你为什么这样做'splitValues.Length> 1'而不是'splitValues.Length> 0'?检查'> 1'意味着您不会缓存单个密钥的值。 – dbc 2014-09-04 14:44:54

回答

0

的问题是,当你正在构建的缓存,明确和重用字典,而不是为每个strName键的新字典。由于您清除的字典是添加到缓存中的字典 - 不是复制字典,但只保存对它的引用 - 您清除以前的值。

尝试以下操作(注意我重构稍低版本便于调试,并消除冗余呼叫“strName.Trim()”):

public Dictionary<string, string> GetDataFromSettings(TextReader reader, string xmlFilePath, string strValues) 
    { 
     XmlDocument xmlDoc = new XmlDocument(); 
     xmlDoc.Load(reader); 
     return GetDataFromSettings(xmlDoc, xmlFilePath, strValues); 
    } 

    public Dictionary<string, string> GetDataFromSettings(XmlDocument xmlDoc, string xmlFilePath, string strValues) 
    { 
     Dictionary<string, string> dictValus = new Dictionary<string, string>(); 
     if (!(xmlDoc == null)) 
     { 
      string[] splitValues = strValues.Split(new char[] { ',' }); 
      if (splitValues.Length > 1) 
      { 
       foreach (string strName in splitValues.Select((s) => s.Trim())) 
       { 
        Dictionary<string, string> strDictValues = new Dictionary<string, string>(); 
        if (HttpRuntime.Cache[strName] == null) 
        { 
         XmlNodeList xmlList = xmlDoc.SelectNodes("Settings/Test1/" + strName); 

         foreach (XmlNode node in xmlList) 
         { 
          if (node.FirstChild.Name.ToString() != node.LastChild.Name.ToString()) 
          { 
           foreach (XmlNode childNode in node.ChildNodes) 
           { 
            strDictValues.Add(strName + "." + childNode.LocalName, childNode.InnerText); 
           } 
          } 
          else 
          { 
           strDictValues.Add(strName + "." + node.FirstChild.LocalName, node.InnerText); 
          } 
         } 
         HttpRuntime.Cache.Insert(strName, strDictValues, new System.Web.Caching.CacheDependency(xmlFilePath)); 
        } 
        else 
        { 
         var tmpDict = (Dictionary<string, string>)HttpRuntime.Cache[strName]; 
         foreach (var tmpVal in tmpDict) 
         { 
          strDictValues.Add(tmpVal.Key, tmpVal.Value); 
         } 
        } 

        foreach (var tmpStrDict in strDictValues) 
        { 
         dictValus.Add(tmpStrDict.Key, tmpStrDict.Value); 
        } 
       } 
      } 
     } 
     return dictValus; 
    } 
+0

感谢您的澄清。 – Jollyguy 2014-09-05 14:44:28