/********************************************************* * 创 建 人:事理 * 创建时间:2012-09-03 11:56 * 版权所有:Copyright © * 描 述:通用应用程序缓存辅助类 * *******************************************************/using System;using System.Data;using System.Web;using System.Collections.Generic;public static class CacheHelper{ public delegate T GetDataMethod();//获取数据的方法 /// /// 通用应用程序缓存方法,缓存数据未指定时间 /// ///缓存数据的类型,一般是集合,如IList<UsersData> /// 键 /// 缓存时间,分钟单位 /// 获取数据的方法 ///数据列表 public static T GetCache(string key, DateTime cacheTimeout, GetDataMethod getDataMethod) { //T dataList = getDataMethod(); //return dataList; if (HttpRuntime.Cache[key] == null) { T dataList = getDataMethod(); HttpRuntime.Cache.Add(key, dataList, null, cacheTimeout, TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null); return dataList; } return (T)HttpRuntime.Cache[key]; } /// /// 移除应用程序缓存 /// /// 键 public static void RemoveCacheByKey(string key) { HttpRuntime.Cache.Remove(key); } ////// 移除Key相似的缓存 /// /// key相似的部分 public static void RemoveCacheBySimilarKey(string similarKey) { System.Collections.IDictionaryEnumerator cacheEnum = HttpRuntime.Cache.GetEnumerator(); Listkeys = new List (); while (cacheEnum.MoveNext()) { if (cacheEnum.Key.ToString().Contains(similarKey)) keys.Add(cacheEnum.Key.ToString()); } for (int i = 0; i < keys.Count; i++) { HttpRuntime.Cache.Remove(keys[i]); } }}
//使用示例rptTop.DataSource = CacheHelper.GetCache("Menu", new CacheHelper.GetDataMethod>( delegate() { return PlateBll.GetAllByPlateType(new int[] { 1 }, 0, new int[] { 0, 1 }); } )); rptTop.DataBind();