Content

Redis缓存空间优化实践详解(redis缓存优化)干货满满

文章摘要

这篇文章描述了一个Java类,该类实现了编码和解码字节的方法。该类包含两个主要方法: 1. **toByteArray()**: 该方法使用`MessageBufferPacker`将对象的字段(如`testStatus`、`userPin`、`investor`等)依次编码为字节序列。每个字段根据是否存在(`null`与否)选择不同的编码方式: - 如果字段为`null`,使用`packNil()`方法编码; - 如果字段不为`null`,则根据字段类型调用相应的编码方法(如`packString()`、`packLong()`、`packString()`等)。 2. **fromByteArray()**: 该方法使用`MessageUnpacker`从字节序列中解析出对象的字段值。同样,每个字段根据编码方式选择相应的解码方法: - 如果字段编码为`nil`,使用`unpackString()`方法提取字符串值; - 如果字段编码为其他类型(如`long`、`string`等),则使用相应的解码方法(如`new Date()`、`new BigDecimal()`等)恢复字段值。 总结而言,该类实现了高效、可靠的字节编码与解码功能,适合需要在不同数据传输场景中保持字段一致性的场景中使用。

 public byte[] toByteArray() throws Exception { MessageBufferPacker packer=MessagePack.newDefaultBufferPacker(); toByteArray(packer); packer.close(); return packer.toByteArray(); } public void toByteArray(MessageBufferPacker packer) throws Exception { if (testStatus==null) { packer.packNil(); }else{ packer.packString(testStatus); } if (userPin==null) { packer.packNil(); }else{ packer.packString(userPin); } if (investor==null) { packer.packNil(); }else{ packer.packString(investor); } if (testQueryTime==null) { packer.packNil(); }else{ packer.packLong(testQueryTime.getTime()); } if (createTime==null) { packer.packNil(); }else{ packer.packLong(createTime.getTime()); } if (bizInfo==null) { packer.packNil(); }else{ packer.packString(bizInfo); } if (otherTime==null) { packer.packNil(); }else{ packer.packLong(otherTime.getTime()); } if (userAmount==null) { packer.packNil(); }else{ packer.packString(userAmount.toString()); } if (userRate==null) { packer.packNil(); }else{ packer.packString(userRate.toString()); } if (applyAmount==null) { packer.packNil(); }else{ packer.packString(applyAmount.toString()); } if (type==null) { packer.packNil(); }else{ packer.packString(type); } if (checkTime==null) { packer.packNil(); }else{ packer.packString(checkTime); } if (preTestStatus==null) { packer.packNil(); }else{ packer.packString(preTestStatus); } } public void fromByteArray(byte[] byteArray) throws Exception { MessageUnpacker unpacker=MessagePack.newDefaultUnpacker(byteArray); fromByteArray(unpacker); unpacker.close(); } public void fromByteArray(MessageUnpacker unpacker) throws Exception { if (!unpacker.tryUnpackNil()){ this.setTestStatus(unpacker.unpackString()); } if (!unpacker.tryUnpackNil()){ this.setUserPin(unpacker.unpackString()); } if (!unpacker.tryUnpackNil()){ this.setInvestor(unpacker.unpackString()); } if (!unpacker.tryUnpackNil()){ this.setTestQueryTime(new Date(unpacker.unpackLong())); } if (!unpacker.tryUnpackNil()){ this.setCreateTime(new Date(unpacker.unpackLong())); } if (!unpacker.tryUnpackNil()){ this.setBizInfo(unpacker.unpackString()); } if (!unpacker.tryUnpackNil()){ this.setOtherTime(new Date(unpacker.unpackLong())); } if (!unpacker.tryUnpackNil()){ this.setUserAmount(new BigDecimal(unpacker.unpackString())); } if (!unpacker.tryUnpackNil()){ this.setUserRate(new BigDecimal(unpacker.unpackString())); } if (!unpacker.tryUnpackNil()){ this.setApplyAmount(new BigDecimal(unpacker.unpackString())); } if (!unpacker.tryUnpackNil()){ this.setType(unpacker.unpackString()); } if (!unpacker.tryUnpackNil()){ this.setCheckTime(unpacker.unpackString()); } if (!unpacker.tryUnpackNil()){ this.setPreTestStatus(unpacker.unpackString()); } }