| | |
| | | package com.zy.asrs.wms.asrs.timer; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.zy.asrs.wms.asrs.entity.CacheStatistics; |
| | | import com.zy.asrs.wms.asrs.service.CacheStatisticsService; |
| | | import com.zy.asrs.wms.common.constant.RedisConstants; |
| | | import com.zy.asrs.wms.common.domain.CacheHitDto; |
| | | import com.zy.asrs.wms.utils.RedisUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.Set; |
| | | |
| | | @Component |
| | | public class CacheStatisticsTimer { |
| | | |
| | | @Autowired |
| | | private RedisUtil redisUtil; |
| | | @Autowired |
| | | private CacheStatisticsService cacheStatisticsService; |
| | | |
| | | /** |
| | | * 将缓存统计保存至数据库 |
| | | * 每30分钟扫描一次 |
| | | */ |
| | | @Scheduled(cron = "0 30 * * * ? ") |
| | | public void run() { |
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); |
| | | String now = format.format(new Date()); |
| | | Set<String> keys = redisUtil.searchRedisKeys(RedisConstants.STATISTICS_CACHE_DATA); |
| | | for (String key : keys) { |
| | | if(key.contains(now)){ |
| | | continue; |
| | | } |
| | | |
| | | try { |
| | | String[] split = key.split(":"); |
| | | String ymd = split[1]; |
| | | String cacheKey = split[2]; |
| | | Object object = redisUtil.get(key); |
| | | if(object == null){ |
| | | continue; |
| | | } |
| | | |
| | | CacheHitDto cacheHitDto = JSON.parseObject(object.toString(), CacheHitDto.class); |
| | | CacheStatistics cacheStatistics = cacheStatisticsService.getOne(new LambdaQueryWrapper<CacheStatistics>().eq(CacheStatistics::getYmd, ymd).eq(CacheStatistics::getCacheKey, cacheKey)); |
| | | if (cacheStatistics == null) { |
| | | cacheStatistics = new CacheStatistics(); |
| | | cacheStatistics.setYmd(ymd); |
| | | cacheStatistics.setCacheKey(cacheKey); |
| | | cacheStatistics.setHit(0); |
| | | cacheStatistics.setMiss(0); |
| | | cacheStatisticsService.save(cacheStatistics); |
| | | } |
| | | |
| | | cacheStatistics.setHit(cacheStatistics.getHit() + cacheHitDto.getHit()); |
| | | cacheStatistics.setMiss(cacheStatistics.getMiss() + cacheHitDto.getMiss()); |
| | | cacheStatisticsService.updateById(cacheStatistics); |
| | | |
| | | redisUtil.del(key); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | package com.zy.asrs.wms.asrs.timer;
|
| | |
|
| | | import com.alibaba.fastjson.JSON;
|
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
| | | import com.zy.asrs.wms.asrs.entity.CacheStatistics;
|
| | | import com.zy.asrs.wms.asrs.service.CacheStatisticsService;
|
| | | import com.zy.asrs.wms.common.constant.RedisConstants;
|
| | | import com.zy.asrs.wms.common.domain.CacheHitDto;
|
| | | import com.zy.asrs.wms.utils.RedisUtil;
|
| | | import org.springframework.beans.factory.annotation.Autowired;
|
| | | import org.springframework.scheduling.annotation.Scheduled;
|
| | | import org.springframework.stereotype.Component;
|
| | |
|
| | | import java.text.SimpleDateFormat;
|
| | | import java.util.Date;
|
| | | import java.util.Set;
|
| | |
|
| | | @Component
|
| | | public class CacheStatisticsTimer {
|
| | |
|
| | | @Autowired
|
| | | private RedisUtil redisUtil;
|
| | | @Autowired
|
| | | private CacheStatisticsService cacheStatisticsService;
|
| | |
|
| | | /**
|
| | | * 将缓存统计保存至数据库
|
| | | * 每30分钟扫描一次
|
| | | */
|
| | | // @Scheduled(cron = "0 30 * * * ? ")
|
| | | public void run() {
|
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
| | | String now = format.format(new Date());
|
| | | Set<String> keys = redisUtil.searchRedisKeys(RedisConstants.STATISTICS_CACHE_DATA);
|
| | | for (String key : keys) {
|
| | | if(key.contains(now)){
|
| | | continue;
|
| | | }
|
| | |
|
| | | try {
|
| | | String[] split = key.split(":");
|
| | | String ymd = split[1];
|
| | | String cacheKey = split[2];
|
| | | Object object = redisUtil.get(key);
|
| | | if(object == null){
|
| | | continue;
|
| | | }
|
| | |
|
| | | CacheHitDto cacheHitDto = JSON.parseObject(object.toString(), CacheHitDto.class);
|
| | | CacheStatistics cacheStatistics = cacheStatisticsService.getOne(new LambdaQueryWrapper<CacheStatistics>().eq(CacheStatistics::getYmd, ymd).eq(CacheStatistics::getCacheKey, cacheKey));
|
| | | if (cacheStatistics == null) {
|
| | | cacheStatistics = new CacheStatistics();
|
| | | cacheStatistics.setYmd(ymd);
|
| | | cacheStatistics.setCacheKey(cacheKey);
|
| | | cacheStatistics.setHit(0);
|
| | | cacheStatistics.setMiss(0);
|
| | | cacheStatisticsService.save(cacheStatistics);
|
| | | }
|
| | |
|
| | | cacheStatistics.setHit(cacheStatistics.getHit() + cacheHitDto.getHit());
|
| | | cacheStatistics.setMiss(cacheStatistics.getMiss() + cacheHitDto.getMiss());
|
| | | cacheStatisticsService.updateById(cacheStatistics);
|
| | |
|
| | | redisUtil.del(key);
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | |
|
| | | }
|
| | |
|
| | | }
|