Home> Java> javaTutorial> body text

How to implement Java redis to store Map object type data

PHPz
Release: 2023-04-25 09:19:27
forward
1600 people have browsed it

    Background Description

    The project needs to store the latest latitude and longitude information collected by the device into the redis cache to facilitate timely query and retrieval. Considering that the equipment queried is different depending on the search conditions. The data is stored in the redis cache as a map type and is recorded here.

    Entity class

    Note: Be sure to implement the serialization interface

    Parent class

    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; } }
    Copy after login

    Subclass

    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; } }
    Copy after login

    Method 1°

    redisTemplate.opsForHash()
    Copy after login

    Sample code

    @Controller @RequestMapping("/redis") public class RedisController { @Autowired private RedisTemplate redisTemplate; /** * @param * @return */ @RequestMapping(value = "/setRedisData", method = RequestMethod.GET) @ResponseBody public Map 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 redisDataMap = new HashMap(); redisDataMap.put(redis1.getName(), redis1); redisDataMap.put(redis2.getName(), redis2); //存入redis redisTemplate.opsForHash().putAll("redisTest",redisDataMap); //获取缓存内容 Map resultMap = redisTemplate.opsForHash().entries("redisTest"); //List reslutMapList = redisTemplate.opsForHash().values("redisTest"); //Set resultMapSet = redisTemplate.opsForHash().keys("redisTest"); //RedisCustom value = (RedisCustom)redisTemplate.opsForHash().get("redisTest","小明"); return ResponseData.success(resultMap); } }
    Copy after login

    Result

    How to implement Java redis to store Map object type data

    Reference
    https://www.yisu.com/article/246815.htm

    Method 2°

    Convert the object into byte[]

    Serialization and deserialization tool class

    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; } }
    Copy after login

    Sample code

    @Controller @RequestMapping("/redis") public class RedisController { /** * @param * @return */ @RequestMapping(value = "/setRedisData", method = RequestMethod.GET) @ResponseBody public Map 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 redisDataMap = new HashMap(); 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 redisData = (Map) obj; return ResponseData.success(redisData); } }
    Copy after login

    The above is the detailed content of How to implement Java redis to store Map object type data. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!