首頁  >  文章  >  Java  >  Java redis存Map物件類型資料怎麼實現

Java redis存Map物件類型資料怎麼實現

PHPz
PHPz轉載
2023-04-25 09:19:271671瀏覽

    背景描述

    專案需要將裝置擷取到的最新經緯度資訊存入redis快取中,方便及時查詢檢索。考慮到根據檢索條件不同,所查詢的設備不同。採取將資料以map類型存入redis緩存,在此記錄一下。

    實體類別

    註:一定要實作序列化介面

    父類別

    public class Redis implements Serializable{
    
        private String name;
        private Integer age;
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
    }

    子類別

    import java.io.Serializable;
    
    public class RedisCustom extends Redis {
    
        private String stuCode;
    
        public String getStuCode() {
            return stuCode;
        }
        public void setStuCode(String stuCode) {
            this.stuCode = stuCode;
        }
    }

    方法1°

    redisTemplate.opsForHash()

    範例程式碼

    @Controller
    @RequestMapping("/redis")
    public class RedisController {
    
        @Autowired
        private RedisTemplate redisTemplate;
    
        /**
         * @param
         * @return
         */
        @RequestMapping(value = "/setRedisData", method = RequestMethod.GET)
        @ResponseBody
        public Map<String, Object> setRedisData() {
    
            RedisCustom redis1 = new RedisCustom();
            redis1.setName("小明");
            redis1.setAge(12);
            redis1.setStuCode("36");
            RedisCustom redis2 = new RedisCustom();
            redis2.setName("小红");
            redis2.setAge(11);
            redis2.setStuCode("24");
    
            //构造存入redis中的map
            Map<String, RedisCustom> redisDataMap = new HashMap<String, RedisCustom>();
            redisDataMap.put(redis1.getName(), redis1);
            redisDataMap.put(redis2.getName(), redis2);
    
            //存入redis
            redisTemplate.opsForHash().putAll("redisTest",redisDataMap);
            //获取缓存内容
            Map<String,RedisCustom> resultMap = redisTemplate.opsForHash().entries("redisTest");
            
            //List<RedisCustom> reslutMapList = redisTemplate.opsForHash().values("redisTest");
            //Set<RedisCustom> resultMapSet = redisTemplate.opsForHash().keys("redisTest");
            //RedisCustom value = (RedisCustom)redisTemplate.opsForHash().get("redisTest","小明");
            
            return ResponseData.success(resultMap);
        }
    }

    結果

    Java redis存Map物件類型資料怎麼實現

    #參考
    https://www.yisu.com/article/246815.htm

    方法2°

    將物件轉換為byte[]

    #序列化及反序列化工具類別

    import java.io.*;
    
    /**
     * 序列化及反序列化工具类
     */
    public class SerializeObjectTool {
        //序列化
        public static byte[] serialize(Object obj) {
            ObjectOutputStream obi = null;
            ByteArrayOutputStream bai = null;
            try {
                bai = new ByteArrayOutputStream();
                obi = new ObjectOutputStream(bai);
                obi.writeObject(obj);
                byte[] byt = bai.toByteArray();
                return byt;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        // 反序列化
        public static Object unserizlize(byte[] byt) {
            ObjectInputStream oii = null;
            ByteArrayInputStream bis = null;
            bis = new ByteArrayInputStream(byt);
            try {
                oii = new ObjectInputStream(bis);
                Object obj = oii.readObject();
                return obj;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    範例程式碼

    @Controller
    @RequestMapping("/redis")
    public class RedisController {
        /**
         * @param
         * @return
         */
        @RequestMapping(value = "/setRedisData", method = RequestMethod.GET)
        @ResponseBody
        public Map<String, Object> setRedisData() {
        
            RedisCustom redis1 = new RedisCustom();
            redis1.setName("小明");
            redis1.setAge(12);
            redis1.setStuCode("36");
            RedisCustom redis2 = new RedisCustom();
            redis2.setName("小红");
            redis2.setAge(11);
            redis2.setStuCode("24");
    
            //构造存入redis中的map
            Map<String, RedisCustom> redisDataMap = new HashMap<String, RedisCustom>();
            redisDataMap.put(redis1.getName(), redis1);
            redisDataMap.put(redis2.getName(), redis2);
    
            //连接redis
            Jedis redis = new Jedis("xx.xx.xxx.xx", 6379);
            redis.auth("xxxxxxxxxxx");
            
            //存
            byte[] personByte = SerializeObjectTool.serialize(redisDataMap);
            redis.set("redisData".getBytes(), personByte);
            //取
            byte[] byt = redis.get("redisData".getBytes());
            Object obj = SerializeObjectTool.unserizlize(byt);
    
            Map<String, RedisCustom> redisData = (Map<String, RedisCustom>) obj;
    
            return ResponseData.success(redisData);
        }
    }

    以上是Java redis存Map物件類型資料怎麼實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!

    陳述:
    本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除