如何缓存数据集以停止与数据库的往返?

发布于 2024-09-19 03:58:18 字数 274 浏览 6 评论 0原文

我正在 ASP.NET 1.1 页面中用 C# 创建搜索结果页面。在我的数据层中,我有一个 DataSet,用于存储普通旧式 ADO.NET 存储过程调用的结果。该数据集有两个数据表,我使用 DataVIew 对列进行过滤和排序。 我只想填充数据集一次,然后处理 DataTable 和派生的 DataView,直到页面卸载。 我应该如何最好地将数据集缓存在 DAL 中,以便仅填充 PageLoad?我是否将其放入 Cache 对象、静态成员变量、属性中...我没有任何花哨的实体模型或 ORM,这是 .NET 1.1。提前致谢。

I am creating a search results page in C# in an ASP.NET 1.1 page. In my data layer I have a DataSet that stores the result of a plain old ADO.NET stored procedure call. The DataSet has two DataTables and I'm using a DataVIew to filter and sort the columns.
I only want to fill the dataset once, and then work on the DataTables and derived DataView until the page is unloaded.
How best should I cache the DataSet in my DAL so that it is filled only PageLoad? Do i put it in a Cache object, a static member variable, a property...I don't have any fancy entity models or ORM, this is .NET 1.1. Thanks in advance.

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

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

发布评论

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

评论(3

对风讲故事 2024-09-26 03:58:18

您的 DAL 会被其他应用程序使用吗?如果没有,那么您可以将 DataSet 的缓存嵌入到 CacheSession 中,具体取决于存储需要哪些功能。

Session 将特定于用户,并在用户会话过期时被清理,这意味着数据可能比所需的时间长得多。 有关会话的详细信息

缓存很好,因为它会自动过期(如果搜索数据不会经常更改,请使用滑动过期),并且您可以用它存储搜索条件,以便其他用户也可以利用搜索,这将节省您对数据库可能由多个用户使用。与使用 Session 相比,多个用户能够访问此数据是一个很大的优势。 有关缓存的详细信息

如果您打算在其他应用程序中使用 DAL ,您可能希望应用程序自行进行缓存。

你只需要一个像这样的包装:

// Consider this psuedo code for using Cache
public DataSet GetMySearchData(string search)
{
    // if it is in my cache already (notice search criteria is the cache key)
    string cacheKey = "Search " + search;
    if (Cache[cacheKey] != null)
    {
        return (DataSet)(Cache[cacheKey]);
    }
    else
    {
        DataSet result = yourDAL.DoSearch(search);
        Cache[cacheKey].Insert(result);  // There are more params needed here...
        return result;
    }
}

Will your DAL be used by any other applications? If not then you can imbed the caching of the DataSet in the Cache or Session depending on what features are need of the storage.

Session will be specific to the user and get cleaned up when the user's session expires which means the data might be around a lot longer than required. More info on Session

Cache is nice because it will auto expire (use sliding expiry if the search data will not change often) and you can store the search criteria with it so that other users can leverage the search as well which will save you calls to the DB by multiple users possibly. Multiple user's being able to access this data is a big advantage over using Session. More info on Cache

If you plan to use your DAL in other apap, you might want the application to do the caching itself.

You just need a wrapper like:

// Consider this psuedo code for using Cache
public DataSet GetMySearchData(string search)
{
    // if it is in my cache already (notice search criteria is the cache key)
    string cacheKey = "Search " + search;
    if (Cache[cacheKey] != null)
    {
        return (DataSet)(Cache[cacheKey]);
    }
    else
    {
        DataSet result = yourDAL.DoSearch(search);
        Cache[cacheKey].Insert(result);  // There are more params needed here...
        return result;
    }
}
淡墨 2024-09-26 03:58:18

有一些适用于 .net 1.1 的 ORM,但如果您不想使用它们,我建议您在缓存中填充一些内容,以便您可以在需要时使其过期并重新获取数据。您还可以将其放入会话(如果数据是用户特定的)或应用程序(如果不是)对象中。

there are ORMs that work with .net 1.1, but if you don't want to use them, I would recommend either populating something in the Cache so that you can expire it if needed and re-get the data. You could also put it into the session (if the data is user specific) or application (if it is not) objects.

哭了丶谁疼 2024-09-26 03:58:18

如果这是每个用户的数据,并且不缓存大量数据并将其放入会话或类似数据中,那么您将严重阻碍站点的扩展能力。考虑随着您添加更多用户,内存中的信息量如何增长,以及当您需要使用多个站点服务器时会发生什么。

相反,请修改该过程,以便您只能检索需要显示的数据页。

如果这是供所有用户使用的数据,那么一定要缓存它。您可以使用 asp.net 缓存来实现这一点。

If this is per user data don't cache a large amount of data putting it into session or similar, you are seriously hindering the ability of your site to scale. Consider how the amount of info in memory grows as you add more users, and then what happens when you need to use more than a single site server.

Instead modify the procedure so you can only retrieve the page of data that you need to show.

If this is data that is used for all users, definitely cache it. You can use the asp.net Cache for that.

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