如何在会话(In Proc)在 ASP.Net 中超时后删除 "user specific data-cache"?
How to remove the "user specific data-cache" once the session(In Proc) gets timeout in ASP.Net?
我们将会话超时设置为 1 小时:
<system.web>
<sessionState mode="InProc" timeout="60"/>
</system.web>
它按预期工作。
与 "Session" 一起,我们创建 用户特定数据缓存 以重用数据。
我们为每个登录用户使用唯一缓存键:
var studentId = GetStudentIdFromRequest();
var cacheKey = "SyllabusInfoCacheKey_" + studentId;
我们设置缓存的过期时间为:
Cache.Insert("MyKey", myObject, null, DateTime.Today.AddDays(1), TimeSpan.Zero);
但是一旦会话超时,我需要删除用户特定的数据缓存。
请帮帮我。
如果您希望缓存中的数据与会话同步,我建议更改方法,即将数据存储在会话本身而不是缓存中。您的要求将自动得到处理。
PS:在您的 previous question 中,我建议使用缓存方法,因为您想使用缓存而不是会话。但它看起来像用例,最适合会话使用而不是缓存。
如果您发现会话方法有任何问题,请发表评论。
Please try below.
将以下代码放入项目的 Global.asax 文件中。
Shared Dict As New Dictionary(Of String, String)
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application startup
Application.Add("AllSession", Dict)
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a session ends.
' Note: The Session_End event is raised only when the sessionstate mode
' is set to InProc in the Web.config file. If session mode is set to StateServer
' or SQLServer, the event is not raised.
If Dict.ContainsKey(Me.Session.SessionID) Then
HttpRuntime.Cache.Remove(Dict.Item(Me.Session.SessionID))
Dict.Remove(Me.Session.SessionID)
Application.Add("AllSession", Dict)
End If
End Sub
请将此代码放入您要分配会话的 vb 文件中。
Dim cacheKey As New Random
Dim DictcacheKey As Dictionary(Of String, String)
DictcacheKey = Application("AllSession")
If Not DictcacheKey.ContainsKey(HttpContext.Current.Session.SessionID) Then
DictcacheKey.Add(HttpContext.Current.Session.SessionID, cacheKey.Next())
Application.Add("AllSession", DictcacheKey)
Cache.Insert(cacheKey.Next(), DictcacheKey, Nothing, DateTime.Today.AddDays(1), TimeSpan.Zero)
End If
注意:如果您不使用 vb,请在 C# 中转换此代码。
我们将会话超时设置为 1 小时:
<system.web>
<sessionState mode="InProc" timeout="60"/>
</system.web>
它按预期工作。
与 "Session" 一起,我们创建 用户特定数据缓存 以重用数据。
我们为每个登录用户使用唯一缓存键:
var studentId = GetStudentIdFromRequest();
var cacheKey = "SyllabusInfoCacheKey_" + studentId;
我们设置缓存的过期时间为:
Cache.Insert("MyKey", myObject, null, DateTime.Today.AddDays(1), TimeSpan.Zero);
但是一旦会话超时,我需要删除用户特定的数据缓存。
请帮帮我。
如果您希望缓存中的数据与会话同步,我建议更改方法,即将数据存储在会话本身而不是缓存中。您的要求将自动得到处理。
PS:在您的 previous question 中,我建议使用缓存方法,因为您想使用缓存而不是会话。但它看起来像用例,最适合会话使用而不是缓存。 如果您发现会话方法有任何问题,请发表评论。
Please try below.
将以下代码放入项目的 Global.asax 文件中。
Shared Dict As New Dictionary(Of String, String)
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application startup
Application.Add("AllSession", Dict)
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a session ends.
' Note: The Session_End event is raised only when the sessionstate mode
' is set to InProc in the Web.config file. If session mode is set to StateServer
' or SQLServer, the event is not raised.
If Dict.ContainsKey(Me.Session.SessionID) Then
HttpRuntime.Cache.Remove(Dict.Item(Me.Session.SessionID))
Dict.Remove(Me.Session.SessionID)
Application.Add("AllSession", Dict)
End If
End Sub
请将此代码放入您要分配会话的 vb 文件中。
Dim cacheKey As New Random
Dim DictcacheKey As Dictionary(Of String, String)
DictcacheKey = Application("AllSession")
If Not DictcacheKey.ContainsKey(HttpContext.Current.Session.SessionID) Then
DictcacheKey.Add(HttpContext.Current.Session.SessionID, cacheKey.Next())
Application.Add("AllSession", DictcacheKey)
Cache.Insert(cacheKey.Next(), DictcacheKey, Nothing, DateTime.Today.AddDays(1), TimeSpan.Zero)
End If
注意:如果您不使用 vb,请在 C# 中转换此代码。