Get the App
SLTechnology News&Howtos  ›  Development  › 

How to store Map object type data in Java redis

Shulou Source: shulou.com Published: 2022-05-31 17:29:30 10月03日 Update

This article mainly introduces the relevant knowledge of "how to achieve Java redis storage Map object type data". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "how to achieve Java redis storage Map object type data" can help you solve the problem.

Background description

The project needs to store the latest longitude and latitude information collected by the equipment into the redis cache to facilitate timely query and retrieval. Considering the different retrieval conditions, the queried devices are different. By storing the data in the redis cache as a map type, record it 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;}} 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;}} method 1 °redisTemplate.opsForHash ()

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 ("Xiaoming"); redis1.setAge (12); redis1.setStuCode ("36") RedisCustom redis2 = new RedisCustom (); redis2.setName ("Xiao Hong"); redis2.setAge (11); redis2.setStuCode ("24"); / / construct map Map redisDataMap = new HashMap () stored in redis; redisDataMap.put (redis1.getName (), redis1); redisDataMap.put (redis2.getName (), redis2) / save redis redisTemplate.opsForHash () .putAll ("redisTest", redisDataMap); / / get cache content 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", "Xiaoming"); return ResponseData.success (resultMap);}}

Result

Referenc

Https://www.yisu.com/article/246815.htm

Method 2 °

Convert an object to byte []

Serialization and anti-sequence chemicals

Import java.io.*;/** * serialization and deserialization chemicals * / public class SerializeObjectTool {/ / serialization 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;} / / deserialize 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;}}

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 ("Xiaoming"); redis1.setAge (12); redis1.setStuCode ("36"); RedisCustom redis2 = new RedisCustom () Redis2.setName ("Xiao Hong"); redis2.setAge (11); redis2.setStuCode ("24"); / / construct map Map redisDataMap = new HashMap () stored in redis; redisDataMap.put (redis1.getName (), redis1); redisDataMap.put (redis2.getName (), redis2); / / Connect redis Jedis redis = new Jedis ("xx.xx.xxx.xx", 6379) Redis.auth ("xxxxxxxxxxx"); / save byte [] personByte = SerializeObjectTool.serialize (redisDataMap); redis.set ("redisData" .getBytes (), personByte); / / take byte [] byt = redis.get ("redisData" .getBytes ()); Object obj = SerializeObjectTool.unserizlize (byt); Map redisData = (Map) obj; return ResponseData.success (redisData) }} this is the end of the introduction on "how to implement Java redis storing Map object type data". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Sequence object data type difference method knowledge cache Xiaoming code content tools examples industry equipment Xiaohong query retrieval utility information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Technology Shulou Information OPPO Reno Microsoft