数据量较大时,把数据放入缓存中的处理办法

来源:互联网 发布:远程网络教育学费 编辑:程序博客网 时间:2024/05/17 09:03

public List<DataSource> TrafficLog{get{List<DataSource> result = new List<DataSource>();if (Session["TrafficLog"] == null || (DateTime.Now - (DateTime)Session["Refunsh"]).Minutes > 10){//默认查询当前月的数据       var monthNow = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM"));int user_state = (int)User_StateEnum.No;//有效用户var trafficLog = from a in DataHelper.Set<T_WebAppTrafficLog>()join b in DataHelper.Set<T_User>()on a.OwnerId equals b.User_IDwhere b.CompanyId == LoginUserCompanyID //当前登录人的公司&& a.CreateTime >= monthNow            //当前月&& b.State == user_stategroup a by new { a.OwnerId, b.User_Name } into cselect new DataSource{User_Name = c.Key.User_Name,Data3G = c.Sum(p => p.G3_Increment),};// 总共个数var appCount = trafficLog.Count();List<DataSource> chart_source = new List<DataSource>();if (appCount > 0)  //是否显示图表{if (appCount > topView){appCount = topView;}var aa = trafficLog.Select(p => new DataSource(){User_Name = p.User_Name,Data3G = p.Data3G,}).ToList().OrderByDescending(p => p.Data3G).Take(appCount);chart_source = aa.Select(p => new DataSource{User_Name = p.User_Name,Data3G = Decimal.Round(Convert.ToDecimal(p.Data3G), 2),}).ToList();}Session["TrafficLog"] = chart_source.ToList();Session["Refunsh"] = DateTime.Now;  //刷新目录时间间隔为10分钟}result = Session["TrafficLog"] as List<DataSource>;return result;}set{Session["TrafficLog"] = value;}}




每隔10分钟缓存一次,不需要每次加载的时候都从数据库中抓取~~

1 0