如何在我的Asp.net Mvc中使用linq2sql存储库实现缓存策略?

时间:2023-02-02 04:14:55

I dont know if I should use the httpcontext caching or the Enterprise Library Caching Application Block. Also, what is the best pattern for the caching Strategy when deleting or updating an entity that is part of a cached list?

我不知道我是否应该使用httpcontext缓存或企业库缓存应用程序块。此外,在删除或更新作为缓存列表一部分的实体时,缓存策略的最佳模式是什么?

Should I remove all of a list from the cache or only remove the item from the cached list? If I update it will I remove the list from the cache or update the entity in it.

我应该从缓存中删除所有列表还是仅从缓存列表中删除该项?如果我更新它,我将从缓存中删除列表或更新其中的实体。

3 个解决方案

#1


2  

There are several approaches to implement caching,httpcontext being the easiest one, but it's not necessarily the worst. Take a look at memcached or MS Velocity, both of which can be used as backends for the ASP.NET Cache. Especially memcached has a reputation of doing a really good job.

实现缓存有几种方法,httpcontext是最简单的方法,但它不一定是最差的。看一下memcached或MS Velocity,它们都可以用作ASP.NET Cache的后端。特别是memcached的声誉非常出色。

As for caching policy: you have to decide what works best for you.I personally would remove the complete list from the cache upon update/delete rather than trying to find out whether the entity is in the list, since it takes a non-trivial amount of time and you need to take concurrency issues into account (locking the list, since somebody might do an update/delete of another entity). Sometimes it does make sense to update an entity in place (if you have a complete object with all data you need), sometimes it's a waste of time, because due to some state change the entity should move somewhere else (e.g. when you sort by LastChangedDate etc.)

至于缓存策略:你必须决定什么最适合你。我个人会在更新/删除时从缓存中删除完整列表,而不是试图找出实体是否在列表中,因为它需要一个非平凡的时间量,您需要考虑并发问题(锁定列表,因为有人可能会更新/删除另一个实体)。有时更新一个实体是有意义的(如果你有一个包含你需要的所有数据的完整对象),有时这是浪费时间,因为由于一些状态改变,实体应该移动到其他地方(例如当你排序时LastChangedDate等)

Don't forget to optimize your DB code too so that it does not take too much time to refresh the flushed list.

不要忘记优化您的数据库代码,以免刷新刷新列表花费太多时间。

#2


6  

Having done some testing with both I did a full review of the caching application block in the context of our code and blogged my experience with it. It's very simple to use and powerful enough for our needs. I would recommend it, my results were blogged here.

在对两者进行了一些测试之后,我在代码的上下文中对缓存应用程序块进行了全面审查,并在博客上发表了我的经验。它使用起来非常简单,功能强大,足以满足我们的需求。我推荐它,我的结果在这里写博客。

In your position I would use the Repository pattern to maintain my cache, it works well for database datasets and should work equally well for the cache in your own. If you're not familar with the repository pattern, check out this post from Steven Walther.. I would tend to disagree with the previous answer however, taking out only the items you need for modification and laeving the rest untouched. This will allow you to expire items from the cache independantly from the whole list should you so wish.

在您的位置,我将使用存储库模式来维护我的缓存,它适用于数据库数据集,并且应该同样适用于您自己的缓存。如果您不熟悉存储库模式,请查看Steven Walther的这篇文章。但是,我倾向于不同意之前的答案,只取出修改所需的项目,并且不改动其余部分。如果您愿意,这将允许您从整个列表中独立地使缓存中的项目到期。

#3


0  

just use [OutputCache(Duration=10, VaryByParam="none")] on every action or even controller you want to cache.

只需在每个动作甚至您想要缓存的控制器上使用[OutputCache(Duration = 10,VaryByParam =“none”)]。

from http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/improving-performance-with-output-caching-cs

#1


2  

There are several approaches to implement caching,httpcontext being the easiest one, but it's not necessarily the worst. Take a look at memcached or MS Velocity, both of which can be used as backends for the ASP.NET Cache. Especially memcached has a reputation of doing a really good job.

实现缓存有几种方法,httpcontext是最简单的方法,但它不一定是最差的。看一下memcached或MS Velocity,它们都可以用作ASP.NET Cache的后端。特别是memcached的声誉非常出色。

As for caching policy: you have to decide what works best for you.I personally would remove the complete list from the cache upon update/delete rather than trying to find out whether the entity is in the list, since it takes a non-trivial amount of time and you need to take concurrency issues into account (locking the list, since somebody might do an update/delete of another entity). Sometimes it does make sense to update an entity in place (if you have a complete object with all data you need), sometimes it's a waste of time, because due to some state change the entity should move somewhere else (e.g. when you sort by LastChangedDate etc.)

至于缓存策略:你必须决定什么最适合你。我个人会在更新/删除时从缓存中删除完整列表,而不是试图找出实体是否在列表中,因为它需要一个非平凡的时间量,您需要考虑并发问题(锁定列表,因为有人可能会更新/删除另一个实体)。有时更新一个实体是有意义的(如果你有一个包含你需要的所有数据的完整对象),有时这是浪费时间,因为由于一些状态改变,实体应该移动到其他地方(例如当你排序时LastChangedDate等)

Don't forget to optimize your DB code too so that it does not take too much time to refresh the flushed list.

不要忘记优化您的数据库代码,以免刷新刷新列表花费太多时间。

#2


6  

Having done some testing with both I did a full review of the caching application block in the context of our code and blogged my experience with it. It's very simple to use and powerful enough for our needs. I would recommend it, my results were blogged here.

在对两者进行了一些测试之后,我在代码的上下文中对缓存应用程序块进行了全面审查,并在博客上发表了我的经验。它使用起来非常简单,功能强大,足以满足我们的需求。我推荐它,我的结果在这里写博客。

In your position I would use the Repository pattern to maintain my cache, it works well for database datasets and should work equally well for the cache in your own. If you're not familar with the repository pattern, check out this post from Steven Walther.. I would tend to disagree with the previous answer however, taking out only the items you need for modification and laeving the rest untouched. This will allow you to expire items from the cache independantly from the whole list should you so wish.

在您的位置,我将使用存储库模式来维护我的缓存,它适用于数据库数据集,并且应该同样适用于您自己的缓存。如果您不熟悉存储库模式,请查看Steven Walther的这篇文章。但是,我倾向于不同意之前的答案,只取出修改所需的项目,并且不改动其余部分。如果您愿意,这将允许您从整个列表中独立地使缓存中的项目到期。

#3


0  

just use [OutputCache(Duration=10, VaryByParam="none")] on every action or even controller you want to cache.

只需在每个动作甚至您想要缓存的控制器上使用[OutputCache(Duration = 10,VaryByParam =“none”)]。

from http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/improving-performance-with-output-caching-cs