博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
缓存帮助类
阅读量:4929 次
发布时间:2019-06-11

本文共 6762 字,大约阅读时间需要 22 分钟。

一、引用

1 using System;2 using System.Collections;3 using System.Web;
View Code

二、使用

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
CacheHelper

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
CookieHelper

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
SessionHelper

 

转载于:https://www.cnblogs.com/raniynight/p/9282556.html

你可能感兴趣的文章
【OpenCV学习笔记】三十、轮廓特征属性及应用(七)—位置关系及轮廓匹配
查看>>
selenide01---截图
查看>>
mysql 查询死锁语句
查看>>
pl_sql操作--激活提升权限scott用户
查看>>
UDP网络编程
查看>>
[Nuxt] Display Vuex Data Differently in Each Page of Nuxt and Vue.js
查看>>
[Ramda] Curry and Uncurry Functions with Ramda
查看>>
[TypeScript] Function Overloads in Typescript
查看>>
[React] React Fundamentals: Mixins
查看>>
关于SQL数据库 数据库名 表名的一些操作
查看>>
1013. 识别三角形
查看>>
IE8+兼容经验小结
查看>>
Android studio下使用SharedSDK
查看>>
会话与请求
查看>>
hibernate one2many (单向关联)
查看>>
自制反汇编逆向分析工具
查看>>
MS SQLserver数据库安装
查看>>
第2章 数字之魅——斐波那契(Fibonacci)数列
查看>>
Spark集群基于Zookeeper的HA搭建部署笔记(转)
查看>>
Codeforces Round #106
查看>>