缓存和 SqlCacheDependency (ASP.NET MVC)

发布于 2024-08-07 02:34:21 字数 927 浏览 2 评论 0原文

我们需要返回记录的子集,为此我们使用以下命令:

using (SqlCommand command = new SqlCommand(
                    "SELECT ID, Name, Flag, IsDefault FROM (SELECT ROW_NUMBER() OVER (ORDER BY @OrderBy DESC) as Row, ID, Name, Flag, IsDefault FROM dbo.Languages) results WHERE Row BETWEEN ((@Page - 1) * @ItemsPerPage + 1) AND (@Page * @ItemsPerPage)",
                    connection))

我设置了一个如下所示的 SqlCacheDependency 声明:

SqlCacheDependency cacheDependency = new SqlCacheDependency(command);

但是在我运行 command.ExecuteReader() 指令后,立即尽管我没有以任何方式更改查询结果,但 SqlCacheDependency 对象变为 true!并且,因此,该查询的结果不会保存在缓存中。

HttpRuntime.Cache.Insert( cacheKey, list, cacheDependency, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(AppConfiguration.CacheExpiration.VeryLowActivity));

是因为该命令有 2 个 SELECT 语句吗?是 ROW_NUMBER() 吗?如果是,还有其他方法对结果进行分页吗?

请帮忙!经过太多时间后,一点点将不胜感激!谢谢

We need to return subset of records and for that we use the following command:

using (SqlCommand command = new SqlCommand(
                    "SELECT ID, Name, Flag, IsDefault FROM (SELECT ROW_NUMBER() OVER (ORDER BY @OrderBy DESC) as Row, ID, Name, Flag, IsDefault FROM dbo.Languages) results WHERE Row BETWEEN ((@Page - 1) * @ItemsPerPage + 1) AND (@Page * @ItemsPerPage)",
                    connection))

I set a SqlCacheDependency declared like this:

SqlCacheDependency cacheDependency = new SqlCacheDependency(command);

But immediately after I run the command.ExecuteReader() instruction, the hasChanged base property of the SqlCacheDependency object becomes true although I did not change the result of the query in any way! And, because of this, the result of this query is not kept in cache.

HttpRuntime.Cache.Insert( cacheKey, list, cacheDependency, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(AppConfiguration.CacheExpiration.VeryLowActivity));

Is it because the command has 2 SELECT statements? Is it ROW_NUMBER()? If yes, is there any other way to paginate results?

Please help! After too many hours, a little will be greatly appreciated! Thank you

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

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

发布评论

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

评论(3

分开我的手 2024-08-14 02:34:21

遇到同样的问题并在没有任何帮助的情况下在线找到相同的答案,我正在研究探查器的 xml 无效订阅响应。

我在 msdn 支持网站上找到了一个示例,该示例的代码顺序略有不同。当我尝试它时,我意识到了问题 - 在创建命令对象和缓存依赖对象之前不要打开连接对象。以下是您必须遵循的顺序,一切都会好起来的:

  1. 确保启用通知 (SqlCahceDependencyAdmin) 并运行 SqlDependency.Start 首先
  2. 创建连接对象
  3. 创建命令对象并分配命令文本、类型和连接对象(构造函数的任意组合) 、设置属性或使用 CreateCommand)。
  4. 创建 sql 缓存依赖项对象
  5. 打开连接对象
  6. 执行查询
  7. 使用依赖项将项目添加到缓存。

如果您遵循此顺序,并遵循 select 语句上的所有其他要求,没有任何权限问题,这将起作用!

我认为问题与 .NET 框架如何管理连接有关,特别是设置了哪些设置。我尝试在我的 sql 命令测试中覆盖它,但它从未起作用。这只是一个猜测 - 我所知道的是改变顺序立即解决了问题。

我能够将以下内容拼凑到 msdn 帖子中。

这篇文章是导致无效订阅的更常见原因之一,并展示了 .Net 客户端如何设置与通知所需的属性相反的属性。

然后这篇文章来自一位像我一样的用户,他已将其代码简化为最简单的格式。我原来的代码模式与他的类似。

https://social.technet.microsoft.com/Forums/windows/en-US/5a29d49b-8c2c-4fe8-b8de-d632a3f60f68/subscriptions-always-invalid-usual-suspects -checked-no-joy?forum=sqlservicebroker

然后我发现这篇文章,也是一个非常简单的问题简化,只是他的问题很简单 - 需要表的 2 个部分名称。就他而言,该建议解决了问题。查看他的代码后,我注意到主要区别是等待打开连接对象,直到创建命令对象和依赖项对象之后。我唯一的假设是在幕后(我还没有启动反射器来检查,所以只是一个假设)连接对象以不同的方式打开,或者事件和命令发生的顺序不同,因为这种关联。

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/bc9ca094-a989-4403-82c6-7f608ed462ce/sql -server-not-creating-subscription-for-simple-select-query-when-using-sqlcachedependency?forum=sqlservicebroker

我希望这可以帮助遇到类似问题的其他人。

Running into the same issue and finding the same answers online without any help, I was reasearching the xml invalid subsicription response from profiler.

I found an example on msdn support site that had a slightly different order of code. When I tried it I realized the problem - Don't open your connection object until after you've created the command object and the cache dependency object. Here is the order you must follow and all will be good:

  1. Be sure to enable notifications (SqlCahceDependencyAdmin) and run SqlDependency.Start first
  2. Create the connection object
  3. Create the command object and assign command text, type, and connection object (any combination of constructors, setting properties, or using CreateCommand).
  4. Create the sql cache dependency object
  5. Open the connection object
  6. Execute the query
  7. Add item to cache using dependency.

If you follow this order, and follow all other requirements on your select statement, don't have any permissions issues, this will work!

I believe the issue has to do with how the .NET framework manages the connection, specifically what settings are set. I tried overriding this in my sql command test but it never worked. This is only a guess - what I do know is changing the order immediately solved the issue.

I was able to piece it together from the following to msdn posts.

This post was one of the more common causes of the invalid subscription, and shows how the .Net client sets the properties that are in contrast to what notification requires.

https://social.msdn.microsoft.com/Forums/en-US/cf3853f3-0ea1-41b9-987e-9922e5766066/changing-default-set-options-forced-by-net?forum=adodotnetdataproviders

Then this post was from a user who, like me, had reduced his code to the simplest format. My original code pattern was similar to his.

https://social.technet.microsoft.com/Forums/windows/en-US/5a29d49b-8c2c-4fe8-b8de-d632a3f60f68/subscriptions-always-invalid-usual-suspects-checked-no-joy?forum=sqlservicebroker

Then I found this post, also a very simple reduction of the problem, only his was a simple issue - needing 2 part name for tables. In his case the suggestion resolved the issue. After looking at his code I noticed the main difference was waiting to open the connection object until AFTER the command object AND the dependency object were created. My only assumption is under the hood (I have not yet started reflector to check so only an assumption) the Connection object is opened differently, or order of events and command happen differently, because of this association.

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/bc9ca094-a989-4403-82c6-7f608ed462ce/sql-server-not-creating-subscription-for-simple-select-query-when-using-sqlcachedependency?forum=sqlservicebroker

I hope this helps someone else in a similar issue.

哭了丶谁疼 2024-08-14 02:34:21

只是猜测,但这可能是因为您的 SELECT 语句没有 ORDER BY 子句吗?

如果您没有指定显式顺序,则查询每次运行时都可能以任意顺序返回结果。也许这导致 SqlCacheDependency 对象认为结果已更改。

尝试添加一个 ORDER BY 子句:

SELECT ID, Name, Flag, IsDefault
FROM
(
    SELECT ROW_NUMBER() OVER (ORDER BY @OrderBy DESC) AS Row,
        ID, Name, Flag, IsDefault
    FROM dbo.Languages
) AS results
WHERE Row BETWEEN ((@Page - 1) * @ItemsPerPage + 1) AND (@Page * @ItemsPerPage)
ORDER BY Row

Just a guess, but could it be because your SELECT statement doesn't have an ORDER BY clause?

If you don't specify an explicit ordering then it's possible for the query to return the results in any order each time it is run. Maybe this is causing the SqlCacheDependency object to think that the results have changed.

Try adding an ORDER BY clause:

SELECT ID, Name, Flag, IsDefault
FROM
(
    SELECT ROW_NUMBER() OVER (ORDER BY @OrderBy DESC) AS Row,
        ID, Name, Flag, IsDefault
    FROM dbo.Languages
) AS results
WHERE Row BETWEEN ((@Page - 1) * @ItemsPerPage + 1) AND (@Page * @ItemsPerPage)
ORDER BY Row
锦欢 2024-08-14 02:34:21

我不是 SqlCacheDependency 方面的专家,事实上,我在寻找我自己的问题的答案时发现了这个问题!但是,我相信您的 SqlCacheDependency 不起作用的原因是您的 SQL 包含嵌套子查询。

查看文档,其中列出了您可以/不可以在 SQL 中使用的内容: 创建通知查询

“......该语句不得包含子查询、外部联接或自联接......”

我还发现了一些宝贵的故障排除信息Redgate 的一个人在这里: 使用和监视 SQL 2005 查询通知帮助我解决了我自己的问题:通过使用 Sql Profiler 跟踪他建议的 QN 事件,我能够发现我的连接错误地使用了“SET ARITHABORT OFF”选项,导致我的通知失败。

i'm no expert on SqlCacheDependency, in fact, i found this question whilst looking for answers to my own issues with it! However, i believe the reason your SqlCacheDependency is not working is because your SQL contains a nested sub query.

Take a look at the documentation which lists what you can/can not use in your SQL: Creating a Query for Notification

"....The statement must not contain subqueries, outer joins, or self-joins....."

I also found some invaluable troubleshooting info from a guy at Redgate here: Using and Monitoring SQL 2005 Query Notification that helped me solve my own problem: By using Sql Profiler to trace the QN events he suggests, i was able to spot my connection was incorrectly using the 'SET ARITHABORT OFF' option, causing my notifications to fail.

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