• redis存取对象


    redis主要存储类型最常用的五种数据类型:

    • String
    • Hash
    • List
    • Set
    • Sorted set

    redis不能直接存取对象,如何解决呢?

    两种方式

    1、利用序列化和反序列化的方式

    两层对象存取到redis 示例:

    序列化工具类

    public class SerializeUtil {
        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) {
            }
            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 class Person implements Serializable {
        private String pid;
        private String pName;
        private String nation;
        private String age;
        private Animal animal;
    

      

    1 public class Animal implements Serializable {
    2     private String aId;
    3     private String color;
    4     private String hobby;
    5     private String aName;

    在此强调必须让类实现序列化

     1 public class RedisObject {
     2     public static void main(String[] args) {
     3         //连接本地的 Redis 服务
     4         Jedis jedis = new Jedis("10.0.34.52",6379);
     5         jedis.auth("");
     6         System.out.println("Connection to server sucessfully");
     7         //查看服务是否运行
     8         System.out.println("Server is running: " + jedis.ping());
     9         Person person = new Person();
    10         person.setAge("15");
    11         person.setNation("美国");
    12         person.setpName("jim");
    13         Animal animal = new Animal();
    14         animal.setaId("21");
    15         animal.setaName("小白");
    16         animal.setColor("red");
    17         person.setAnimal(animal);
    18         System.out.println("序列化中");
    19         jedis.set("person".getBytes(),SerializeUtil.serialize(person));
    20         System.out.println("序列化成功");
    21         byte[] bytes = jedis.get("person".getBytes());
    22         Object object = SerializeUtil.unserialize(bytes);
    23         Person person1= (Person)object;
    24         System.out.println(person1);

    2:将java对象转换为json字符串,利用json与java对象之间可以相互转换的方式进行存值和取值

    try {
    String s = JacksonUtils.getInstance().writeValueAsString(person);
    System.out.println("对象转化字符串:"+s);
    jedis.set("person2",s);
    String person2 = jedis.get("person2");
    Person person3 = JacksonUtils.getInstance().readValue(person2, Person.class);
    System.out.println(person3);
    } catch (IOException e) {
    e.printStackTrace();
    }

    
    

    
    
    
    想要体面生活,又觉得打拼辛苦;想要健康身体,又无法坚持运动。人最失败的,莫过于对自己不负责任,连答应自己的事都办不到,又何必抱怨这个世界都和你作对?人生的道理很简单,你想要什么,就去付出足够的努力。
  • 相关阅读:
    填充与复制
    张量排序
    数据统计
    合并与分割
    前向传播(张量)- 实战
    数学运算
    Broadcasting
    TensorFlow2-维度变换
    集合3
    集合2
  • 原文地址:https://www.cnblogs.com/potentPrince/p/8668544.html
Copyright © 2020-2023  润新知