Java redis存Map對象類型數(shù)據(jù)的實(shí)現(xiàn)
背景描述
項(xiàng)目需要將設(shè)備采集到的最新經(jīng)緯度信息存入redis緩存中,方便及時查詢檢索。考慮到根據(jù)檢索條件不同,所查詢的設(shè)備不同。采取將數(shù)據(jù)以map類型存入redis緩存,在此記錄一下。
實(shí)體類
注:一定要實(shí)現(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");
? ? ? ? //構(gòu)造存入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);
? ? ? ? //獲取緩存內(nèi)容
? ? ? ? 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);
? ? }
}結(jié)果

參考
http://www.dbjr.com.cn/article/246815.htm
方法2°
將對象轉(zhuǎn)成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");
? ? ? ? //構(gòu)造存入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
到此這篇關(guān)于Java redis存Map對象類型數(shù)據(jù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java redis存Map對象類型內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA操作MongoDB數(shù)據(jù)庫實(shí)例教程
MongoDB是一個文檔型數(shù)據(jù)庫,是NOSQL家族中最重要的成員之一,下面這篇文章主要給大家介紹了關(guān)于JAVA操作MongoDB數(shù)據(jù)庫的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
Java基于SpringBoot和tk.mybatis實(shí)現(xiàn)事務(wù)讀寫分離代碼實(shí)例
這篇文章主要介紹了Java基于SpringBoot和tk.mybatis實(shí)現(xiàn)事務(wù)讀寫分離代碼實(shí)例,讀寫分離,基本的原理是讓主數(shù)據(jù)庫處理事務(wù)性增、改、刪操作,而從數(shù)據(jù)庫處理SELECT查詢操作,數(shù)據(jù)庫復(fù)制被用來把事務(wù)性操作導(dǎo)致的變更同步到集群中的從數(shù)據(jù)庫,需要的朋友可以參考下2023-10-10
Java正則表達(dá)式如何匹配特定html標(biāo)簽內(nèi)的內(nèi)容
這篇文章主要給大家介紹了關(guān)于Java正則表達(dá)式如何匹配特定html標(biāo)簽內(nèi)的內(nèi)容的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Java向MySQL添加中文數(shù)據(jù)數(shù)據(jù)庫顯示亂碼的解決方案
在用springboot做項(xiàng)目時,由于重新安裝了本地Mysql數(shù)據(jù)庫(5.7版本)在前臺向數(shù)據(jù)庫插入和更新數(shù)據(jù)可的時候,涉及中文的時候在數(shù)據(jù)庫一直顯示異常,所以本文給大家介紹了相關(guān)的解決方案,需要的朋友可以參考下2024-02-02
rabbitmq basicReject/basicNack/basicRecover的區(qū)別及說明
這篇文章主要介紹了rabbitmq basicReject/basicNack/basicRecover的區(qū)別及說明,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
Java虛擬機(jī)JVM性能優(yōu)化(一):JVM知識總結(jié)
這篇文章主要介紹了Java虛擬機(jī)JVM性能優(yōu)化(一):JVM知識總結(jié),本文是系列文章的第一篇,后續(xù)篇章請繼續(xù)關(guān)注腳本之家,需要的朋友可以參考下2014-09-09

