C# 字典作为应用程序缓存
C# Dictionary As Application Cache
我什至不知道我是否走在正确的轨道上,但我试过使用 Dictionary 来存储大数据和静态数据(如多语言数据)并在整个应用程序中使用它。此示例适用于 WinForms 应用程序。
在这种情况下,我有一个 table 包含各种参数,分为几组。 table 由以下字段组成:GROUP_CODE、PARAMETER_KEY、PARAMETER_VALUE...它只是包含很多值,如本例所示:
GROUP_CODE: 'MAILING_INFO'
PARAMETER_KEY: 'SendMailAfterProcessIsDone'
PARAMETER_VALUE: 'a@b.com'
保存和从数据库中获取数据没有任何问题。我唯一的问题是我将如何处理这些数据...
我的 ParameterValues class 包含以下与 select 来自 table...
相同的字段
public class ParameterValues
{
private string groupCode;
private string parameterKey;
private string parameterValue;
public string GroupCode
{
get { return groupCode; }
set { groupCode = value; }
}
public string ParameterKey
{
get { return parameterKey; }
set { parameterKey = value; }
}
public string ParameterValue
{
get { return parameterValue; }
set { parameterValue = value; }
}
}
在另一个名为 CacheHelper 的 class 中,我试图将此对象与 GROUP_CODE 值作为键放入字典中。 (希望这是有道理的)
public class CacheHelper
{
public Dictionary<string, ParameterValues> LoadParameterCache2()
{
//Dictionary<string, ParameterValues> objList = new Dictionary<string, ParameterValues>();
Dictionary<string, List<ParameterValues>> objList = new Dictionary<string, List<ParameterValues>>();
//call the values from the database blah blah
while (rdr.Read())
{
ParameterValues cacheObj = new ParameterValues();
cacheObj.ParameterKey = rdr.GetString("PARAMETER_KEY");
cacheObj.ParameterValue = rdr.GetString("PARAMETER_VALUE");
objList.Add(rdr.GetString("GROUP_CODE"), /*List ParameterValues goes here blah blah*/);
}
return objList; //and finally return it to the main application thread
}
}
我必须根据来自数据库的 GROUP_CODE 值对其进行分组,因为 Dic 的 "key" 必须是唯一的...我知道....
因为我觉得不够清楚,所以我尝试在下图中演示我实际要做什么...
我根本无法将它分组。请有人告诉我一种方法...如果这是将其用作真正大数据(我的意思是数千行)的应用程序缓存的好方法,我也想听听您的想法。
非常感谢!
我想我明白了您遇到的问题,请注意语法可能不是 100%,因为我是凭记忆输入的。
public class CacheHelper
{
public Dictionary<string, List<ParameterValues>> LoadParameterCache2()
{
var objList = new Dictionary<string, List<ParameterValues>>();
//call the values from the database blah blah
while (rdr.Read())
{
var cacheObj = new ParameterValues();
cacheObj.ParameterKey = rdr.GetString("PARAMETER_KEY");
cacheObj.ParameterValue = rdr.GetString("PARAMETER_VALUE");
//here is where you need to change things.
var groupCode = rdr.GetString("GROUP_CODE");
if (objList.ContainsKey(groupCode) == false)
{
objList.Add(groupCode, new List<ParameterValues> {cacheObj});
}
else
{
objList[groupCode].Add(cacheObj);
}
}
return objList; //and finally return it to the main application thread
}
}
我不打算谈论缓存解决方案,因为有一些解决方案,但我对任何一个都不是特别有经验。
但是如果您想尝试静态字典解决方案,您可以尝试使用 Linq 对数据进行分组:
public Dictionary<string, List<ParameterValues>> LoadParameterCache2()
{
List<KeyValuePair<string, ParameterValues>> dataValues = new List<KeyValuePair<string, ParameterValues>>();
// Your code to produce the DataReader
DataTableReader rdr = GetDataReader();
while (rdr.Read())
{
ParameterValues cacheObj = new ParameterValues();
cacheObj.ParameterKey = (string)rdr["PARAMETER_KEY"];
cacheObj.ParameterValue = (string)rdr["PARAMETER_VALUE"];
KeyValuePair<string, ParameterValues> dataValue = new KeyValuePair<string, ParameterValues>((string)rdr["GROUP_CODE"], cacheObj);
dataValues.Add(dataValue);
}
Dictionary<string, List<ParameterValues>> objList = dataValues.GroupBy(d => d.Key).ToDictionary(k => k.Key, v => v.Select(i => i.Value).ToList());
return objList;
}
我什至不知道我是否走在正确的轨道上,但我试过使用 Dictionary 来存储大数据和静态数据(如多语言数据)并在整个应用程序中使用它。此示例适用于 WinForms 应用程序。
在这种情况下,我有一个 table 包含各种参数,分为几组。 table 由以下字段组成:GROUP_CODE、PARAMETER_KEY、PARAMETER_VALUE...它只是包含很多值,如本例所示:
GROUP_CODE: 'MAILING_INFO'
PARAMETER_KEY: 'SendMailAfterProcessIsDone'
PARAMETER_VALUE: 'a@b.com'
保存和从数据库中获取数据没有任何问题。我唯一的问题是我将如何处理这些数据...
我的 ParameterValues class 包含以下与 select 来自 table...
相同的字段public class ParameterValues
{
private string groupCode;
private string parameterKey;
private string parameterValue;
public string GroupCode
{
get { return groupCode; }
set { groupCode = value; }
}
public string ParameterKey
{
get { return parameterKey; }
set { parameterKey = value; }
}
public string ParameterValue
{
get { return parameterValue; }
set { parameterValue = value; }
}
}
在另一个名为 CacheHelper 的 class 中,我试图将此对象与 GROUP_CODE 值作为键放入字典中。 (希望这是有道理的)
public class CacheHelper
{
public Dictionary<string, ParameterValues> LoadParameterCache2()
{
//Dictionary<string, ParameterValues> objList = new Dictionary<string, ParameterValues>();
Dictionary<string, List<ParameterValues>> objList = new Dictionary<string, List<ParameterValues>>();
//call the values from the database blah blah
while (rdr.Read())
{
ParameterValues cacheObj = new ParameterValues();
cacheObj.ParameterKey = rdr.GetString("PARAMETER_KEY");
cacheObj.ParameterValue = rdr.GetString("PARAMETER_VALUE");
objList.Add(rdr.GetString("GROUP_CODE"), /*List ParameterValues goes here blah blah*/);
}
return objList; //and finally return it to the main application thread
}
}
我必须根据来自数据库的 GROUP_CODE 值对其进行分组,因为 Dic 的 "key" 必须是唯一的...我知道....
因为我觉得不够清楚,所以我尝试在下图中演示我实际要做什么...
我根本无法将它分组。请有人告诉我一种方法...如果这是将其用作真正大数据(我的意思是数千行)的应用程序缓存的好方法,我也想听听您的想法。
非常感谢!
我想我明白了您遇到的问题,请注意语法可能不是 100%,因为我是凭记忆输入的。
public class CacheHelper
{
public Dictionary<string, List<ParameterValues>> LoadParameterCache2()
{
var objList = new Dictionary<string, List<ParameterValues>>();
//call the values from the database blah blah
while (rdr.Read())
{
var cacheObj = new ParameterValues();
cacheObj.ParameterKey = rdr.GetString("PARAMETER_KEY");
cacheObj.ParameterValue = rdr.GetString("PARAMETER_VALUE");
//here is where you need to change things.
var groupCode = rdr.GetString("GROUP_CODE");
if (objList.ContainsKey(groupCode) == false)
{
objList.Add(groupCode, new List<ParameterValues> {cacheObj});
}
else
{
objList[groupCode].Add(cacheObj);
}
}
return objList; //and finally return it to the main application thread
}
}
我不打算谈论缓存解决方案,因为有一些解决方案,但我对任何一个都不是特别有经验。
但是如果您想尝试静态字典解决方案,您可以尝试使用 Linq 对数据进行分组:
public Dictionary<string, List<ParameterValues>> LoadParameterCache2()
{
List<KeyValuePair<string, ParameterValues>> dataValues = new List<KeyValuePair<string, ParameterValues>>();
// Your code to produce the DataReader
DataTableReader rdr = GetDataReader();
while (rdr.Read())
{
ParameterValues cacheObj = new ParameterValues();
cacheObj.ParameterKey = (string)rdr["PARAMETER_KEY"];
cacheObj.ParameterValue = (string)rdr["PARAMETER_VALUE"];
KeyValuePair<string, ParameterValues> dataValue = new KeyValuePair<string, ParameterValues>((string)rdr["GROUP_CODE"], cacheObj);
dataValues.Add(dataValue);
}
Dictionary<string, List<ParameterValues>> objList = dataValues.GroupBy(d => d.Key).ToDictionary(k => k.Key, v => v.Select(i => i.Value).ToList());
return objList;
}