package cn.keking.config;

import cn.keking.service.cache.CacheService;
import cn.keking.utils.Consts;
import cn.keking.utils.KkFileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;

/**
 * @auther: chenjh
 * @since: 2019/6/11 7:45
 */
@Component
@ConditionalOnExpression("'${kkfile.cache.clean.enabled:false}'.equals('true')")
public class SchedulerCleanConfig {

    @Resource
    private ConfigConstants configConstants;

    private final Logger logger = LoggerFactory.getLogger(SchedulerCleanConfig.class);

    private final CacheService cacheService;

    public SchedulerCleanConfig(CacheService cacheService) {
        this.cacheService = cacheService;
    }


    //默认每晚3点执行一次
    @Scheduled(cron = "${kkfile.cache.clean.cron:0 0 3 * * ?}")
    public void clean() {
        logger.info("Cache clean start");
        cacheService.cleanCache();
        KkFileUtils.deleteDirectory(configConstants.getFileDir()+ File.separator+ Consts.UPLOAD_PATH_NAME);
        logger.info("Cache clean end");
    }
}
