locked
Controlling Web Application Cache When Accessed By Mulitple PC's RRS feed

  • Question

  • User-224188520 posted

    Hey All, I have this bit of code to cache results from search function for 5 minutes.

    If Cache("searchDetails") Is Nothing Then
        Cache.Insert("searchDetails", returnSearchResults(locationSearch, Request.QueryString("search")), Nothing, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration)
    End If
    
    searchResults_Grid.DataSource = Cache("searchDetails")
    searchResults_Grid.DataBind()

    There is some comparative work being viewed by users and they switch back and forth between screens in the application, so it makes sense to cache the results instead of hitting the database multiple times for the same search.

    What I noticed is 2 computers side by side searching different results end up having one or the others search results show up when going back and forth to the search results page. So from that I guess the server side cache is a global value that both users end up sharing.

    How do I store this information in cache that would be unique for each computers search results?

    Regards,

    Monday, February 23, 2015 11:06 AM

Answers

  • User-760709272 posted

    You can incorporate the session id into the cache key so each person gets their own results.  I don't know vb.net so some of this might need fixing

    Dim key as String
    
    key = String.Concat("searchDetails_", Session.SessionID)
    
    If Cache(key) Is Nothing Then
        Cache.Insert(key, returnSearchResults(locationSearch, Request.QueryString("search")), Nothing, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration)
    End If
    
    searchResults_Grid.DataSource = Cache(key)
    searchResults_Grid.DataBind()

    not perfect but should work.  It means people using the same session in multiple tabs still get the same results but not much you can do about that.  It also means that if someone's session is abandoned and is then restarted they will get the same results as iis recycles the session id.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 25, 2015 8:29 AM

All replies

  • User1711366110 posted

    hi Rivelyn,
      As per your case, I have been shared my ideas below :

    1.The cache is global so every use will access the same resources through the site. You can look at the Session if you need session persistent user data.

    2. Just because your data updates every 5minutes does not mean that your cache needs to be updated every ten minutes.

    3.Determine how frequently you have to update the data to meet your service level agreements.

    4.Avoid repopulating caches for data that changes frequently.

    5.If your data changes frequently, that data may not be a good candidate for caching.

    6.Have a look at Caching Callback example below :

    private void Page_Load(object sender, System.EventArgs e) { 
    string cacheKey = "myKey"; 
    string data = ""; 
    // Check to see if the data is in the cache already if(Cache[cacheKey]==null) {
    // Get the data since it isn't in the cache data = System.DateTime.Now.ToString(); //create an instance of the callback delegate
    CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove); Label1.Text = "Generated: " + data; Cache.Insert(cacheKey,data,null, System.DateTime.Now.AddSeconds(5), System.Web.Caching.Cache.NoSlidingExpiration,
    System.Web.Caching.CacheItemPriority.Default, callBack); } else { Label1.Text = "Cached: " + Cache[cacheKey].ToString(); } } private void onRemove(string key, object val, CacheItemRemovedReason reason) { //create an instance of the callback delegate CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);
    Cache.Insert(key,val.ToString() + "*",null,System.DateTime.Now.AddSeconds(5),Cache.NoSlidingExpiration,
    System.Web.Caching.CacheItemPriority.Default, callBack); }

    For more information, Click here to refer.

    --
    with regards,
    Edwin

    Tuesday, February 24, 2015 10:16 PM
  • User-224188520 posted

    Hey Edwin,

    Thanks for your reply.

    What I am dealing with is a person may, in a span of a few minutes revisit the same data several times, so caching is just a way to allow the page load time to decrease after the first visit. That is why I just wanted a 5 minute cache, I can't use session persistent user data because all logins are the same.

    What exactly is the onRemove function of your code doing?

    Wednesday, February 25, 2015 8:03 AM
  • User-760709272 posted

    You can incorporate the session id into the cache key so each person gets their own results.  I don't know vb.net so some of this might need fixing

    Dim key as String
    
    key = String.Concat("searchDetails_", Session.SessionID)
    
    If Cache(key) Is Nothing Then
        Cache.Insert(key, returnSearchResults(locationSearch, Request.QueryString("search")), Nothing, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration)
    End If
    
    searchResults_Grid.DataSource = Cache(key)
    searchResults_Grid.DataBind()

    not perfect but should work.  It means people using the same session in multiple tabs still get the same results but not much you can do about that.  It also means that if someone's session is abandoned and is then restarted they will get the same results as iis recycles the session id.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 25, 2015 8:29 AM
  • User-224188520 posted

    AidyF - That is what I was thinking if I created a unique session of some information, like a datetime at the time of the data request? Then cache the data results using the session appended to the searchDetails as the cache key, again like you recommend?

    Would that not give every user even logged in under the same account a unique cache result?

    Wednesday, February 25, 2015 8:49 AM
  • User-760709272 posted

    Cache is global, all users share it, which is why you need to make the key related to the user.  If you incorporate the sessionid into the key as per my code example then every session will store and retrieve their data independent of each other regardless of what user the person is logged in as (authentication isn't related to session).  This means different people using different browsers will have their own cached data even if logged in as the same person because they still have their own unique session despite being logged in as the same user.

    Wednesday, February 25, 2015 9:10 AM
  • User-224188520 posted

    Thanks AidyF, I have marked your code example above as the answer.

    Wednesday, February 25, 2015 10:55 AM