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 sho开发者_高级运维wn 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.
Why not use the HttpContext.Items property to store it in so it's stored globally instead of locally (IE: HttpContext.Session)?
精彩评论