首页 » NoSQL » redis存储对象

redis存储对象

原文 http://blog.csdn.net/leo187/article/details/79206138

2018-01-31 02:01:42阅读(475)

redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set –有序集合)和hash(哈希类型)。在实际工作中需要将数据以对象的形式存储,实现如下: 配置redis可以参考 http://blog.csdn.net/leo187/article/details/78704811 配置成功后进行如下操作 先创建一个测试对象goods
package com.project.entity;
import java.io.Serializable;
//实体实现序列化接口
public class EmallGoods  implements Serializable  {
    private Integer goodsid;
    private String goodsname;
    public Integer getGoodsid() {
        return goodsid;
    }
    public void setGoodsid(Integer goodsid) {
        this.goodsid = goodsid;
    }
    public String getGoodsname() {
        return goodsname;
    }
    public void setGoodsname(String goodsname) {
        this.goodsname = goodsname == null ? null : goodsname.trim();
    }
}
序列化、反序列化Util类
package com.project.controller.Redis;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SerializeUtilNew {
      public static byte[] serialize(Object object) {
            ObjectOutputStream oos = null;
            ByteArrayOutputStream baos = null;
            try {
                baos = new ByteArrayOutputStream();
                oos = new ObjectOutputStream(baos);
                oos.writeObject(object);
                byte[] bytes = baos.toByteArray();
                return bytes;
           } catch (Exception e) {
               e.printStackTrace();
           }
            return null;
     }
      public static Object unserialize( byte[] bytes) {
            ByteArrayInputStream bais = null;
            try {
                bais = new ByteArrayInputStream(bytes);
                ObjectInputStream ois = new ObjectInputStream(bais);
                return ois.readObject();
           } catch (Exception e) {
           }
            return null;
     }
}
实例调用
public void selectReviewsByGoodsId(HttpServletRequest request){
            EmallGoods goodsInfo = new EmallGoods();
            goodsInfo.setGoodsid(1000);
            goodsInfo.setGoodsname("晨光 M&G 中性笔 K35 0.5mm(黑色)12支/盒(替芯:G-5)");
            try {
                Jedis jedis = new Jedis();
//以商品id为key,商品对象为value存储              jedis.set(goodsInfo.getGoodsid().toString().getBytes(),SerializeUtilNew.serialize(goodsInfo));
                byte[] value = jedis.get(goodsInfo.getGoodsid().toString().getBytes());
                Object object = SerializeUtilNew.unserialize(value);           
                if(object!= null){
                        EmallGoods goo=(EmallGoods) object;
                        System. out.println(goo.getGoodsname());
                        System. out.println(goo.getGoodsid());
                   }
            } catch (Exception e) {
                System.out.println("登录无法更新该商品缓存");
            }
    }

-打印输出:

晨光 M&G 中性笔 K35 0.5mm(黑色)12支/盒(替芯:G-5)
1000

最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹