redis storage object

Posted by endersix on Mon, 04 May 2020 17:53:44 +0200

  • redis is a key value storage system. Similar to Memcached, it supports more value types, including string, list, set, Zset and hash. In the actual work, the data needs to be stored in the form of objects, and the implementation is as follows:
  • For redis configuration, please refer to http://blog.csdn.net/leo187/article/details/78704811
  • After the configuration is successful, perform the following operations
  • First create a test object good
package com.project.entity;

import java.io.Serializable;
//Entity implementation serialization interface
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();
    }
}
  • Serialize, deserialize Util class
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;
     }
}
  • Instance call
public void selectReviewsByGoodsId(HttpServletRequest request){
            EmallGoods goodsInfo = new EmallGoods();
            goodsInfo.setGoodsid(1000);
            goodsInfo.setGoodsname("Dawn M&G Roller ball pen K35 0.5mm(Black) 12/Box (core replacement: G-5)");
            try {
                Jedis jedis = new Jedis();
//Store jedis.set(goodsInfo.getGoodsid().toString().getBytes(),SerializeUtilNew.serialize(goodsInfo)) with commodity id as key and commodity object as value;
                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("Login failed to update the product cache");
            }
    }

-Printout:

Chenguang M & G neutral pen K35 0.5mm (black) 12 pieces / box (core replacement: G-5)
1000

Topics: Jedis Java Redis