Java redis存Map對象類型數(shù)據(jù)的實現(xiàn)
背景描述
項目需要將設備采集到的最新經緯度信息存入redis緩存中,方便及時查詢檢索??紤]到根據(jù)檢索條件不同,所查詢的設備不同。采取將數(shù)據(jù)以map類型存入redis緩存,在此記錄一下。
實體類
注:一定要實現(xiàn)序列化接口
父類
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); ? ? } }
結果
參考
http://www.dbjr.com.cn/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); ? ? } }
參考
https://blog.csdn.net/chris_111x/article/details/85236458
到此這篇關于Java redis存Map對象類型數(shù)據(jù)的實現(xiàn)的文章就介紹到這了,更多相關Java redis存Map對象類型內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java基于SpringBoot和tk.mybatis實現(xiàn)事務讀寫分離代碼實例
這篇文章主要介紹了Java基于SpringBoot和tk.mybatis實現(xiàn)事務讀寫分離代碼實例,讀寫分離,基本的原理是讓主數(shù)據(jù)庫處理事務性增、改、刪操作,而從數(shù)據(jù)庫處理SELECT查詢操作,數(shù)據(jù)庫復制被用來把事務性操作導致的變更同步到集群中的從數(shù)據(jù)庫,需要的朋友可以參考下2023-10-10Java向MySQL添加中文數(shù)據(jù)數(shù)據(jù)庫顯示亂碼的解決方案
在用springboot做項目時,由于重新安裝了本地Mysql數(shù)據(jù)庫(5.7版本)在前臺向數(shù)據(jù)庫插入和更新數(shù)據(jù)可的時候,涉及中文的時候在數(shù)據(jù)庫一直顯示異常,所以本文給大家介紹了相關的解決方案,需要的朋友可以參考下2024-02-02rabbitmq basicReject/basicNack/basicRecover的區(qū)別及說明
這篇文章主要介紹了rabbitmq basicReject/basicNack/basicRecover的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01Java虛擬機JVM性能優(yōu)化(一):JVM知識總結
這篇文章主要介紹了Java虛擬機JVM性能優(yōu)化(一):JVM知識總結,本文是系列文章的第一篇,后續(xù)篇章請繼續(xù)關注腳本之家,需要的朋友可以參考下2014-09-09