简体   繁体   中英

.net dictionary cache c#

Hi I want to make the code below run more efficiently using caching in .net. How do I get the contents on the dictionary cached so that when the buttSubmit_Click() is called the dictionary does not have to be redefined and cached data is used instead.

protected void buttSubmit_Click(object sender, EventArgs e)
{
    Dictionary<string, string> dict = new Dictionary<string, string>();
    dict.Add("rad1", "value1");
    dict.Add("rad2", "value2");
    dict.Add("rad3", "value3");
    dict.Add("rad4", "value4");

    string vValue;
    dict.TryGetValue(RadioButtonList.SelectedValue, out vValue);
    submitVote(vValue);
}

Well, frankly if it is that simple I'd be tempted to use a switch , but - assuming that is a simplification, maybe make it a field (in this case I've made it static , but that is up to you):

private static readonly Dictionary<string, string> dict
      = new Dictionary<string, string> {
    {"rad1", "value1"},
    {"rad2", "value2"},
    {"rad3", "value3"},
    {"rad4", "value4"},
};

protected void buttSubmit_Click(object sender, EventArgs e)
{
    string value;
    if(dict.TryGetValue(RadioButtonList.SelectedValue, out value))
    {
        submitVote(value);
    }
}

Declare and fill the Dictionary outside the method as a field.

Okay, I'll expand; as the static field is show in the other answer, here is the instance field:

protected Dictionary<string, string> dict = new Dictionary<string, string>();

public MyClass()
{
    dict.Add("rad1", "value1");
    dict.Add("rad2", "value2");
    dict.Add("rad3", "value3");
    dict.Add("rad4", "value4");
}

protected void buttSubmit_Click(object sender, EventArgs e)
{    
    string vValue;
    dict.TryGetValue(RadioButtonList.SelectedValue, out vValue);
    submitVote(vValue);
}

Choice depends on the use case. If neither is good, consider a if/else block.

To cache constant value simply avoid unnecessary initialization, something like:

Dictionary<string, string> dict = null;
protected void buttSubmit_Click(object sender, EventArgs e)
{
    if(dict == null)
    {
        dict = new Dictionary<string, string>();
        dict.Add("rad1", "value1");
        dict.Add("rad2", "value2");
        dict.Add("rad3", "value3");
        dict.Add("rad4", "value4");
    }

    string vValue;
    dict.TryGetValue(RadioButtonList.SelectedValue, out vValue);
    submitVote(vValue);
}

To cache dynamic data, you have to additionally provide a criteria to reset cache (making it null again) whenever data are changed and have to be re-cached.

Adding a global.asax to your project and adding your Dictionary to this as global static is a solution, too. This solution with global.asax has the (dis)advantage that it persistent for all users and requests - in the other solutions the Dictionary is created for every request newly. At first you have to create a global.asax in your web project (if not aleady in place):

在此处输入图片说明全球Asax 2

Then you have to edit your global.asax.cs file and add your dict:

public class Global : System.Web.HttpApplication
{

    public static readonly Dictionary<string, string> TEST_DICT
              = new Dictionary<string, string> {
            {"rad1", "value1"},
            {"rad2", "value2"},
            {"rad3", "value3"},
            {"rad4", "value4"},
        };

}

If you want to access this dict simple call Global.TEST_DICT:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label_TESTDICT.Text = "";
        List<String> keys = Global.TEST_DICT.Keys.ToList();
        foreach (String key in keys)
        {
            Label_TESTDICT.Text += key + ":" + Global.TEST_DICT[key] + "<br>"; 
        }
    }
}

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