ASP.Net 状态服务器和访问 HttpContext.Current.Session
我目前正在使用 ASP.Net 状态服务器在我的应用程序中存储会话数据。
作为解决更大的会话问题(不要问)的解决方法,我需要从会话中删除一组包含特定字符串的键,但我不知道确切的键。
我想做类似下面代码的事情。由于会话数据不在进程中,我担心的是,通过这样做,我会将整个会话数据加载到 ASP.Net 工作进程中。这是一个问题,因为当前应用程序在会话中放置了太多数据(我们正在尝试解决的长期问题)。
有谁知道会话是如何在幕后访问的?有没有一种方法可以迭代会话密钥而不将会话加载到内存中?
//does this line cause the session to be loaded back into the WP?
var session = HttpContext.Current.Session;
if (session != null)
{
//what about this line?
var keysToRemove = session.Keys.Cast<object>().
Where(key => key.ToString().Contains(MYKEY)).ToList();
foreach (var key in keysToRemove)
{
session.Remove(key.ToString());
}
}
I am currently using ASP.Net state server to store session data in my application.
As a workaround to a larger problem with sessions (don't ask), I need to remove a set of keys that contain a certain string from session, but I don't know the exact keys.
I want to do something like the code below. Since the session data is out of process, my concern is that by doing this I am loading the entirety of the session data into the ASP.Net worker process. This is a problem, because the current application puts too much data in session (the longer term problem we are trying to solve).
Does anyone know how the session gets accessed under the hood? Is there a way to iterate over session keys without loading the session into memory?
//does this line cause the session to be loaded back into the WP?
var session = HttpContext.Current.Session;
if (session != null)
{
//what about this line?
var keysToRemove = session.Keys.Cast<object>().
Where(key => key.ToString().Contains(MYKEY)).ToList();
foreach (var key in keysToRemove)
{
session.Remove(key.ToString());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
整个会话始终根据用户对内置提供程序的请求加载到内存中。它在执行到达正常页面代码之前加载(不像您想象的那样延迟加载)。为了防止加载,您唯一可以做的就是让模块侦听早期事件之一并从请求中删除会话 ID。
注意:session.Keys已经是字符串的集合,因此您可以删除如此复杂的转换。
Whole session is always loaded to memory on user's request for built in providers. It is loaded before execution reaches normal page code (not lazily as you assume). The only thing you can do to prevent loading is to have module that listens to one of the early events and removes session Id from request.
Note: session.Keys is already collection of strings, so you can remove such complicated casting.