如何缓存数据集以停止到db的往返行程? [英] How do I cache a dataset to stop round trips to db?
问题描述
我只想填写数据集一次,然后在DataTable和派生的DataView上工作,直到页面被卸载。
如何最好地缓存DataSet在我的DAL,以便它只填充PageLoad?我把它放在一个Cache对象,一个静态成员变量,一个属性...我没有任何花哨的实体模型或ORM,这是.NET 1.1。感谢提前。
您的DAL会被其他应用程序使用吗?如果没有,那么您可以在缓存
或会话 c中缓存
DataSet
code>取决于存储所需的功能。
会话
将特定于用户并在用户的会话过期时清理,这意味着数据可能比所需的时间长得多。 有关会议的更多信息
缓存
很好,因为它会自动过期(如果搜索数据不会频繁更改,请使用滑动到期),您可以存储搜索条件,以便其他用户可以利用搜索,这可以将您的呼叫保存到多个用户的数据库。使用 Session
可以使用多个用户访问这些数据。 有关缓存的更多信息
如果您打算在其他apap中使用DAL,您可能希望应用程序自己执行缓存。
您只需要一个包装器,如: / p>
//考虑使用这个psuedo代码使用Cache
public DataSet GetMySearchData(string search)
{
//如果它在我的缓存已经(通知搜索条件是缓存键)
string cacheKey =搜索+搜索;
if(Cache [cacheKey]!= null)
{
return(DataSet)(Cache [cacheKey]);
}
else
{
DataSet result = yourDAL.DoSearch(search);
缓存[cacheKey] .Insert(result); //这里需要更多的参数...
返回结果;
}
}
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.
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;
}
}
这篇关于如何缓存数据集以停止到db的往返行程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!