简体   繁体   中英

C# Dictionary As Application Cache

I don't even know if I am on the right track but I gave a shot to Dictionary to store big and static data (like multi language data) and use it in the whole application. This example goes for a WinForms app.

In this scenario, I have a table which holds various parameters, divided into groups. The table is made of following fields: GROUP_CODE, PARAMETER_KEY, PARAMETER_VALUE... It simply holds lots of values as in this example:

GROUP_CODE: 'MAILING_INFO'
PARAMETER_KEY: 'SendMailAfterProcessIsDone'
PARAMETER_VALUE: 'a@b.com'

There is no problem or anything to hold and get the data from the database. My only problem is that how exactly I am going to handle this data...

My ParameterValues class holds the following, same fields I select from the 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; }
    }
}

In another class, called CacheHelper , I am trying to put this object with the GROUP_CODE value as key into a Dictionary. (Hope that made sense)

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
    }
}

I have to group it by GROUP_CODE values as from the database since Dic's "key" has to be unique... I know....

Since I think it was not clear enough, I tried to demonstrate what I am actually trying to do in the following image...

我想要的是这样的东西...

I simply cannot put it there groupped. Please someone show me a way... And also I'd like to hear your ideas if this is a good way to use this as application caching for really large data (thousands of rows I mean).

Many thanks!

I think I get where you are having a problem, please note the syntax may be not 100% as I am typing from memory.

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
  }
}

I am not going to talk about caching solutions, as there are some out there and I am not particularly experienced with any.

But If you want to try static Dictionary solution, you could try grouping your data using 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM