如何在asp.net mvc中为每个用户使用sqlcachedependency

发布于 2024-08-19 19:23:04 字数 349 浏览 3 评论 0原文

我在 asp.net mvc (C#) 中有一个应用程序,其中每个用户都有不同的产品,共享一个由用户 ID 分隔的公共表。

我需要为每个用户的产品实现 SqlCacheDependency

场景是:假设有两个用户(user1 和 user2),并根据用户 ID 为他们分配产品。

我需要为每个用户使用 SqlCacheDependency 缓存产品,因为他们不会频繁更新/更改它。即使属于 user2 的产品发生更改,我也需要维护 user1 的缓存。

如何根据共享公共表的产品的用户 ID 维护 SqlCacheDependency

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

也许需要通过用户id缓存的不是SqlCacheDependency,而是HttpContextCache条目。

这对你有用吗? (我假设 userProducts 是您的 DataTableList(视情况而定)。

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));

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文