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

发布于 2024-10-18 09:27:04 字数 864 浏览 4 评论 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”中执行此操作,如下所示


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

这很好;该操作仅执行一次,我可以在整个过程中使用数据,但是由于操作需要时间,因此我的页面将需要一些时间来加载。

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

谢谢, 拉胡尔

In my code I have a dropdown selection and upon selection from dropdown the code performs further processing and generates report/data.

Further, the entire program depends on data which is gathered from 3 different operation

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)

So, to make it efficient I am thinking to perform this operation only once and use the data for all the different selection from dropdown.

Question is how can I do so as below:

  1. I can't put it in "page_load" event because every time page loads the operations will carry out

  2. I can't put it inside "dropdownlist_selectedindexchanged" event because then it will be same as #1.

  3. I thought of doing it in "page_load" as below


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

This is fine; the operations gets performed only once and I can use the data throughout, but then my page will take time to load as the operations takes time.

Is there any other way I can achieve what I want? Please let me know.

Thanks,
Rahul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧城空念 2024-10-25 09:27:04

如果数据集不会更改,您可能可以在 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";
    }

}

If the data set will not change, you probably could manage to do it once at Application_Start().

Edit - something like this (typing from memory and away from VS, i do 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";
    }

}
甜味超标? 2024-10-25 09:27:04

缓存它。使用 CacheHelper从这里< /a>,你可以这样做:

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

Cache it. Using the CacheHelper class from here, you could do:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文