您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java redis存Map對象類型數據怎么實現”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“Java redis存Map對象類型數據怎么實現”文章能幫助大家解決問題。
項目需要將設備采集到的最新經緯度信息存入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; } }
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); } }
結果
參考
http://www.5655pk.com/article/246815.htm
將對象轉成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對象類型數據怎么實現”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。