views:

132

answers:

1

I have an application in asp.net mvc (C#), in which each user have different products sharing a common table separated by user id.

I need to implement SqlCacheDependency for each user's products.

The Scenario is: say there are two users (user1 and user2) and products assigned for them based on the user id.

I need to cache the products using SqlCacheDependency for each user as they wont update/change it frequently. I need to maintain the cache of the user1 even when the products belonging to user2 are changed.

How can i maintain the SqlCacheDependency based on the User id for products sharing a common table?

A: 

Maybe it's not the SqlCacheDependency that needs to be cached by user id, but the HttpContextCache entry.

Would this work for you? (I'm assuming userProducts to be your DataTable or List<Product> as appropriate.

var cmd = new SqlCommand("select * from product where user_id = @user_id");
cmd.Parameters.Add("@user_id", userId);
HttpContext.Current.Cache.Insert(string.Format("products:{0}", userId), userProducts, new SqlCacheDependency(cmd));
Josh Kodroff
I have just thought of this idea. But the sad thing is, the architecture(like BLL, DAL) which we are using doesn't allow sqlcommand to be used in BLL and System.Web.HttpContext.Current.Cache in DAL. Is there any other alternative for this? Please suggest
Prasad
Normally you wouldn't have a SqlCommand parameter in the BLL, but you should be able to do it through the DAL. You would then expose a wrapper method in the BLL that calls the DAL method. Is it possible for you to add such a wrapper in the BLL?
Josh Kodroff