ASP.NET 使用 SqlCommand 设置缓存依赖项
这是根据查询设置缓存项的有效方法吗?
HttpRuntime.Cache.Insert(
"ListLanguages",
list,
new SqlCacheDependency(command),
DateTime.UtcNow.AddMinutes(AppConfiguration.CacheExpiration.MinimumActivity),
Cache.NoSlidingExpiration);
command 是一个 SqlCommand,之前初始化为:
SqlCommand command = new SqlCommand("Listlanguages", connection);
其中“ListLanguages”是一个存储过程,它只是一个选择。
我发现这是一种比聚合缓存依赖更简单、更防故障的方法(我的意思是防故障,因为我不必自己聚合表!:)。
更有经验的程序员怎么想?
Is this an effective way to set the cache item dependent on the query?
HttpRuntime.Cache.Insert(
"ListLanguages",
list,
new SqlCacheDependency(command),
DateTime.UtcNow.AddMinutes(AppConfiguration.CacheExpiration.MinimumActivity),
Cache.NoSlidingExpiration);
command is a SqlCommand initialized previously as:
SqlCommand command = new SqlCommand("Listlanguages", connection);
where "ListLanguages" is a stored procedure which is simply a select.
I find this an easier and more failure-proof method than aggregated cache dependency (I mean failure-proof because I don't have to aggregated the tables myself!:).
What do more experienced programmers think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不需要使用存储过程,该命令可以直接基于其中包含的 select 语句。
就我个人而言,我避免使用 SqlCacheDependency,我总是担心查询中可能有一些其所基于的代理系统无法处理的内容,而且我总是记不住它们是什么。它看起来也有点太复杂了,所以我担心它可能会脆弱。
编辑
在用户更新其个人资料的特定情况下,我会让更新个人资料的代码删除缓存的副本。
从更一般的意义上来说,我会建立一个可接受的延迟来接收最新信息,并将绝对到期时间设置为该延迟。
如果 SQL 查询成本高昂,我会考虑在其他表中暂存通用摘要,并使用更新此数据(例如 SP)的代码来调整或删除暂存数据。
我并不是说我永远不会使用 SqlCacheDepencency,但到目前为止,我还没有遇到过它是唯一明智的选择的场景,尽管我确信它们存在。我想当您无法完全控制可能修改数据库的所有代码时,可能会出现这种情况。
什么是“最新”?
在 Web 应用程序中,用户可能看到的最新信息是最后响应中提供的信息。以下是一些需要考虑的事项。
我要指出的一点是,无论我们在系统中投入多少缓存智能,都会不可避免地存在延迟,并且我们无需多加考虑就接受这种延迟。
考虑到这一点,在许多情况下,我们可能觉得有义务提供最新信息,但这种义务实际上并没有得到保证。 SO 本身就是一个很好的例子。我们在这里看到的许多查询结果实际上都被缓存了,并且可能会看到与我们所知道的所做的更改不太一致的数据。然而,其他人并不知道我们的改变,而且他们在我们做出改变的那一刻就看到这些改变并不重要。
I don't think you need to use a stored procedure, the command can be based on the select statement that is contained within it directly.
Personally I avoid SqlCacheDependency, I'm always concerned that the query might just have something in it that the broker system its based on doesn't cope with and I can't always remember what they are. It also just seems a little too complex under-the-hood so I worry that it might be on the fragile side.
Edit
In the specific case of a user updating their profile I would have the code that updates the profile delete the cached copy.
In a more general sense I would establish an acceptable latency for receiving up-to-date info and set the absolute expiration to that.
In case of expensive SQL queries I would consider staging common summaries in other tables and have code that updates this data (such as SPs) adjust or delete the staged data.
I'm not saying I would never use SqlCacheDepencency but so far I haven't come across a scenario where its the only sensible option although I'm sure they exist. I guess such scenarios could arise where you are not in complete control of all code that may modify the database.
What is "up-to-date" anyway?
In a Web application the latest information a user can possibly see is that provided in the last response. Here are some things to consider.
The point I'm leading to is that no matter how much cache cleverness we put into systems there is an inevitable latency and that we accept this sort of latency without giving it much thought.
With that in mind in many situations where we might feel obligated to deliver the very latest info such obligation isn't really warranted. A good example of this is SO itself. Many of the query results we see here are actually cached and its possible to see data that isn't quite in line with changes that we know we've made. However other people are unaware of our changes and it isn't critical that they see them the very second we've made them.