redis
/** * 往Hash中存入数据 * * @param key Redis键 * @param hKey Hash键 * @param value 值 */ public <T> void setCacheMapValue(final String key, final String hKey, final T value) { redisTemplate.opsForHash().put(key, hKey, value); }
/** * 获取Hash中的数据 * * @param key Redis键 * @param hKey Hash键 * @return Hash中的对象 */ public <T> T getCacheMapValue(final String key, final String hKey) { HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash(); return opsForHash.get(key, hKey); }
/** * 获得缓存的Map * * @param key * @return */ public <T> Map<String, T> getCacheMap(final String key) { return redisTemplate.opsForHash().entries(key); }
/** * map自增 * @param key * @param hKey * @param val * @return */ public Long hashIncrement(final String key, final String hKey, Long val) { return redisTemplate.opsForHash().increment(key, hKey, val); }
1、 redis 存储 hashmap:
Map<String,Integer> map = new HashMap<>(); for(int i = 0; i < 100; i++) { map.put(i+"",i); } redisService.setCacheMapValue("testkey","k1",map);
存储后获取值:
Map<String,Integer> redRes = redisService.getCacheMapValue("testkey","k1"); Map<String,Map<String,Integer>> allRes = redisService.getCacheMap("testkey");
@Scheduled(cron = "0 0 0/1 * * ?") // @Scheduled(fixedRate = 5000) public void configureTasks() { Boolean lock = redisService.setIfAbsent("headlinesRanking", "", 300); if (!lock) { return; } log.info("========== 头条直播间数据统计开始 ========== "); DateTime date = DateUtil.beginOfHour(DateUtil.date()); DateTime lastDateTime = DateUtil.beginOfHour(DateUtil.offsetHour(date, -1));