redis中存hashMap数据结构
前⾔
最近在做项⽬中,需要存Map<String,Map<String,String>>这种类型的数据。代码实现环境是springboot 1.5.9.RELEASE+ java8 + redis
具体代码实现
1.pom⽂件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
service层代码实现
@Service
public class TestService {
@Autowired
private RedisTemplate redisTemplate;
public void testOne(Integer id, Student stu)throws JsonProcessingException {
String stukey =Id());
Map<String, String> map = redisTemplate.opsForHash().entries(stukey);
Integer offNum, limitNum, totalNum;
ObjectMapper om =new ObjectMapper();redis支持的数据结构
Map<String, String> mapItem =new HashMap<>();
if(map == null || map.size()==0||!Age())){
offNum = Type().equals(stuType.Value())?1:0;
limitNum = Type().equals(stuType.Value())?1:0;            totalNum =1;
mapItem.put("off", String());
mapItem.put("limit", String());
mapItem.put("total", String());
mapItem.put("name", Name());
}else{
mapItem = (Age()),new HashMap<>());
offNum = Integer.("off"))+
(Type().equals(stuType.Value())?1:0);
limitNum = Integer.("limit"))+
(Type().equals(stuType.Value())?1:0);
totalNum = Integer.("total"))+1;
mapItem.put("off", String());
mapItem.put("limit", String());
mapItem.put("total", String());
mapItem.put("name", Name());
}
map.Age(), om.writeValueAsString(mapItem));
redisTemplate.opsForHash().putAll(silenceAlarmsKey, map);
}
public static String getSilenceAlarmsKey(Integer id){
return PREFIX +"stu:age:"+ id;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。