Javaredis存Map对象类型数据的实现
短信预约 -IT技能 免费直播动态提醒
背景描述
项目需要将设备采集到的最新经纬度信息存入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;
}
}
方法1°
redisTemplate.opsForHash()
示例代码
@Controller
@RequestMapping("/redis")
public class RedisController {
@Autowired
private RedisTemplate redisTemplate;
@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);
}
}
结果
参考
https://www.jb51.net/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 {
@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对象类型数据的实现的文章就介绍到这了,更多相关Java redis存Map对象类型内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341