redis中存hashMap数据结构

前言

最近在做项目中,需要存Map>这种类型的数据。代码实现环境是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 = getSilenceAlarmsKey(stu.getId());
        Map<String, String> map = redisTemplate.opsForHash().entries(stukey);
        Integer offNum, limitNum, totalNum;
        ObjectMapper om = new ObjectMapper();

        Map<String, String> mapItem = new HashMap<>();
        if (map == null || map.size() == 0 || !map.containsKey(stu.getAge())) {
            offNum = stu.getType().equals(stuType.OFFLINE_ALARM.getValue()) ? 1 : 0;
            limitNum = stu.getType().equals(stuType.TRANSFINITE_ALARM.getValue()) ? 1 : 0;
            totalNum = 1;
            mapItem.put("off", offNum.toString());
            mapItem.put("limit", limitNum.toString());
            mapItem.put("total", totalNum.toString());
            mapItem.put("name", stu.getName());
        } else {
            mapItem = Helpers.getToEntity(map.get(stu.getAge()), new HashMap<>());
            offNum = Integer.valueOf(mapItem.get("off")) +
                    (stu.getType().equals(stuType.OFFLINE_ALARM.getValue()) ? 1 : 0);
            limitNum = Integer.valueOf(mapItem.get("limit")) +
                    (stu.getType().equals(stuType.TRANSFINITE_ALARM.getValue()) ? 1 : 0);
            totalNum = Integer.valueOf(mapItem.get("total")) + 1;
            mapItem.put("off", offNum.toString());
            mapItem.put("limit", limitNum.toString());
            mapItem.put("total", totalNum.toString());
            mapItem.put("name", alarm.getName());
        }
        map.put(alarm.getAge(), om.writeValueAsString(mapItem));
        redisTemplate.opsForHash().putAll(silenceAlarmsKey, map);
    }

 public static String getSilenceAlarmsKey(Integer id) {
        return PREFIX + "stu:age:" + id;
    }
}

你可能感兴趣的:(springboot简单应用)