简体   繁体   English

应该使用什么技术来处理asp.net缓存中的对象,以防止来自多个asp.net辅助线程访问的问题?

[英]what techniques should be used to handle objects in asp.net Cache to prevent problems from multiple asp.net worker threads access?

since asp.net contains multiple threads that are executing at the same time. 因为asp.net包含多个同时执行的线程。
so if 2 threads access an object (simple or complex) that i got from the the asp.net httpcontext Cache. 因此,如果2个线程访问我从asp.net httpcontext缓存获取的对象(简单或复杂)。
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 谢谢

In 5+ years of ASP.NET development projects, I've never come across a situation where this was a worry. 在5年以上的ASP.NET开发项目中,我从未遇到过这种担心的情况。

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. 如果您不需要经常修改它,那么锁定对象不会是性能问题。

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 (eg 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. 这种选择会导致其自身的线程问题,因为新对象必须随后替换缓存中的旧对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM