应使用哪些技术来处理 ASP.NET 缓存中的对象,以防止多个 ASP.NET 工作线程访问出现问题?

发布于 2024-09-17 22:32:45 字数 249 浏览 4 评论 0原文

因为 ASP.NET 包含多个同时执行的线程。
因此,如果 2 个线程访问我从 asp.net httpcontext 缓存获取的对象(简单或复杂)。
如果这两个头尝试同时修改/读取该对象,这不会导致该对象出现状态问题吗?
那么我应该采取什么样的预防措施?
例如,我想在使用对象时可能会锁定该对象? (这会导致性能问题吗?)
或者也许当我从缓存中检索某个对象时,我应该从中创建一个副本?
或者也许我根本不需要担心这个问题?
谢谢

since asp.net contains multiple threads that are executing at the same time.
so if 2 threads access an object (simple or complex) that i got from the the asp.net httpcontext Cache.
can't this lead to state problems on that object if these 2 theads tried to modify/read it at the same time?
so what kind of precautions should i implement?
for example i am thinking maybe locking the object while working with it? (wont this cause performance problems?)
or maybe when i retrieve some object from the cache i should create a copy from it?
or maybe i dont need to worry about this issue at all?
thanks

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

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

发布评论

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

评论(2

诺曦 2024-09-24 22:32:45

您需要根据问题的上下文来决定这一点,一刀切的解决方案在这里不起作用。如果您只是读取数据,那么您将不会遇到线程问题。如果您经常向其中写入数据,则使用缓存毫无意义。如果它有点混合并且缓存确实有助于提高性能等,那么您要么需要诉诸正常的线程同步技术(例如读写锁),要么使您的对象不可变,其中对对象的更改总是创建一个新对象。这种选择会导致其自身的线程问题,因为新对象必须替换缓存中的旧对象。

You need to decide this based on the context of your problem, a one size fits all solution won't work here. If you are only reading data, then you will have no threading issues. If you are writing data to this frequently, its pointless using the cache. If its a bit of a mixture and caching does help with performance etc, then you either need to resort to normal thread synchronisation techniques (e.g. reader writer locks) or perhaps make your object immutable where changes to your object always create a new object. That choice leads to threading problems of its own as the new object has to then replace the old object in cache.

手心的温暖 2024-09-24 22:32:45

在 5 年多的 ASP.NET 开发项目中,我从未遇到过需要担心的情​​况。

话虽这么说...您在阅读任何项目时肯定不会遇到问题。如果您有一个想要经常修改的对象,为什么它首先会出现在缓存中?如果不需要经常修改它,那么锁定对象不会成为性能问题。

In 5+ years of ASP.NET development projects, I've never come across a situation where this was a worry.

That being said... you're certainly not going to have a problem reading any items. If you have an object that you want to modify often, why is it in the cache to begin with? If you don't need to modify it often, then locking the object won't be a performance problem.

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