简体   繁体   中英

ASP.NET Caching with Session variable?

I've got some kind of community website, there are about 40.000 users in database with many property columns. There are two pages where latest and online users are shown based on some kind of criteria ie. gender.

I've made some SQL trick to get only ten rows per page so not that much information is sent back by sql server for search,inbox etc.. and other user based data but online and latest users are global for all users of my website hence i could use some sort of caching for these. Problem is that i use some user variables in Session set upon login thus if i use default output caching Session will be swapped with other users.

How would you solve this issue ? I know few ways but some guys in here got a lot of experience so i wait for your input. I think it is common issue for anyone who did or do application with many users in it. Maybe StackOverflow crew ???

Cheers

You say, that you cannot use output caching, because you are using user specific data to generate the output. But you can also use the cache programatically to store objects that you access in the code behind file (HttpContext.Cache)

So you could write something like this (pseudo code) in your code behind file:

var cacheableUserData = (UserData)Cache("UserData");
if (cacheableUserData== null)
{
    cacheableUserData= GetDataFromDB();
    Cache.Add("UserData", cacheableUserData); // Set cache expiration here
}
var dataToPresent = CombineData(userData, loggedInUserData);

Then you are not caching the output, but part of the data that is used to generate the output.

为什么不使用HttpContext.Items属性将其存储在其中,以便将其全局存储而不是本地存储(即HttpContext.Session)?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM