一、引用
1 using System; 2 using System.Collections; 3 using System.Web;
二、使用
1、Cache帮助类
1 #region 获取缓存 2 /// <summary> 3 /// 获取缓存 4 /// </summary> 5 /// <param name="cacheKey">键</param> 6 /// <returns></returns> 7 public static object GetCache(string cacheKey) 8 { 9 System.Web.Caching.Cache objCache = HttpRuntime.Cache; 10 return objCache[cacheKey]; 11 } 12 #endregion 13 14 #region 设置数据缓存 15 /// <summary> 16 /// 设置数据缓存 17 /// </summary> 18 /// <param name="cacheKey">键</param> 19 /// <param name="objObject">值</param> 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 /// <summary> 26 /// 设置数据缓存 27 /// </summary> 28 /// <param name="cacheKey"></param> 29 /// <param name="objObject"></param> 30 /// <param name="timeout"></param> 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 /// <summary> 37 /// 设置数据缓存 38 /// </summary> 39 /// <param name="cacheKey"></param> 40 /// <param name="objObject"></param> 41 /// <param name="absoluteExpiration"></param> 42 /// <param name="slidingExpiration"></param> 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 #endregion 49 50 #region 移除指定的数据缓存 51 public static void RemoveAllCache(string cacheKey) 52 { 53 System.Web.Caching.Cache _cache = HttpRuntime.Cache; 54 _cache.Remove(cacheKey); 55 } 56 #endregion 57 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 /// <summary> 3 /// 获取cookie的值 4 /// </summary> 5 /// <param name="cookieName"></param> 6 /// <returns></returns> 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 #endregion 16 17 #region 写cookie的值 18 /// <summary> 19 /// 写cookie的值,最大超时时间 20 /// </summary> 21 /// <param name="cookieName">cookie的名称</param> 22 /// <param name="cookieValue">cookie的值</param> 23 /// <returns></returns> 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 /// <summary> 32 /// 写cookie的值 33 /// </summary> 34 /// <param name="cookieName">cookie的名称</param> 35 /// <param name="cookieValue">cookie的值</param> 36 /// <param name="expiresTime">超时时间,单位是分钟</param> 37 /// <returns></returns> 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 #endregion 46 47 #region 检查Cookie 48 /// <summary> 49 /// 检查Cookie 50 /// </summary> 51 /// <param name="cookieName"/> 52 /// <returns/> 53 public static bool CheckCookie(string cookieName) 54 { 55 return HttpContext.Current.Request.Cookies[cookieName] != null; 56 } 57 #endregion 58 59 #region 移除cookie 60 /// <summary> 61 /// 移除cookie 62 /// </summary> 63 /// <param name="cookieName"></param> 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 /// <summary> 3 /// 获取session的值 4 /// </summary> 5 /// <param name="sessionName"></param> 6 /// <returns></returns> 7 public static object GetSession(string sessionName) 8 { 9 if (HttpContext.Current.Session[sessionName] == null) 10 return ""; 11 else 12 return HttpContext.Current.Session[sessionName]; 13 } 14 #endregion 15 16 #region 写session的值 17 /// <summary> 18 /// 写session的值,默认设置为24*60分钟 19 /// </summary> 20 /// <param name="sessionValue">session的名称</param> 21 /// <param name="sessionName">session的值</param> 22 /// <returns></returns> 23 public static void WriteSession(string sessionName, object sessionValue) 24 { 25 HttpContext.Current.Session[sessionName] = sessionValue; 26 HttpContext.Current.Session.Timeout = 1440; 27 } 28 /// <summary> 29 /// 写session的值,设置超时时间 30 /// </summary> 31 /// <param name="sessionName">session的名称</param> 32 /// <param name="sessionValue">session的值</param> 33 /// <param name="expiresTime">超时时间,单位是分钟</param> 34 /// <returns></returns> 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 #endregion 41 42 #region 检查session 43 /// <summary> 44 /// 检查session,存在返回true 45 /// </summary> 46 /// <param name="sessionName"/> 47 /// <returns/> 48 public static bool CheckSession(string sessionName) 49 { 50 return HttpContext.Current.Session[sessionName] != null; 51 } 52 #endregion 53 54 #region 移除session 55 /// <summary> 56 /// 移除session 57 /// </summary> 58 /// <param name="sessionName"></param> 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