现在的位置: 首页 > 综合 > 正文

动软.NET生成器的缓存设计

2012年01月20日 ⁄ 综合 ⁄ 共 2877字 ⁄ 字号 评论关闭
using System;
using System.Data;
using System.Collections.Generic;
using LTP.Common;
using Maticsoft.Model;
namespace Maticsoft.BLL
{
	/// <summary>
	/// 业务逻辑类Product 的摘要说明。
	/// </summary>
	public class Product
	{
		private readonly Maticsoft.DAL.Product dal=new Maticsoft.DAL.Product();
		public Product()
		{}
		#region  成员方法
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string ProductId)
		{
			return dal.Exists(ProductId);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void Add(Maticsoft.Model.Product model)
		{
			dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public void Update(Maticsoft.Model.Product model)
		{
			dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public void Delete(string ProductId)
		{
			
			dal.Delete(ProductId);
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.Product GetModel(string ProductId)
		{
			
			return dal.GetModel(ProductId);
		}

		/// <summary>
		/// 得到一个对象实体,从缓存中。
		/// </summary>
		public Maticsoft.Model.Product GetModelByCache(string ProductId)
		{
			
			string CacheKey = "ProductModel-" + ProductId;
			object objModel = LTP.Common.DataCache.GetCache(CacheKey);
			if (objModel == null)  //判断缓存是否过期
			{
				try
				{
					objModel = dal.GetModel(ProductId);
					if (objModel != null)
					{
						int ModelCache = LTP.Common.ConfigHelper.GetConfigInt("ModelCache");
						LTP.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (Maticsoft.Model.Product)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Maticsoft.Model.Product> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			List<Maticsoft.Model.Product> modelList = new List<Maticsoft.Model.Product>();
			int rowsCount = ds.Tables[0].Rows.Count;
			if (rowsCount > 0)
			{
				Maticsoft.Model.Product model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new Maticsoft.Model.Product();
					model.ProductId=ds.Tables[0].Rows[n]["ProductId"].ToString();
					model.CategoryId=ds.Tables[0].Rows[n]["CategoryId"].ToString();
					model.Name=ds.Tables[0].Rows[n]["Name"].ToString();
					model.Descn=ds.Tables[0].Rows[n]["Descn"].ToString();
					model.Image=ds.Tables[0].Rows[n]["Image"].ToString();
					modelList.Add(model);
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  成员方法
	}
}
在所有model的业务层对应的BLL类中,都有一个getModelByCache(主键)的方法。
LTP.Common.DataCache类如下:
public class DataCache
{
    // Methods
    public DataCache(){}
    public static object GetCache(string CacheKey)
    {
      return HttpRuntime.Cache[CacheKey];
    }

   public static void SetCache(string CacheKey, object objObject)
  {
    HttpRuntime.Cache.Insert(CacheKey, objObject);
  }

   public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
  {
    HttpRuntime.Cache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
  }

 
}
由于自动设定了缓存的过期时间,所以没有手动移除。
那么在页面如何访问,什么时候用缓存,什么时候不用?
另外sqldependency的设置该如何使用?
 

抱歉!评论已关闭.