Redis缓存对象的实现原理

      截止到目前为止,在redis官方的文档和实现里面并没有针对object 对象缓存的方法,然而,在我们的实际开发需要中,在很多时候我们是需要进行对象缓存的,并且可以正确的读取出来! 在笔者正在开发的红包项目中,针对每天红包就需要使用的对象缓存,并可以随时修改缓存对象中的红包数量值等信息!那么具体实现呢?

       在官方提供的方法中,我们找到了有这么一个操作方法: 

jedis.set(byte[], byte[])

看这个方法,是进行字节码操作的,这让我们很容易想到在一些远程方法调用中,我们传递对象同样传递的是字节码,是不是可以参考呢?

首先,既然需要对对象进行字节操作,即可写和可读的操作,为了保证这个原则,那么缓存对象需要实现Serializable 接口,进行序列化和反序列化!

涉及到的知识点:

1: Serializable (接口,实现此接口的对象可以进行序列化)

2: ByteArrayOutputStream,ObjectOutputStream 对象转换为字节码输出流

3: ByteArrayInputStream ,ObjectInputStream 字节码转换为对象的输入流

了解了如上三点知识后,我们就可以对对象进行缓存操作了!

示例代码如下,包括了对象缓存和List 对象数组缓存,需要声明的是,放入list数组中的对象同样需要实现Serializable 接口:

public class ObjectsTranscoder extends SerializeTranscoder {
<span style="color:#808000;">@SuppressWarnings</span>(<span style="color:#008000;"><strong>"unchecked"</strong></span>)
<span style="color:#808000;">@Override

public byte[] serialize(Object value) {
if (value == null) {
throw new NullPointerException(“Can’t serialize null”);
}
byte[] result = null;
ByteArrayOutputStream bos = null;
ObjectOutputStream os = null;
try {
bos = new ByteArrayOutputStream();
os = new ObjectOutputStream(bos);
os.writeObject(value);
os.close();
bos.close();
result = bos.toByteArray();
} catch (IOException e) {
throw new IllegalArgumentException(“Non-serializable object”, e);
} finally {
close(os);
close(bos);
}
return result;
}

<span style="color:#808000;">@SuppressWarnings</span>(<span style="color:#008000;"><strong>"unchecked"</strong></span>)
<span style="color:#808000;">@Override

public Object deserialize(byte[] in) {
Object result = null;
ByteArrayInputStream bis = null;
ObjectInputStream is = null;
try {
if (in != null) {
bis = new ByteArrayInputStream(in);
is = new ObjectInputStream(bis);
result = is.readObject();
is.close();
bis.close();
}
} catch (IOException e) {
logger.error(String.format(“Caught IOException decoding %d bytes of data”,
in == null ? 0 : in.length) + e);
} catch (ClassNotFoundException e) {
logger.error(String.format(“Caught CNFE decoding %d bytes of data”,
in == null ? 0 : in.length) + e);
} finally {
close(is);
close(bis);
}
return result;
}
}

对象的转换示例如上代码:

数组缓存代码:

public class ListTranscoder<M extends Serializable> extends SerializeTranscoder {
<span style="color:#808000;">@SuppressWarnings</span>(<span style="color:#008000;"><strong>"unchecked"</strong></span>)
<span style="color:#000080;"><strong>public </strong></span>List&lt;<span style="color:#20999d;">M</span>&gt; deserialize(<span style="color:#000080;"><strong>byte</strong></span>[] in) {
    List&lt;<span style="color:#20999d;">M</span>&gt; list = <span style="color:#000080;"><strong>new </strong></span>ArrayList&lt;&gt;();
    ByteArrayInputStream bis = <span style="color:#000080;"><strong>null</strong></span>;
    ObjectInputStream is = <span style="color:#000080;"><strong>null</strong></span>;
    <span style="color:#000080;"><strong>try </strong></span>{
        <span style="color:#000080;"><strong>if </strong></span>(in != <span style="color:#000080;"><strong>null</strong></span>) {
            bis = <span style="color:#000080;"><strong>new </strong></span>ByteArrayInputStream(in);
            is = <span style="color:#000080;"><strong>new </strong></span>ObjectInputStream(bis);
            <span style="color:#000080;"><strong>while </strong></span>(<span style="color:#000080;"><strong>true</strong></span>) {
                <span style="color:#20999d;">M </span>m = (<span style="color:#20999d;">M</span>)is.readObject();
                <span style="color:#000080;"><strong>if </strong></span>(m == <span style="color:#000080;"><strong>null</strong></span>) {
                    <span style="color:#000080;"><strong>break</strong></span>;
                }

                list.add(m);

            }
            is.close();
            bis.close();
        }
    } <span style="color:#000080;"><strong>catch </strong></span>(IOException e) {
        <span style="color:#660e7a;"><em>logger</em></span>.error(String.<span style="font-style:italic;">format</span>(<span style="color:#008000;"><strong>"Caught IOException decoding %d bytes of data"</strong></span>,
                in == <span style="color:#000080;"><strong>null </strong></span>? <span style="color:#0000ff;">0 </span>: in.<span style="color:#660e7a;"><strong>length</strong></span>) + e);
    } <span style="color:#000080;"><strong>catch </strong></span>(ClassNotFoundException e) {
        <span style="color:#660e7a;"><em>logger</em></span>.error(String.<span style="font-style:italic;">format</span>(<span style="color:#008000;"><strong>"Caught CNFE decoding %d bytes of data"</strong></span>,
                in == <span style="color:#000080;"><strong>null </strong></span>? <span style="color:#0000ff;">0 </span>: in.<span style="color:#660e7a;"><strong>length</strong></span>) + e);
    }  <span style="color:#000080;"><strong>finally </strong></span>{
        close(is);
        close(bis);
    }

    <span style="color:#000080;"><strong>return  </strong></span>list;
}


<span style="color:#808000;">@SuppressWarnings</span>(<span style="color:#008000;"><strong>"unchecked"</strong></span>)
<span style="color:#808000;">@Override

public byte[] serialize(Object value) {
if (value == null)
throw new NullPointerException(“Can’t serialize null”);

    List&lt;<span style="color:#20999d;">M</span>&gt; values = (List&lt;<span style="color:#20999d;">M</span>&gt;) value;

    <span style="color:#000080;"><strong>byte</strong></span>[] results = <span style="color:#000080;"><strong>null</strong></span>;
    ByteArrayOutputStream bos = <span style="color:#000080;"><strong>null</strong></span>;
    ObjectOutputStream os = <span style="color:#000080;"><strong>null</strong></span>;

    <span style="color:#000080;"><strong>try </strong></span>{
        bos = <span style="color:#000080;"><strong>new </strong></span>ByteArrayOutputStream();
        os = <span style="color:#000080;"><strong>new </strong></span>ObjectOutputStream(bos);
        <span style="color:#000080;"><strong>for </strong></span>(<span style="color:#20999d;">M </span>m : values) {
            os.writeObject(m);
        }

        <span style="color:#808080;"><em>// os.writeObject(null);

os.close();
bos.close();
results = bos.toByteArray();
} catch (IOException e) {
throw new IllegalArgumentException(“Non-serializable object”, e);
} finally {
close(os);
close(bos);
}

    <span style="color:#000080;"><strong>return </strong></span>results;
}

}

通过以上操作即可以实现对象的缓存和读取了!

虽然自己实现了,还是希望redis官方尽快提供对象的缓存操作吧!



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值