简体   繁体   English

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

[英]How do I cache a dataset to stop round trips to db?

I am creating a search results page in C# in an ASP.NET 1.1 page. 我正在ASP.NET 1.1页面的C#中创建搜索结果页面。 In my data layer I have a DataSet that stores the result of a plain old ADO.NET stored procedure call. 在我的数据层中,我有一个DataSet,用于存储普通的ADO.NET存储过程调用的结果。 The DataSet has two DataTables and I'm using a DataVIew to filter and sort the columns. DataSet有两个DataTables,我正在使用DataVIew来对列进行过滤和排序。 I only want to fill the dataset once, and then work on the DataTables and derived DataView until the page is unloaded. 我只想填充一次数据集,然后处理DataTables和派生的DataView,直到页面被卸载。 How best should I cache the DataSet in my DAL so that it is filled only PageLoad? 我应该如何最好地将DataSet缓存在DAL中,以便仅填充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. 我是否将其放入Cache对象,静态成员变量,属性...我没有任何精美的实体模型或ORM,这是.NET 1.1。 Thanks in advance. 提前致谢。

Will your DAL be used by any other applications? 您的DAL是否会被其他任何应用程序使用? 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. 如果没有,那么你可以在嵌入的缓存DataSetCacheSession取决于所需要存储的哪些功能。

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. Session将特定于用户,并在用户会话到期时进行清理,这意味着数据可能比所需的时间长得多。 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. Cache很不错,因为它会自动过期(如果搜索数据不会经常更改,请使用滑动过期),并且您可以将搜索条件与其一起存储,以便其他用户也可以利用搜索,这将使您对数据库的调用节省多个用户可能。 Multiple user's being able to access this data is a big advantage over using Session . 与使用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. 如果您打算在其他apap中使用DAL,则可能希望应用程序自己进行缓存。

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

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. .net 1.1中有一些ORM,但是如果您不想使用它们,我建议您在Cache中填充一些内容,以便您可以在需要时使它过期并重新获取数据。 You could also put it into the session (if the data is user specific) or application (if it is not) objects. 您也可以将其放入会话(如果数据是特定于用户的)或应用程序(如果不是用户)的对象中。

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. 您可以为此使用asp.net缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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