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

随心笔谈2年前发布 admin
166 0 0

文章摘要

这篇文章描述了一个Java类,该类实现了`toByteArray`和`fromByteArray`方法,用于将对象的状态信息编码为字节数组,并将其反向解码回原始对象状态。`toByteArray`方法使用`MessageBufferPacker`将各字段(如`testStatus`、`userPin`、`investor`等)进行编码,而`fromByteArray`方法则通过`MessageUnpacker`从字节数组中提取这些字段并恢复对象状态。文章重点介绍了编码和解码的过程,以及如何处理字段编码失败的情况。

 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());
}
}

© 版权声明

相关文章