一、引用
1 using System;2 using System.Collections;3 using System.Web;
二、使用
1、Cache帮助类
1 #region 获取缓存 2 ///3 /// 获取缓存 4 /// 5 /// 键 6 ///7 public static object GetCache(string cacheKey) 8 { 9 System.Web.Caching.Cache objCache = HttpRuntime.Cache;10 return objCache[cacheKey];11 }12 #endregion13 14 #region 设置数据缓存15 /// 16 /// 设置数据缓存17 /// 18 /// 键19 /// 值20 public static void SetCache(string cacheKey, object objObject)21 {22 System.Web.Caching.Cache objCache = HttpRuntime.Cache;23 objCache.Insert(cacheKey, objObject);24 }25 ///26 /// 设置数据缓存27 /// 28 /// 29 /// 30 /// 31 public static void SetCache(string cacheKey, object objObject, TimeSpan timeout)32 {33 System.Web.Caching.Cache objCache = HttpRuntime.Cache;34 objCache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);35 }36 ///37 /// 设置数据缓存38 /// 39 /// 40 /// 41 /// 42 /// 43 public static void SetCache(string cacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)44 {45 System.Web.Caching.Cache objCache = HttpRuntime.Cache;46 objCache.Insert(cacheKey, objObject, null, absoluteExpiration, slidingExpiration);47 }48 #endregion49 50 #region 移除指定的数据缓存51 public static void RemoveAllCache(string cacheKey)52 {53 System.Web.Caching.Cache _cache = HttpRuntime.Cache;54 _cache.Remove(cacheKey);55 }56 #endregion57 58 #region 移除全部缓存59 public static void RemoveAllCache()60 {61 System.Web.Caching.Cache _cache = HttpRuntime.Cache;62 IDictionaryEnumerator cacheEnum = _cache.GetEnumerator();63 while (cacheEnum.MoveNext())64 {65 _cache.Remove(cacheEnum.Key.ToString());66 }67 }68 #endregion
2、Cookie帮助类
1 #region 获取cookie的值 2 ///3 /// 获取cookie的值 4 /// 5 /// 6 ///7 public static string GetCookie(string cookieName) 8 { 9 if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[cookieName] != null)10 {11 return HttpContext.Current.Request.Cookies[cookieName].Value.ToString();12 }13 return "";14 }15 #endregion16 17 #region 写cookie的值18 /// 19 /// 写cookie的值,最大超时时间20 /// 21 /// cookie的名称22 /// cookie的值 23 ///24 public static void WriteCookie(string cookieName, string cookieValue)25 {26 HttpCookie cookies = HttpContext.Current.Request.Cookies[cookieName] ?? new HttpCookie(cookieName);27 cookies.Value = cookieValue;28 cookies.Expires = DateTime.Now.AddDays(1);29 HttpContext.Current.Response.AppendCookie(cookies);30 }31 /// 32 /// 写cookie的值33 /// 34 /// cookie的名称35 /// cookie的值36 /// 超时时间,单位是分钟37 ///38 public static void WriteCookie(string cookieName, string cookieValue, double expiresTime)39 {40 HttpCookie cookies = HttpContext.Current.Request.Cookies[cookieName] ?? new HttpCookie(cookieName);41 cookies.Value = cookieValue;42 cookies.Expires = DateTime.Now.AddMinutes(expiresTime);43 HttpContext.Current.Response.AppendCookie(cookies);44 }45 #endregion46 47 #region 检查Cookie48 /// 49 /// 检查Cookie50 /// 51 /// 52 ///53 public static bool CheckCookie(string cookieName)54 {55 return HttpContext.Current.Request.Cookies[cookieName] != null;56 }57 #endregion58 59 #region 移除cookie60 /// 61 /// 移除cookie62 /// 63 /// 64 public static void RemoveCookie(string cookieName)65 {66 HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];67 if (cookie == null)68 return;69 cookie.Expires = DateTime.Now.AddYears(-3);70 HttpContext.Current.Response.Cookies.Add(cookie);71 }72 #endregion
3、Session帮助类
1 #region 获取session的值 2 ///3 /// 获取session的值 4 /// 5 /// 6 ///7 public static object GetSession(string sessionName) 8 { 9 if (HttpContext.Current.Session[sessionName] == null)10 return "";11 else12 return HttpContext.Current.Session[sessionName];13 }14 #endregion15 16 #region 写session的值17 /// 18 /// 写session的值,默认设置为24*60分钟19 /// 20 /// session的名称21 /// session的值 22 ///23 public static void WriteSession(string sessionName, object sessionValue)24 {25 HttpContext.Current.Session[sessionName] = sessionValue;26 HttpContext.Current.Session.Timeout = 1440;27 }28 /// 29 /// 写session的值,设置超时时间30 /// 31 /// session的名称32 /// session的值33 /// 超时时间,单位是分钟34 ///35 public static void WriteSession(string sessionName, object sessionValue, int expiresTime)36 {37 HttpContext.Current.Session[sessionName] = sessionValue;38 HttpContext.Current.Session.Timeout = expiresTime;39 }40 #endregion41 42 #region 检查session43 /// 44 /// 检查session,存在返回true45 /// 46 /// 47 ///48 public static bool CheckSession(string sessionName)49 {50 return HttpContext.Current.Session[sessionName] != null;51 }52 #endregion53 54 #region 移除session55 /// 56 /// 移除session57 /// 58 /// 59 public static void RemoveSession(string sessionName)60 {61 var session = HttpContext.Current.Session[sessionName];62 if (session == null)63 return;64 session = null;65 HttpContext.Current.Session.Remove(sessionName);66 }67 #endregion