这个方法是数据缓存操作的基本类:它作用是,在加载的时候,从数据库把数据读取出来放在缓存里面,同时每隔一段时间,从数据库再读取一次,以保证数据的真实性。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;
using System.Linq.Expressions;
using System.Threading;
using JFrame.Utility;
using JFrame.Entity;
using System.Data.Objects;
namespace JFrame.Dal
{
public class DataCacheCommon<T> where T : class
{
private List<T> DataList = new List<T>();
private static object ulock = new object();
private DateTime nextDataUpdate = DateTime.Now;
/// <summary>
/// 每次从数据库更新的间隔为60分钟
/// </summary>
private int updateInterval = 60;
public delegate List<T> UpdateDataFunction();
/// <summary>
/// 实例化对象时,必须提供更新数据的方法
/// </summary>
/// <param name="?"></param>
private DataCacheCommon(UpdateDataFunction Function)
{
Type type = typeof(T);
new DalBaseData<SysRunLogs>().AddEntity(new SysRunLogs()
{
AddDateTime = DateTime.Now,
LoginInfo = "启动实例化 " + type.Name
});
OnTimeUpdateDataList(Function);
}
private DataCacheCommon()
{ }
private static DataCacheCommon<T> instance = null;
private static readonly object padlock = new object();
/// <summary>
/// 单例模式创建
/// </summary>
/// <param name="updateTime"></param>
/// <returns></returns>
public static DataCacheCommon<T> Instance(UpdateDataFunction updateTime)
{
if (instance == null)
{
lock (padlock)
{
if (instance == null)
{
instance = new DataCacheCommon<T>(updateTime);
}
}
}
return instance;
}
/// <summary>
/// 操作类型
/// </summary>
private enum OperType
{
Select = 1,
Update = 2,
Delete = 3,
Add = 4,
Set=5
}
/// <summary>
/// 更新实体
/// </summary>
/// <param name="model">新的实体对象</param>
/// <param name="query">原有的实体对象</param>
public void Update(T model, Func<T, bool> query)
{
DataOper(OperType.Update, model, query);
}
/// <summary>
/// 增加实体
/// </summary>
/// <param name="model"></param>
public void Add(T model)
{
DataOper(OperType.Add, model, null);
}
/// <summary>
/// 根据条件删除数据方法
/// </summary>
/// <param name="query"></param>
public void Delete(Func<T, bool> query)
{
DataOper(OperType.Delete, null, query);
}
/// <summary>
/// 根据尸体数据删除方法
/// </summary>
/// <param name="Entity"></param>
public void Delete(T Entity)
{
DataOper(OperType.Delete, Entity, null);
}
/// <summary>
/// 根据尸体数据删除方法
/// </summary>
/// <param name="Entity"></param>
public void Delete(Expression<Func<T, bool>> query)
{
DataOper(OperType.Delete, null, query.Compile());
}
public List<T> GetDataList(Func<T, bool> query)
{
return DataOper(OperType.Select, null, query);
}
public List<T> GetDataList(Expression<Func<T, bool>> query)
{
return DataOper(OperType.Select, null, query.Compile());
}
public List<T> GetDataList()
{
return DataOper(OperType.Select, null, null);
}
public List<T> GetDataList(Expression<Func<T, bool>> query, PagingInfo PageInfo, Func<T, object> orderByDesc)
{
List<T> DataWhere = DataOper(OperType.Select, null, query.Compile());
PageInfo.TotalRecord = DataWhere.Count();
PageInfo.TotalPage = PageInfo.TotalRecord / PageInfo.PageSize + 1;
return (from t in DataWhere
select t).OrderByDescending(orderByDesc).Skip(PageInfo.PageSize * (PageInfo.PageIndex - 1)).Take(PageInfo.PageSize).ToList();
}
/// <summary>
/// 下一次更新时间
/// </summary>
public DateTime NextDataUpdate
{
set
{
nextDataUpdate = value;
}
}
/// <summary>
/// 每次从数据更新时间,默认为60分钟更新一次
/// </summary>
public int UpdateInterval
{
get
{
return updateInterval;
}
set
{
updateInterval = value;
}
}
/// <summary>
/// 调用更新数据的线程方法
/// </summary>
/// <param name="Function"></param>
private void OnTimeUpdateDataList(UpdateDataFunction Function)
{
ParameterizedThreadStart ParStart = new ParameterizedThreadStart(UpdateData);
Thread myThread = new Thread(ParStart);
object o = Function;
myThread.Start(o);
}
/// <summary>
/// 数据定时更新的方法
/// </summary>
private void UpdateData(object Function)
{
while (true)
{
if (nextDataUpdate <= DateTime.Now)
{
List<T> NewData = (List<T>)((UpdateDataFunction)Function)();
DataOper(OperType.Set, null, null, NewData);
Type type = typeof(T);
new DalBaseData<SysRunLogs>().AddEntity(new SysRunLogs()
{
AddDateTime = DateTime.Now,
LoginInfo = "更新缓存 " + type.Name + " 数据 " + DataList.Count() + " 条!"
});
nextDataUpdate = nextDataUpdate.AddMinutes(UpdateInterval);// UpdateInterval*60*1000;
}
Thread.Sleep(1000);
}
}
/// <summary>
/// 公共列表操作类
/// </summary>
/// <param name="OperType">操作类型(select update delete add/insert)</param>
/// <param name="model">实体对象 修改和增加 删除的时候必须传入</param>
/// <param name="query">当为select时候的查询条件</param>
/// <returns>当为select则返回 list列表 否则不进行返回</returns>
private List<T> DataOper(OperType operType, T model, Func<T, bool> query,List<T> SetList=null)
{
if (DataList == null)
{
DataList = new List<T>();
}
if (operType == OperType.Select)
{
if (query == null)
{
return DataList;
}
List<T> SelectList = new List<T>();
foreach (var item in DataList.Where(query).ToList())
{
SelectList.Add(Copy(item));
}
return SelectList;
}
lock (ulock)
{
switch (operType)
{
case OperType.Update:
T tmodel = DataList.Where(query).FirstOrDefault();
if (tmodel != null)
{
tmodel = Copy(model);
}
return null;
case OperType.Delete:
if (query != null)
{
List<T> DeleteList = DataList.Where(query).ToList();
if (DeleteList != null)
{
foreach (var item in DeleteList)
{
DataList.Remove(item);
}
}
}
else if (model != null)
{
DataList.Remove(model);
}
return null;
case OperType.Add:
DataList.Add(model);
return null;
case OperType.Set:
DataList = new List<T>();
foreach (var item in SetList)
{
DataList.Add(Copy(item));
}
return null;
}
return null;
}
}
/// <summary>
/// 数据复制
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entity"></param>
/// <returns></returns>
public T Copy<T>(T entity) where T : class
{
Type type = typeof(T);
T newEntity = System.Activator.CreateInstance<T>(); ;
object propertyValue = null;
PropertyInfo[] properties1 = newEntity.GetType().GetProperties();
foreach (PropertyInfo property in properties1)
{
propertyValue = null;
if (null != property.GetSetMethod())
{
PropertyInfo entityProperty =
entity.GetType().GetProperty(property.Name);
if (entityProperty.PropertyType.BaseType ==
Type.GetType("System.ValueType") ||
entityProperty.PropertyType ==
Type.GetType("System.String"))
propertyValue = entity.GetType().GetProperty(property.Name).GetValue(entity, null);
if (propertyValue == null)
{
propertyValue = entity.GetType().GetProperty(property.Name).GetValue(entity, null);
}
if (null != propertyValue)
{
try
{
string Name = property.Name;// "Reference";
if (Name.IndexOf("Reference") < 0)
{
property.SetValue(newEntity, propertyValue, null);
}
}
catch (Exception ex) { }
}
}
}
return newEntity;
}
}
}