AppFabric中/ NCACHE - 将这些解决HttpRuntime.Cache对象通过引用更新? [英] AppFabric/NCache - will these solve HttpRuntime.Cache objects being updated by reference?

查看:722
本文介绍了AppFabric中/ NCACHE - 将这些解决HttpRuntime.Cache对象通过引用更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用ASP.NET应用程序HttpRuntime.Cache,从缓存随后被更新将导致缓存的对象(通过引用)也被更新检索到的任何物件。随后从高速缓存读取将得到更新后的值,这可能不是所期望的。

有关于这个问题的多个职位,例如:

为只读 阅读HttpRuntime.Cache项目p>

和建议的解决方案是使用二进制序列来创建一个深拷贝克隆。

二进制序列的问题是,它的速度慢(慢得令人难以置信),我不能承受任何潜在的性能瓶颈。我看过使用反射深拷贝和而这似乎是更好的表演,这不是小事与我们复杂​​的DTO落实。任何有兴趣,这可能要看看下面的简短的文章:

快速深克隆

有没有人有缓存解决方案,如AppFrabric / NCACHE等任何经验,知道他们是否会直接解决这个问题?

在此先感谢

格里夫


解决方案

喜欢的nCache和AppFabric的产品也存储过程外的一个缓存服务对象之前进行序列化。所以,你还是会采取系列化命中,再加上你会得到通过(甚至通过网络或者)走出去的进程来访问缓存服务序列化对象进一步放缓。

在你的类实现 ICloneable 进行手动调整深拷贝将避免反射,将超越二进制序列化,但如果你的DTO是非常复杂的,这可能是不实际的。

更新,以提供细节:

AppFabric的usese的NetDataContractSerializer序列化(所描述的这里)。该NetDataContractSerializer可以比的BinaryFormatter快一点,但其性能通常是在同一范围:<一href=\"http://blogs.msdn.com/b/youssefm/archive/2009/07/10/comparing-the-performance-of-net-serializers.aspx\" rel=\"nofollow\">http://blogs.msdn.com/b/youssefm/archive/2009/07/10/comparing-the-performance-of-net-serializers.aspx

nCache的推出自己的序列化器,被称为紧凑型系列化。您需要为实现自己的ICompactSerializable接口上的DTO类和读/手工编写所有成员,否则让他们的客户端库检查你的类​​,然后发出了自己的序列化code在运行时为你做这项工作(这是当你的应用程序启动时,他们有反映过来类,放出自己的MSIL)一次性击中。我没有对他们的表现的数据,但它是安全的假设,它比执行反射(Bin​​aryFormatter的/ DataContractSerializer的),并可能在相同的性能领域的protobuf的,MessagePack,并避免过度反映其他串行地方串行更快。更多细节<一个href=\"http://blogs.alachisoft.com/ncache/how-compact-object-serialization-speeds-up-distributed-cache/\"相对=nofollow>这里。

(我为一家公司(ScaleOut软件)这是在同一个空间作为nCache的工作,所以我也许应该更多地了解他们是如何做到的事情ScaleOut让你插上任何你想要的串行 - 我们通常建议的 protobuf网或的 MessagePack ,因为它们通常被认为是.NET序列化的表现卫冕冠军 - 绝对,如果你决定使用串行器,使您的深层副本需要在这两个密切关注。)

When using the HttpRuntime.Cache in an ASP.NET application, any item retrieved from the cache that is then updated will result in the cached object being updated too (by reference). Subsequent reads from the cache will get the updated value, which may not be desirable.

There are multiple posts on this subject, for example:

Read HttpRuntime.Cache item as read-only

And the suggested solution is to create a deep-copy clone using binary serialization.

The problem with binary serialization is that it's slow (incredibly slow), and I can't afford any potential performance bottlenecks. I have looked at deep-copying using reflection and whilst this appears to be better performing, it's not trivial to implement with our complex DTOs. Anyone interested in this may want to have a look at the following brief article:

Fast Deep Cloning

Does anyone have any experience of caching solutions such as AppFrabric / NCache etc and know whether they would solve this problem directly?

Thanks in advance

Griff

解决方案

Products like NCache and AppFabric also perform serialization before storing the object in an out-of-process caching service. So you'd still take that serialization hit, plus you'd get slowed down even further by going out-of-process (or maybe even over the network) to access the serialized object in the caching service.

Implementing ICloneable on your classes to perform hand-tuned deep copies will avoid reflection and will outperform binary serialization, but this may not be practical if your DTOs are very complex.

Updated to provide specifics:

AppFabric usese the NetDataContractSerializer for serialization (as described here). The NetDataContractSerializer can be a little faster than the BinaryFormatter, but its performance is usually in the same ballpark: http://blogs.msdn.com/b/youssefm/archive/2009/07/10/comparing-the-performance-of-net-serializers.aspx

NCache rolled their own serializer, called "Compact Serialization". You need to either implement their ICompactSerializable interface on your DTO classes and read/write all members by hand, or else let their client libraries examine your class and then emit its own serialization code at runtime to do that work for you (it's a one-time hit when your app starts up, where they have to reflect over your class and emit their own MSIL). I don't have data on their performance, but it's safe to assume that it's faster than serializers that perform reflection (BinaryFormatter/DataContractSerializer) and probably somewhere in the same performance realm as protobuf, MessagePack, and other serializers that avoid excessive reflection. More detail is here.

(I work for a company (ScaleOut Software) that's in the same space as NCache, so I should probably know more about how they do things. ScaleOut lets you plug in whatever serializer you want--we usually recommend Protobuf-net or MessagePack, since they're generally considered to be the reigning champions for .NET serialization performance--definitely take a close look at those two if you decide to use a serializer to make your deep copies.)

这篇关于AppFabric中/ NCACHE - 将这些解决HttpRuntime.Cache对象通过引用更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆