2019-08-06

redis缓存

操作方法: 

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

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

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

    涉及到的知识点:

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

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

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

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

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

public class ObjectsTranscoder extends SerializeTranscoder {

    @SuppressWarnings("unchecked")

    @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;

    }

    @SuppressWarnings("unchecked")

    @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 {

    @SuppressWarnings("unchecked")

    public List<M> deserialize(byte[] in) {

        List<M> list = new ArrayList<>();

        ByteArrayInputStream bis = null;

        ObjectInputStream is = null;

        try {

            if (in != null) {

                bis = new ByteArrayInputStream(in);

                is = new ObjectInputStream(bis);

                while (true) {

                    M m = (M)is.readObject();

                    if (m == null) {

                        break;

                    }

                    list.add(m);

                }

                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  list;

    }

    @SuppressWarnings("unchecked")

    @Override

    public byte[] serialize(Object value) {

        if (value == null)

            throw new NullPointerException("Can't serialize null");

        List<M> values = (List<M>) value;

        byte[] results = null;

        ByteArrayOutputStream bos = null;

        ObjectOutputStream os = null;

        try {

            bos = new ByteArrayOutputStream();

            os = new ObjectOutputStream(bos);

            for (M m : values) {

                os.writeObject(m);

            }

            // 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);

        }

        return results;

    }

}

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容