views:

519

answers:

2

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

A: 

Why not use the HttpContext.Items property to store it in so it's stored globally instead of locally (IE: HttpContext.Session)?

Kim Johansson
I store there userID,userAge and few other fields. How saving those globally will do ?
eugeneK
I don't really understand the problem... :SOh, after some more reading. You need to cache by session variables.Default output caching won't affect the session unless you use user-sepcific stuff in the control rendered with outputcache.
Kim Johansson
Output cache will affect my controls as i use ?userID=something when something is taken from Session so if i output cache the page, i get userID of first user to use the page in output cache cycle.
eugeneK
Use VaryByParam
Kim Johansson
VaryByParam is based on query string.
eugeneK
?userID=something isn't querystring?
Kim Johansson
HttpContext.Items is pr. request only, so that would be a bad place to cache data.
Pete
+1  A: 

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.

Pete