需要关于C#编码的建议(效率)

时间:2011-02-22 18:01:47

标签: asp.net c#-2.0

在我的代码中,我有一个下拉选项,从下拉列表中选择后,代码会执行进一步处理并生成报告/数据。

此外,整个程序依赖于从3种不同操作中收集的数据

Operation1: processing a text files of size of size > 6MB

Operation2: SQL Query to a DB (Query takes around 1 minute)

Operation 3: HTTP POST request to server (The main costliest part of the programe)

所以,为了提高效率,我想只执行一次这个操作,并使用数据从下拉列表中选择所有不同的选项。

问题是我怎么能这样做:

  1. 我不能把它放在“page_load”事件中,因为每次页面加载操作都会执行

  2. 我不能把它放在“dropdownlist_selectedindexchanged”事件中,因为它会与#1相同。

  3. 我想在“page_load”中执行此操作,如下所示


  4. void Page_Load(object sender, EventArgs e)
    {
        if(!ispostback)
        {
            Operation1();
            Operation2();
            Operation3();
        }
    }
    

    这很好;操作只执行一次,我可以使用整个数据,但随着操作需要时间,我的页面将花费时间加载。

    还有其他方法可以实现我想要的吗?请告诉我。

    谢谢, 拉胡

2 个答案:

答案 0 :(得分:1)

如果数据集不会更改,您可能会设法在Application_Start()处执行一次。

编辑 - 这样的事情(从内存输入,远离VS,我做VB):

Protected void page_load(object sender, eventargs e)
{
    // the name can be anything
    if (!System.Web.HttpContext.Current.Session["data_cache_filled"])
    {
        // code to fill the cache.
        // ...

        //mark it as filled
        System.Web.HttpContext.Current.Session["data_cache_filled"] = "yes";
    }

}

答案 1 :(得分:1)

缓存它。使用CacheHelperfrom here,您可以执行以下操作:

internal List<Employee> Operation1()
{
  List<Employee> employeeData;

  if (!CacheHelper.Get("employeeData", out employeeData))
  {
    employeeData = (from x in db.Employees select x).ToList(); // or whatever

    CacheHelper.Add(employeeData, "employeeData");
  }

  return employeeData;
}