OutPutCache 自定义缓存:session、登录用户、cookie 等
2011-07-11 17:25
453 查看
其实这个也是新瓶装旧酒的问题,前段时间,把这个做了,况且效果和性能都还不错,所以记录下,希望能帮助更多的人
虽然 outputcache 很重要,但是这个功能真的不好用,很多时间不能满足需求,比如做一些自定义方面的缓存依赖,如:session、登录用户信息、用户cookie信息 等,更重要的是,想做片段缓存的话,就只能用 用户控件了,并且这是时,用户控件之间的传值就比较难了,所以,让人很不爽!真怀疑,asp.net 的开发工程师是不是就不用 asp.net,开发出来的产品,离实用还是有段距离的吧!!!!
好了,还得自己动手,解决了 一些自定义方面的需求,支持 session、登录用户、cookie 等,代码如下:
以下代码只是测试,如果使用,请按自己需求更改部分代码。
Global.asax 文件里面加入此方法:
之后在调用页面头部加入下面代码,也就是在 VaryByParam 里面自定义依赖项:
<%@ OutputCache Duration="10" VaryByParam="id;pagenum" VaryByCustom="Admin;Login"%>
这个页面的缓存依赖将会是:contentid(id,url中的参数)、pagenum(pagenum,url中的参数)、Admin(是否是管理员,自定义的依赖)和Login(用户是否登录,自定义的依赖)
好了,这样就能比较完美的支持 session、登录用户、cookie 等自定义依赖缓存了,outputcache 也终于能智能点了,不过,还是没有想到更加方便的对页面片段进行依赖缓存了
虽然 outputcache 很重要,但是这个功能真的不好用,很多时间不能满足需求,比如做一些自定义方面的缓存依赖,如:session、登录用户信息、用户cookie信息 等,更重要的是,想做片段缓存的话,就只能用 用户控件了,并且这是时,用户控件之间的传值就比较难了,所以,让人很不爽!真怀疑,asp.net 的开发工程师是不是就不用 asp.net,开发出来的产品,离实用还是有段距离的吧!!!!
好了,还得自己动手,解决了 一些自定义方面的需求,支持 session、登录用户、cookie 等,代码如下:
以下代码只是测试,如果使用,请按自己需求更改部分代码。
Global.asax 文件里面加入此方法:
protected void Application_BeginRequest(Object sender, EventArgs e) { //solove "vary" outputcache bug Response.Cache.SetOmitVaryStar(true); //exclude mobile browser string acceptTypes = Request.Headers["Accept"]; if (!string.IsNullOrEmpty(acceptTypes) && acceptTypes.ToLower().Contains("text/vnd.wap.wml")) { Response.Cache.SetCacheability(HttpCacheability.NoCache); } } public override string GetVaryByCustomString(HttpContext context, string arg) { string key=string.Empty; arg = arg.ToLower(); //outputcache customer if (arg.Contains("user")) //Login User { key +="user:"+ HttpContext.Current.User.Identity.Name +";"; } if (arg.Contains("admin")) // Is Admin User { key +="admin:"+ HttpContext.Current.User.Identity.Name +";-"; } if (arg.Contains("hot")) //Is HotRate List { HttpCookie cookie = Request.Cookies["hot"]; bool isHotRate =true; if (cookie !=null&& cookie.Value =="0") isHotRate =false; key +="hot:"+ isHotRate.ToString() +";"; } if (arg.Contains("login")) //User Is Login { key +="login:"+ context.User.Identity.IsAuthenticated.ToString() +";"; } return key; }
之后在调用页面头部加入下面代码,也就是在 VaryByParam 里面自定义依赖项:
<%@ OutputCache Duration="10" VaryByParam="id;pagenum" VaryByCustom="Admin;Login"%>
这个页面的缓存依赖将会是:contentid(id,url中的参数)、pagenum(pagenum,url中的参数)、Admin(是否是管理员,自定义的依赖)和Login(用户是否登录,自定义的依赖)
好了,这样就能比较完美的支持 session、登录用户、cookie 等自定义依赖缓存了,outputcache 也终于能智能点了,不过,还是没有想到更加方便的对页面片段进行依赖缓存了
相关文章推荐
- OutPutCache 自定义缓存:session、登录用户、cookie
- php用户登录代码session、cookie自动记忆功能
- 使用cookie,session,filter等技术实现用户自动登录
- php同时使用session和cookie来保存用户登录信息
- java web session+cookie实现用户自动登录
- 用Cookie和Session实现用户登录 函数
- cookie,session,实现服务器记忆用户登录状态功能
- php 依据session与cookie用户登录状态操作类
- Asp.Net使用加密cookie代替session验证用户登录状态 源码分享 欢迎拍砖
- 关于用户登录状态存session,cookie还是数据库或者memcache的优劣
- 关于用户登录状态存session,cookie还是数据库或者memcache的优劣
- YII用户注册和用户登录(五)之进行session和cookie分析 ,并在前后区分session和cookie
- 服务端如何识别已登录用户身份之Session管理和Cookie应用
- 使用加密cookie代替session验证用户登录状态
- Facebook(一)自定义登录按钮,登陆完毕后清除缓存的Session信息
- Cookie与session 用户登录
- 分别用cookie和session保存用户登录信息
- php同时使用session和cookie来保存用户登录信息
- PHP根据session与cookie用户登录状态操作类的代码