这个框架到如今最大的变化立即就要出现了,哪就是对缓存的使用。由于系统常常要去读取数据库数据。可是大家知道。数据库的处理能力是有限的,所以对于一些数据量不大,可是又 须要常常去读取的功能来说。更好的方法就是使用缓存。 上面4的方法是不适用缓存的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JFrame.AccessCommon;
using System.Data.Objects.DataClasses;
using JFrame.Utility;
using System.Linq.Expressions;
using System.Reflection;
namespace JFrame.Dal
{
/// <summary>
/// 使用缓存进行数据更新,缓存临时仅仅支持lamda表达式
/// </summary>
/// <typeparam name="T"></typeparam>
public class DalBaseDataCache<T> : AccessBase<T> where T : EntityObject
{
static DataCacheCommon<T> CacheCommon; //DataCacheCommon<T>.Instance(GetListByWhere);
public DalBaseDataCache()
: base(ConnectionString: DataBaseConnectionString.GetConnectionString())
{
CacheCommon = DataCacheCommon<T>.Instance(GetListByWhere);
}
public DalBaseDataCache(string PrimaryKey = "", string strTableName = "", string QueryColums = "")
: base(ConnectionString: DataBaseConnectionString.GetConnectionString(), PrimaryKey: PrimaryKey, strTableName: strTableName, QueryColums: QueryColums)
{
CacheCommon = DataCacheCommon<T>.Instance(GetListByWhere);
}
/// <summary>
/// 添加单个实体
/// </summary>
/// <param name="t"></param>
public virtual void AddEntity(T t)
{
Data.InsertEntity<T>(t);
CacheCommon.Add(t);
}
/// <summary>
/// 获取单个实体
/// </summary>
/// <param name="query">查询条件</param>
/// <returns></returns>
public virtual T GetSingleEntity(Expression<Func<T, bool>> query)
{
return CacheCommon.GetDataList(query).FirstOrDefault(); // Data.GetSingleEntity<T>(query);
}
public virtual List<T> GetAllEntityByPage(Expression<Func<T, bool>> query, PagingInfo PageInfo, Func<T, object> orderByDesc)
{
return CacheCommon.GetDataList(query, PageInfo, orderByDesc);
}
/// <summary>
/// 获取单个实体
/// </summary>
/// <typeparam name="T">泛型类型參数</typeparam>
/// <param name="express">查询条件</param>
/// <returns></returns>
public virtual T TryGetSingleEntity(Expression<Func<T, bool>> query)
{
try
{
return CacheCommon.GetDataList(query).FirstOrDefault();// Data.GetSingleEntity<T>(query);
}
catch (Exception ex)
{
return null;
}
}
/// <summary>
/// 改动单个实体
/// </summary>
/// <param name="t"></param>
public virtual void UpdateEntity(T t)
{
Type type = typeof(T);
PropertyInfo[] infos = type.GetProperties();
PropertyInfo info = infos.Where(p => p.Name == _PrimaryKey).FirstOrDefault();
object value = null;
if (info != null)
{
value = info.GetValue(t, null);
}
else
{
return;
}
Data.Update<T>(t, _PrimaryKey, value);
CacheCommon.NextDataUpdate = DateTime.Now;
}
/// <summary>
/// 更新实体。不会从数据库同步
/// </summary>
/// <param name="t"></param>
/// <param name="query"></param>
public virtual void UpdateEntity(T t, Expression<Func<T, bool>> query)
{
Type type = typeof(T);
PropertyInfo[] infos = type.GetProperties();
PropertyInfo info = infos.Where(p => p.Name == _PrimaryKey).FirstOrDefault();
object value = null;
if (info != null)
{
value = info.GetValue(t, null);
}
else
{
return;
}
Data.Update<T>(t, _PrimaryKey, value);
CacheCommon.Update(t, query.Compile());
}
/// <summary>
/// 依据条件删除信息
/// </summary>
/// <param name="query">条件</param>
public virtual void Delete(Expression<Func<T, bool>> query)
{
Data.DeleteEntitys<T>(query);
CacheCommon.Delete(query);
}
/// <summary>
/// (缓存中)依据条件获取相关监測信息表
/// </summary>
/// <param name="strWhere">Where条件</param>
/// <returns>数据集合</returns>
public virtual List<T> GetListByWhere(Expression<Func<T, bool>> query)
{
//new System.Linq.Expressions.Expression.BinaryExpressionProxy (query.Body)
//(new System.Linq.Expressions.Expression.BinaryExpressionProxy((new System.Linq.Expressions.Expression.LambdaExpressionProxy(query)).Body)).DebugView
return CacheCommon.GetDataList(query);
}
/// <summary>
/// 依据条件获取相关监測信息表
/// </summary>
/// <param name="strWhere">Where条件</param>
/// <returns>数据集合</returns>
public virtual List<T> GetALLCacheList()
{
return CacheCommon.GetDataList();
}
}
}