如何在缓存中存储的数据表重用呢? [英] How to store datatable in cache to reuse it?

查看:120
本文介绍了如何在缓存中存储的数据表重用呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序已经使用的通用处理器服务请求。

In my application i have used Generic handler to serve requests.

我想机制一样,如果第一次请求到达处理程序,它对服务器发出请求,然后缓存整个数据表,所以对于即将到来的请求,如果下一个请求的产品code在缓存中的数据表存在,不应该去服务器获取数据again..it应该检查数据表中的唯一数据。

I want mechanism like if the first time request comes to handler ,it makes a request to server and then Cache whole datatable, so for upcoming request if the next requested productcode is exist in the datatable of cache, it should not go to server for fetching data again..it should check data of datatable only.

所以,你能解释一下我如何可以设置高速缓存中的数据表。??

推荐答案

东西沿着这些路线?

public DataTable GetDataTableFromCacheOrDatabase()
{
   DataTable dataTable = HttpContext.Current.Cache["secret key"] as DataTable;
   if(dataTable == null)
   {
       dataTable = GetDataTableFromDatabase();
       HttpContext.Current.Cache["secret key"] = dataTable;
   }
   return dataTable;
}

您会需要一些机制来从缓存刷新的数据表,如果它的变化。例如,你可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.caching.sqlcachedependency.aspx\">SqlCacheDependency.

You're going to need some mechanism to flush the data table from the cache if it changes. For example you could use an SqlCacheDependency.

根据要求,清除缓存一个小时后,该表添加你会做:

As requested, to clear the cache an hour after the table is added you would do:

HttpContext.Current.Cache.Insert("secret key", dataTable, null, DateTime.Now.AddHours(1), System.Web.Caching.Cache.NoSlidingExpiration);

这篇关于如何在缓存中存储的数据表重用呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆