redis 入门学习-- java 对象存入redis的2种方式

1、将对象序列化成字节数组,存入String中

/**
     * 将对象缓存到redis的string结构数据中
     * @throws Exception 
     *
     */
    @Test
    public void testObjectCache() throws Exception{
        
            ProductInfo p = new ProductInfo();
            
            p.setName("中华牙膏");
            p.setDescription("美白防蛀");
            p.setCatelog("日用品");
            p.setPrice(10.8);

            //将对象序列化成字节数组
            ByteArrayOutputStream ba = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(ba);
            
            //用对象序列化流来将p对象序列化,然后把序列化之后的二进制数据写到ba流中
            oos.writeObject(p);
            
            //将ba流转成byte数组
            byte[] pBytes = ba.toByteArray();
            
            //将对象序列化之后的byte数组存到redis的string结构数据中
            jedis.set("product:01".getBytes(), pBytes);
        
            //根据key从redis中取出对象的byte数据
            byte[] pBytesResp = jedis.get("product:01".getBytes());
            
            //将byte数据反序列出对象
            ByteArrayInputStream bi = new ByteArrayInputStream(pBytesResp);
            
            ObjectInputStream oi = new ObjectInputStream(bi);
            
            //从对象读取流中读取出p对象
            ProductInfo pResp = (ProductInfo) oi.readObject();
            
            System.out.println(pResp);
    }

 

2、将对象转成json字符串缓存到redis的string结构数据中

/**
* 将对象转成json字符串缓存到redis的string结构数据中
*/
@Test
public void testObjectToJsonCache(){
        
        
        ProductInfo p = new ProductInfo();
        
        p.setName("两面针");
        p.setDescription("防蛀专用");
        p.setCatelog("日用品");
        p.setPrice(10.9);

        //利用gson将对象转成json串
        Gson gson = new Gson();
        String pJson = gson.toJson(p);
        
        //将json串存入redis
        jedis.set("prodcut:02", pJson);
        
        
        //从redis中取出对象的json串
        String pJsonResp = jedis.get("prodcut:02");
        
        //将返回的json解析成对象
        ProductInfo pResponse = gson.fromJson(pJsonResp, ProductInfo.class);
        
        //显示对象的属性
        System.out.println(pResponse);
    }

3、ProductInfo对象代码

import java.io.Serializable;

public class ProductInfo implements Serializable{


    private static final long serialVersionUID = 3002512957989050750L;
    private String name;
    private String description;
    private double price;
    private String catelog;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public String getCatelog() {
        return catelog;
    }
    public void setCatelog(String catelog) {
        this.catelog = catelog;
    }
    
    @Override
    public String toString() {
         
        return name+" "+description + " "+ catelog+ " " + price;
    }
}

 

posted @ 2019-02-15 16:29  Miles_mjy  阅读(4918)  评论(0编辑  收藏  举报