一个生活中的事实是,组织有不同的应用程序使用相同类的不同版本,因为它们是在不同的时间点开发的,并且无法跟上。 这些应用程序经常需要彼此共享数据也是不争的事实。
您可以通过数据库执行此操作。 但是,这很慢且不可扩展。 您真正想要的是通过 分布式缓存 这是快速且可扩展的。 但是,运行时的对象共享会立即引发版本兼容性问题。
一种方法是通过 XML,您可以在其中将对象的一个版本转换为另一个版本。 但它非常慢。 另一种方法是实现您自己的自定义转换,该转换采用一个对象版本并将其转换为另一个版本。 但是,你必须保持这一点,这对你来说是一个很大的努力。
如果分布式缓存以某种方式为您处理版本兼容性不是很好吗? 好, NCache 正是这样做的。 NCache 为您提供不同版本之间的二进制级对象转换。 您可以通过 XML 配置文件映射不同的版本,并 NCache 了解如何从一个版本转换为另一个版本。
另外,由于 NCache 以二进制格式(而不是 XML)存储所有这些不同的版本,数据大小保持非常紧凑和小,因此速度很快。 在高流量环境中,对象大小加起来会消耗大量额外的带宽,这有其自身的成本。
这是一个例子 NCache 带有类版本映射的 config.ncconf:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<cache-config name="myInteropCache" inproc="False" config-id="0" last-modified="" type="clustered-cache" auto-start="False"> ... <data-sharing> <type id="1001" handle="Employee" portable="True"> <attribute-list> <attribute name="_Name" type="System.String" order="1"> <attribute name="_Age" type="System.Int32" order="2"> <attribute name="_Address" type="System.String" order="3"> <attribute name="_ID" type="System.String" order="4"> <attribute name="_PostalAddress" type="System.String" order="5"> </attribute></attribute></attribute></attribute></attribute></attribute-list> <class name="DataModel.Employee:1.0.0.0" handle-id="1" assembly="DataModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" type="net"> <attribute name="_Name" type="System.String" order="1"> <attribute name="_Age" type="System.Int32" order="2"> <attribute name="_Address" type="System.String" order="3"> </attribute></attribute></attribute></class> <class name="DataModel.Employee:2.0.0.0" handle-id="2" assembly="DataModel, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" type="net"> <attribute name="_ID" type="System.String" order="4"> <attribute name="_Name" type="System.String" order="1"> <attribute name="_Age" type="System.Int32" order="2"> <attribute name="_PostalAddress" type="System.String" order="3"> </attribute></attribute></attribute></attribute></class> </type> </data-sharing> ... </cache-config> |
如何 NCache 会吗
在上面看到的 config.nconf 文件中,您会注意到 Employee 类首先定义了一组属性。 这些是与版本无关的属性,出现在所有版本中。 这实际上是出现在不同版本中的所有属性的超集。 在此之下,您指定特定于版本的属性并将它们映射到上面的与版本无关的属性。
假设您保存了 Employee 版本 1.0.0.0,它具有 Employee 版本 2.0.0.0 的子集。 现在,当另一个应用程序尝试获取相同的 Employee,但它希望将其视为 2.0.0.0 版本时, NCache 知道哪些 2.0.0.0 版本的属性要填充数据,哪些要留空。
其次,在上面的示例配置中,您会注意到 Employee 版本 2.0.0.0 没有地址字段,即使版本 1.0.0.0 有它。 所以,在这种情况下,当 NCache 尝试读取存储在缓存中的 Employee 1.0.0.0 并尝试将其转换为 Employee 版本 2.0.0.0,它知道不要复制 Address 字段,因为它在这个较新的版本中不存在。
还有很多其他场景 NCache 为您无缝处理。 请阅读在线产品文档以获取更多详细信息。
最后,所有这一切中最好的部分是您无需编写任何序列化代码或对应用程序进行任何代码更改即可使用它 NCache 功能。 NCache 实现了运行时代码生成机制,在运行时为你的类生成内存中的序列化和反序列化代码,并使用非常快的编译形式。
总之,使用 NCache 您现在可以在您的应用程序之间共享不同的对象版本,甚至无需修改您的应用程序代码。