在我的应用程序中,我们使用C#字典在内存中缓存大型Business对象。
我正在将巨大的多轮引用的C#对象(当JSON序列化时为〜300KB)转换为DTO,以进行Redis缓存的JSON序列化/反序列化。
似乎由于参数化构造函数,我的业务层中的某些类型无法轻松初始化。
有没有一种方法可以在不进行序列化的情况下实现Redis缓存?这样可以退还我缓存的相同对象吗?
好的。我知道我问了这个问题已经很长时间了。但是我们解决了这个问题。我们使用BinaryFormatter序列化程序,该序列化程序实际上将.NET对象转换为内存映射,并将其序列化为二进制数组。该内存映射可用于在没有公共默认构造函数的情况下重新创建原始对象。早期版本的.NET支持此BinaryFormatter序列化。
这种方法的缺点是我们必须手动进入对象层次结构中的每个类,并为Serializable
该类提供Attribute装饰。另一个缺点是,只有包含非null值的属性才被序列化。这意味着,如果您的任何单元测试都没有初始化要序列化的对象的某些或任何属性,那么我们很有可能遇到运行时“序列化异常”,可以通过修饰类型来轻松解决该异常。Serializable
我们也可以使用其他序列化格式,例如MsgPack等。
public class TheTypeToBeCached
{
public OneSubType Property1 {get;set;}
public SecondSubType Property2 {get;set;}
}
public class OneSubType
{
public ThirdSubType Property3 {get;set;}
public ForthSubType Property4 {get;set;}
public AnotherSubType Property5 {get;set;}
}
public class SecondSubType
{
public ForthSubType Property6 {get;set;}
public AnotherSubType Property7 {get;set;}
}
public class ThridSubType
{
public SecondSubType Property8{get;set;}
}
public class ForthSubType
{
public SecondSubType Property9{get;set;}
}
public class AnotherSubType
{
public OneSubType Property10{get;set;}
}
即使具有多个回合关系,也可以使用BinaryFormatter序列化程序对上述层次结构进行序列化和反序列化。
[Serializable]
public class TheTypeToBeCached
{
public OneSubType Property1 {get;set;}
public SecondSubType Property2 {get;set;}
}
[Serializable]
public class OneSubType
{
public ThirdSubType Property3 {get;set;}
public ForthSubType Property4 {get;set;}
public AnotherSubType Property5 {get;set;}
}
[Serializable]
public class SecondSubType
{
public ForthSubType Property6 {get;set;}
public AnotherSubType Property7 {get;set;}
}
[Serializable]
public class ThridSubType
{
public SecondSubType Property8{get;set;}
}
[Serializable]
public class ForthSubType
{
public SecondSubType Property9{get;set;}
}
[Serializable]
public class AnotherSubType
{
public OneSubType Property10{get;set;}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句