0

We are setting session-timeout for 1 hour:

<system.web>  
  <sessionState mode="InProc" timeout="60"/>  
</system.web> 

It works as expected.

Alongwith "Session", we create user specific data-cache to reuse the data.

We use unique cache-key for each logged in user:

var studentId = GetStudentIdFromRequest();
var cacheKey = "SyllabusInfoCacheKey_" + studentId;

We set the expiration time of cache as:

Cache.Insert("MyKey", myObject, null, DateTime.Today.AddDays(1), TimeSpan.Zero);

But I need to remove the user specific data-cache once the session gets timeout.

Kindly help me out.

Pranav Kumar
  • 249
  • 1
  • 5
  • 16

2 Answers2

1

If you want the data in cache to be in sync with the session, i would suggest to change the approach i.e to store the data in session itself instead of cache. Your requirement will be automatically taken care of.

PS: In your previous question i had suggested the Cache approach, since you wanted to use Cache instead of Session. But it looks like the use case, suits best for Session usage instead of Cache. Please comment if you see any issue with the Session approach.

Community
  • 1
  • 1
Boney
  • 2,072
  • 3
  • 18
  • 23
  • Session is great for user-specific data, but is recommended to keep very small and again stick to the primary data types like ints and strings. But I need to store 'datatable' in my 'use-case'. That is why, I thought to go with "Caching". Kindly clarify, if I am on a wrong track. Thanks in Advance! – Pranav Kumar Apr 14 '17 at 09:29
  • Unless you have that level of performance requirement, i don't see any issue with use of Session especially since your requirement points in the direction of Session use. May be you can do a performance comparison between Session and Cache, but i can assure you that we have used Session for storing large objects. – Boney Apr 14 '17 at 09:41
  • I am not sure Boney, how to compare performance between 'Session' and 'Cache' in real time. I went through few blogs and then pointed myself towards Cache.. It'll be very helpful if you suggest "how to compare performance between 'Session' and 'Cache' in real time". Kindly share any blog/document. Thanks in Advance! – Pranav Kumar Apr 14 '17 at 10:00
  • I meant using something like a stopwatch. Not sure if there would be any big difference testing with a single user. You can giv it a try anyways. You can refer to this link: http://stackoverflow.com/questions/969290/exact-time-measurement-for-performance-testing – Boney Apr 14 '17 at 16:30
1

Please try below.


Put below code in Global.asax file in your project.

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


Please put this code in you vb file where you are assigning session.

    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

Note: convert this code in C# if you are not using vb.

Rajan
  • 303
  • 1
  • 12
  • But how would he get all the key values here, unless there is some cache key repository. There may be a lot of cache keys used at different places in the code. – Boney Apr 14 '17 at 09:30
  • Right @Boney but this is the only a event that he can get session timeout. And also he need to store all cache keys if he want to use cache. otherwise session is the best way. – Rajan Apr 14 '17 at 10:00
  • But @Rajan, Session is recommended to keep very small and again stick to the primary data types like ints and strings. Kindly clarify, if this statement is erroneous. As I need to store 'datatable (user specific data)' at server side to avoid repeated DB calls, is "Session" a better approach than "Cache" for my use-case? – Pranav Kumar Apr 14 '17 at 10:07
  • Depends on your requirement. We are stroring big object also in it when we require. You can use state server also. - @PranavKumar – Rajan Apr 14 '17 at 10:24
  • @Rajan : I tried with the approach but it is throwing 'Null-Reference' exception' as the "HttpContext.Current" object contains NULL value inside "Session_End" event. Kindly suggest. – Pranav Kumar Apr 15 '17 at 10:47
  • You can also set cache timeout same as session timeout. so when you session timeout cache also will be timeout. Please let me know when you done. - @PranavKumar – Rajan Apr 16 '17 at 10:09
  • 1
    The other way is do the same timeout of both cache & session. It can also resolve your problem. Please rate my answer. So I can get reputation... :) @PranavKumar – Rajan Apr 20 '17 at 04:17
  • @Rajan I had already liked and now accepted your answer as well. Let me know if I can do anything else. Thanks again! :) :) – Pranav Kumar Apr 20 '17 at 18:07
  • @PranavKumar Thanks you also but the other way if it can more efficient for you only. :) – Rajan Apr 22 '17 at 11:13