1、首先构造逻辑层填充数据方法,此处举例,大致方法为City_Select(string province_id)为根据省份id取出城市,返回类型为datatable;
2、然后依据逻辑层方法建立委托定义类:
using System;
using System.Web;
using System.Data;
using System.Collections;
/// <summary>
/// 搜集编写者losingrose
/// QQ:303864496
/// MSN:losingrose@21cn.com
/// http://losingrose.cnblogs.com
/// 委托方法定义
/// </summary>
public class DelegateLists
{
public delegate DataTable datatable_1fill(string a);
}
3、然后建立Cache的操作类:
using System;
using System.Web;
using System.Data;
using System.Web.Caching;
using System.Collections;
/// <summary>
/// 搜集编写者losingrose
/// QQ:303864496
/// MSN:losingrose@21cn.com
/// http://losingrose.cnblogs.com
/// Cache操作类
/// </summary>
public static class MyCache
{
static Cache cache = HttpContext.Current.Cache;
static double default_buffminutes = Convert.ToDouble(System.Web.Configuration.WebConfigurationManager.AppSettings["default_buffminutes"]);
/// <summary>
/// 获取Cache对象,如果对象不存在,则调用相关事件委托填充并返回Cache的值。
/// </summary>
/// <param name="key">键名</param>
/// <param name="buffminutes">缓存时间(按分钟计),为0则读取配置文件设定的时间</param>
/// <param name="method">填充的方法委托</param>
/// <param name="parms">方法所需要的参数列表</param>
/// <returns>返回Cache的值</returns>
public static object Cache_Select(string key, double buffminutes, Delegate method, params object[] parms)
{
object x = cache.Get(key);
if (x == null)
{
if (buffminutes == 0)
buffminutes = default_buffminutes;
Cache_Insert(key, method.DynamicInvoke(parms), buffminutes);
x = cache.Get(key);
}
return x;
}
}
4、页面使用方法:
DelegateLists.datatable_1fill del = new DelegateLists.datatable_1fill(bll.City_Select);
object[] prams = new object[1];
prams[0] = "1";
DropDownList1.DataSource = (DataTable)MyCache.Cache_Select("内存中Cache的名称", 0, del, prams);
DropDownList1.DataBind();
此方法已经过一中型网站测试,代码修正了很多,配合Memcached基本稳定,使用Cache真的是相当不稳定,如有兴趣的可以一起研究讨论。具体请参考文章
Asp.Net使用Memcached以解决Cache的不稳定性
losingrose
qq:303864496
浙公网安备 33010602011771号