动态紧凑序列化实现
NCache stores application data in serialized form - it is serialized on the cache client before being added to the cache. Therefore, the data you add to the cache must also be marked as serializable. It supports native binary serialization and JSON's information does not travel on the network, and the object size on the network is reduced. As the object size decreases, network trips become faster, and eventually, the cost decreases, hence, increasing the overall application performance.
紧凑序列化也是二进制序列化,它具有以下优点:
紧凑的数据大小
In NCache 二进制数据,序列化对象的大小是紧凑的。 在某些场景下,数据大小可能比本机序列化小 10 倍。 较小的序列化对象在网络上传输所需的时间较短,因此吞吐量较高。 这也有助于减少缓存服务器上的内存消耗。 以这种方式, NCache 实现更好的性能 - 减少时间和成本。 小的对象大小和快速的网络流量提高了应用程序性能。
比本机序列化更快
紧凑序列化比本机序列化更快,因为它不像本机序列化那样依赖反射。 本机序列化使用反射来获取属性信息。 这种反射可以避免 NCache的紧凑序列化。 紧凑类型的类型信息仅在初始化缓存时加载一次,这减少了使用本机序列化所面临的开销。 基准测试表明,在某些用例中,它比本机序列化快 5~10 倍。
无代码更改
无需更改代码即可使类紧凑可序列化。 唯一需要的步骤是通过管理工具( NCache 管理中心或其命令行工具)。
选择性序列化: NCache 允许选择性序列化; 您可以选择要标记为不可序列化的类的数据成员。 任何缓存对象的排除属性要么为空,要么具有默认值(如果已分配)。 此功能在某些属性不需要缓存的场景中非常有用,例如数据库的连接对象。 通过这种选择性序列化,该过程变得更加高效和紧凑。
对紧凑序列化的粒度控制: 为此,您可以实施
ICompactSerializable
。 该接口使您可以对紧凑序列化进行更细粒度的控制,您可以根据业务需求控制要序列化的数据成员以及要排除的数据成员。支持本机序列化: NCache 支持本机二进制序列化。 数据以序列化形式存储在缓存中,.NET 和 Java 客户端均支持客户端本机序列化 NCache.
实现对象类型的紧凑序列化
要使用紧凑序列化,需要通过以下方式配置要紧凑序列化的对象类型 NCache 管理中心. NCache 内部处理注册类的序列化/反序列化,用户不需要为其显式编码。
重要
类属性需要按照序列化顺序进行反序列化,否则序列化流无法反序列化。
然而, NCache 还允许显式实施 ICompactSerializable
界面。
为了实现这个接口,DemoObject类需要实现 Serialize
和 Deserialize
将被调用的方法 NCache 序列化/反序列化该对象时。 下面给出了实现该接口的示例代码:
重要
如果要将一个类标记为需要参数化构造函数的紧凑序列化,请确保该类包含默认构造函数。
先决条件
- 了解与所有人员合作所需的标准先决条件 NCache 服务器端功能请参考给定页面 服务器端 API 先决条件.
- 缓存必须正在运行。
- 申请必须是 连接到缓存 在执行操作之前。
- 有关 API 详细信息,请参阅: 紧凑串行化, 连载, 反序列化, 检查是否可序列化, IGenericTypeProvider.
创建演示对象
public class DemoObject : ICompactSerializable
{
private string _attribute1;
private int _attribute2;
private bool _attribute3;
public DemoObject()
{ }
public DemoObject(string attrib1, int attrib2, bool attrib3)
{
_attribute1 = attrib1;
_attribute2 = attrib2;
_attribute3 = attrib3;
}
#region ICompactSerializable Members
public void Deserialize(CompactReader reader) {
_attribute1 = reader.ReadObject() as string;
_attribute2 = reader.ReadInt32();
_attribute3 = reader.ReadBoolean();
}
public void Serialize(CompactWriter writer){
writer.WriteObject(_attribute1);
writer.Write(_attribute2);
writer.Write(_attribute3);
}
#endregion
}
实现泛型类型处理程序
NCache 还允许在紧凑序列化中注册泛型类型。 为此,每种类型都需要通过 NCache 管理中心。 还有另一种方法可以通过“通用类型处理程序”注册通用紧凑类型。 使用通用类型处理程序,用户可以通过实现以下方式一次性添加所有通用类型 IGenericTypeProvider
界面。 如果用户想要注册大量类型,这可以节省大量时间和精力。
重要
要使用该接口,请在您的应用程序中包含以下命名空间: Alachisoft.NCache.Runtime.GenericTypesProvider
为了实现这一点,用户需要实现 GetGenericTypes
方法并返回所需的泛型类型 Type
大批。 之后,将此类程序集部署到 NCache 管理中心。 该 NCache 管理中心将注册所有类型中填充的 Type
大批。 下面给出示例代码:
重要
如果要将一个类标记为需要参数化构造函数的紧凑序列化,请确保该类包含默认构造函数。
public class CustomGenericType<T, K, V>
{ }
public class SetGenerics : IGenericTypeProvider
{
Type[] types = new Type[6];
Type[] IGenericTypeProvider.GetGenericTypes()
{
types[0] = typeof(Dictionary<string, Customer>);
types[1] = typeof(List<int>);
types[2] = typeof(CustomGenericType<Employee, Product, Customer>);
types[3] = typeof(CustomGenericType<Employee, Product, int>);
types[4] = typeof(CustomGenericType<Employee, string, byte>);
types[5] = typeof(CustomGenericType<string, Customer, bool>);
return types;
}
public bool CheckIfSerializable(Type type, FieldInfo fieldInfo)
{
// Implement logic
}
}
故障排除
紧凑序列化异常
在未注册 Compact Serializable 类型的节点上可能会发生序列化异常。
解决方法
注册紧凑的可序列化 集群中所有节点上的类型。
参见
.NET: Alachisoft.NCache。运行 命名空间。