2018-01-17 14:10:40 +08:00
|
|
|
package cn.keking.utils;
|
|
|
|
|
|
2019-04-16 21:09:32 +08:00
|
|
|
import cn.keking.config.ConfigConstants;
|
2019-06-11 09:53:39 +08:00
|
|
|
import cn.keking.service.cache.CacheService;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
2018-01-17 14:10:40 +08:00
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
2019-06-11 09:53:39 +08:00
|
|
|
/**
|
|
|
|
|
* @auther: chenjh
|
|
|
|
|
* @since: 2019/6/11 7:45
|
|
|
|
|
*/
|
2018-01-17 14:10:40 +08:00
|
|
|
@Component
|
2019-10-24 19:22:23 +08:00
|
|
|
@ConditionalOnExpression("'${cache.clean.enabled:false}'.equals('true')")
|
2018-01-17 14:10:40 +08:00
|
|
|
public class ShedulerClean {
|
|
|
|
|
|
2020-05-14 10:11:15 +08:00
|
|
|
private final Logger logger = LoggerFactory.getLogger(ShedulerClean.class);
|
2019-06-11 09:53:39 +08:00
|
|
|
|
2020-05-15 18:09:19 +08:00
|
|
|
private final CacheService cacheService;
|
2019-06-11 09:53:39 +08:00
|
|
|
|
2020-05-15 18:09:19 +08:00
|
|
|
public ShedulerClean(CacheService cacheService) {
|
|
|
|
|
this.cacheService = cacheService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private final String fileDir = ConfigConstants.getFileDir();
|
2019-06-11 09:53:39 +08:00
|
|
|
|
2019-10-24 19:22:23 +08:00
|
|
|
//默认每晚3点执行一次
|
|
|
|
|
@Scheduled(cron = "${cache.clean.cron:0 0 3 * * ?}")
|
2019-06-11 09:53:39 +08:00
|
|
|
public void clean() {
|
2020-05-14 10:11:15 +08:00
|
|
|
logger.info("Cache clean start");
|
2019-06-11 09:53:39 +08:00
|
|
|
cacheService.cleanCache();
|
2020-12-26 19:13:50 +08:00
|
|
|
FileUtils.deleteDirectory(fileDir);
|
2020-05-14 10:11:15 +08:00
|
|
|
logger.info("Cache clean end");
|
2018-01-17 14:10:40 +08:00
|
|
|
}
|
|
|
|
|
}
|