Reputation: 10476
I have a page that sets a session variable to hold a list of lookups from the database for the page when the page is loaded.
The page also will need to access that list of lookups when Ajax calls are made from the browser.
I'd like to not load the list of lookups from the database for each Ajax call, but I would like to remove the list of lookups from the session if the user leaves the page. Is there a best practice or recommended strategy for doing this?
Upvotes: 0
Views: 1589
Reputation: 4239
You can clear the session variable with javascript using something like this
window.onbeforeunload = cleanup;
function cleanup()
{
// Clear session variable here
}
However, this isn't foolproof since the user could always disable javascript, etc. Usually this won't be an issue since the session variable will have a timeout anyway. If you are really concerned, you should remove any current sessions on page load.
Another option, depending on your situation, is to use the Caching.Cache
class to hold the values. When you insert the values into the cache, you can set them to expire after a TimeSpan
, and just set it for pretty short, like 5 minutes or so.
Note that while sessions are per user, their is only one cache per server instance.
Upvotes: 2