代码:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web;namespace Common.Utils{ ////// 缓存 /// public static class CacheHelper { ////// 获取缓存 /// public static object Get(string cacheKey) { return HttpRuntime.Cache[cacheKey]; } ////// 设置缓存 /// public static void Set(string cacheKey, object value) { HttpRuntime.Cache.Insert(cacheKey, value); } ////// 添加缓存 /// public static void Set(string cacheKey, object value) { Type type = typeof(T); string tableName = type.Name; if (HttpRuntime.Cache[tableName] == null) { Dictionary dic = new Dictionary (); dic.Add(cacheKey, value); HttpRuntime.Cache.Insert(tableName, dic); } else { Dictionary dic = (Dictionary )HttpRuntime.Cache[tableName]; if (dic.Keys.Contains (cacheKey)) { dic[cacheKey] = value; } else { dic.Add(cacheKey, value); } HttpRuntime.Cache[tableName] = dic; } } /// /// 获取缓存 /// public static object Get(string cacheKey) { Type type = typeof(T); string tableName = type.Name; if (HttpRuntime.Cache[tableName] != null) { Dictionary dic = (Dictionary )HttpRuntime.Cache[tableName]; if (dic.Keys.Contains (cacheKey)) { return dic[cacheKey]; } } return null; } /// /// 删除缓存 /// public static void Remove() { HttpRuntime.Cache.Remove(typeof(T).Name); } }}