shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.walker.cache;
 
import com.walker.infrastructure.core.ApplicationBeanInitialized;
import com.walker.infrastructure.scheduler.AbstractTimedTask;
 
import java.util.Iterator;
import java.util.List;
 
/**
 * 定时清除缓存过期数据任务
 * @author MikeShi
 *
 */
public class CacheExpiredTask extends AbstractTimedTask implements ApplicationBeanInitialized {
 
    private List<Cache> cacheList;
    
    public void setCacheList(List<Cache> cacheList) {
        this.cacheList = cacheList;
        if(cacheList.size() <= 0){
            this.setPeriod(0);
        }
    }
 
    @Override
    public void execute() throws Exception {
        logger.debug("//************ 开始执行缓存过期清理任务 ***************");
        startup();
        if(cacheList == null) return;
        
        long currentMillis = System.currentTimeMillis();
        
        //遍历所有系统缓存对象
        for(Cache cache : cacheList){
            Cachable d = null;
            long timeOut = cache.getExpiredTime();
//            logger.debug("缓存 " + cache.getCacheName() + " 超时时间是:" + timeOut);
            
//            List<String> deleteKeys = new LinkedList<String>();
            
            for(Iterator<Cachable> i = cache.getIterator(); i.hasNext();){
                d = i.next();
                if(d.isExpired(timeOut, currentMillis)){
//                    deleteKeys.add(d.getKey());
                    // 直接通过迭代器删除,不用在标记那些需要删除
                    i.remove();
                }
            }
//            cache.remove(deleteKeys);
        }
    }
 
    public void startup() {
//        if(cacheList == null){
//            cacheList = SystemBeanUtils.getExpiredConfigCaches();
//        }
//        if(cacheList.size() <= 0){
//            this.setPeriod(0);
//        }
    }
 
}