在这种情况下我该如何使用outputcache?
在我的应用程序中,我会记住用户在会话中选择的语言。 问题是,如果我使用输出缓存,那么更改语言功能将不起作用,因为当我根据 Session["lang"] 值从数据库检索时,它会缓存结果。
那么我是否有另一种方法来使用缓存功能?或者我怎样才能减少响应时间?
In my application, I will remember user's language choice in session.
The problem is if I use output cache, then the change language function will not work, because it caches the result when I retrieve from database according to the Session["lang"] value.
So if I have another method to use cache function ? Or how can I decrease the response time ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
输出缓存基础结构的一部分是 VaryBy 机制,它是一种指示 ASP.NET 保持同一页面的并行缓存因某些数据(如查询字符串)而变化的方法。在这种情况下,VaryByCustom 机制可能是最容易实现的。 这是一篇简短的文章,其中有一个很好的示例。
首先,缓存属性:
缓存配置文件:
最后,global.asax.cs 中的自定义逻辑:
现在,对于
Session["lang"]
返回的每个可能的唯一值,ASP .NET 将保留在该参数下执行的页面的缓存副本。Part of the Output Caching infrastructure is the VaryBy mechanism, which is a way to instruct ASP.NET to keep parallel caches of the same page varied by some piece of data, like a querystring. In this case, the VaryByCustom mechanism may be the simplest to implement. Here's a short article with a good example.
First, the caching attribute:
The cache profile:
And finally, the custom logic in global.asax.cs:
Now for each possible unique value that
Session["lang"]
returns, ASP.NET will keep a cached copy of the page which executed under that parameter.将此值保存在cookie中,会话在GetVaryByCustomString中不可用
Save this value in cookies, session are not available in GetVaryByCustomString