首页 > Java > java教程 > 正文

Java redis存Map对象类型数据怎么实现

PHPz
发布: 2023-04-25 09:19:27
转载
1679 人浏览过

    背景描述

    项目需要将设备采集到的最新经纬度信息存入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
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板