缓存

一、通过数据库的sysdate字段

public class CacheData
{

/// <summary>
/// 字典集合
/// </summary>
private static Dictionary<string, DataTable> dicStuData = new Dictionary<string, DataTable>()// <summary>

/// 根据班级查找班级学生信息
/// </summary>
/// <returns></returns>
public static DataTable GetStuInfoByName(string name)
{
DataTable dt=null;
//次日清空
if (dicStuData .TryGetValue(name, out dt)&&dt.Rows.Count>0)
{
DateTime cacheTime = Convert.ToDateTime(dt.Rows[0]["sysdate"]);
//缓存一天
if (cacheTime.Date != DateTime.Now.Date)
{
dicStuData [name] = null;
}
}

if (dt == null|| dt.Rows.Count == 0)
{
//查询学生信息
string sql = @"select  distinct  stuname,stuNo, address , sysdate from classInfo
where classname ='{0}'";
try
{

//查询数据库
dt=dicStuData [name] = DBContext.GetDataTable(sql);
}
catch (Exception ex)
{

//记录错误日志
Log.Error("根据班级名:["+name+"],查询学生信息异常:", ex);
}
}
return dt;
}

}

posted @ 2017-09-10 10:16  小痞子Yang  阅读(253)  评论(0编辑  收藏  举报