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

public List TrafficLog
{
get
{
List result = new List();
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()
join b in DataHelper.Set()
on a.OwnerId equals b.User_ID
where b.CompanyId == LoginUserCompanyID //当前登录人的公司
&& a.CreateTime >= monthNow            //当前月
&& b.State == user_state
group a by new { a.OwnerId, b.User_Name } into c
select new DataSource
{
User_Name = c.Key.User_Name,
Data3G = c.Sum(p => p.G3_Increment),
};


// 总共个数
var appCount = trafficLog.Count();
List chart_source = new List();
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;
return result;
}
set
{
Session["TrafficLog"] = value;
}
}




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

你可能感兴趣的:(C#.NET)