我需要在我的mvc项目上缓存数据.什么是正确的方法?

I Need to cache data on my mvc project. what's proper way?
2021-07-22
  •  译文(汉语)
  •  原文(英语)

我有一个mvc项目.我们有一些必须为每个用户收集的数据.我创建了一些单例类来缓存数据,以更好地承担责任.有些人在他们的项目中避免使用单例类.我想知道您是否喜欢这个,如果不喜欢,您的替代品是什么?

解决过程1

99%的正确方法是不要重新发明轮子.如果您不满意System.Web.Caching.Cache,则可以System.Runtime.Caching.MemoryCache按用户使用实例.

解决过程2

我看不出您不应该在单例中缓存数据的原因.我还使用这种缓存方式来大大减少数据库调用的次数.只需确保您使缓存保持最新状态即可.

我不知道您的项目及其环境,但就我而言,有时候数据库中的数据是从另一个应用程序更改的.我实现了数据库触发器来解决此问题.在这种情况下,您应该考虑做同样的事情,因此,每当数据库中某些本地缓存的数据发生更改时,您都会收到通知并可以更新缓存.

我想我不必提这件事,但是缓存通常只对不经常更改的相对静态数据有意义.

I've got an mvc project. we have some data that must collected for each user. i created some singleton class to cache data for better responsibility. somebodies avoided using singleton class in their projects. i want to know do you prefer this and if not, what's your replacement??

Solutions1

The proper way in 99% is not reinventing the wheel. If you're not all right with System.Web.Caching.Cache, you can use System.Runtime.Caching.MemoryCache instance per user.

Solutions2

I don't see a reason why you should not cache data in your singleton. I'm also using this way of caching in order to greatly reduce the amount of database calls. Just make sure that you keep your cache up to date.

I don't know your project and its environment, but in my case it there were situations when the data in the DB gets changed from another application. I implemented a database trigger to resolve this issue. You should consider doing the same in your case, so whenever some of your locally cached data changes in the DB, you get notified and can update the cache.

I think I don't have to mention this, but caching of course usually only makes sense for relatively static data that does not change very often.

转载于:https://stackoverflow.com/questions/17377319/i-need-to-cache-data-on-my-mvc-project-whats-proper-way

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入