MemoryCacheCenter.java 1.35 KB
package com.yanzuoguang.util.cache;

import com.yanzuoguang.util.extend.ConfigBase;
import com.yanzuoguang.util.thread.ThreadHelper;
import com.yanzuoguang.util.thread.AbstractThreadList;

import java.util.List;
import java.util.Vector;

/**
 * 内存缓存中心,负责自动清除过期缓存
 *
 * @author 颜佐光
 */
public class MemoryCacheCenter {

    /**
     * 缓存的对象
     */
    public final static List<MemoryCache> CLEAR_LIST = new Vector<MemoryCache>();

    static {
        init();
    }

    /**
     * 初始化
     */
    private static void init() {
        // 缓存对象处理线程
        ThreadHelper.runThread(new Runnable() {
            @Override
            public void run() {
                do {
                    clear();
                    // 等待1秒
                    ThreadHelper.sleep(ConfigBase.MEMORY_CLEAR_TIMEOUT);
                } while (true);
            }
        });
    }

    /**
     * 间隔5秒执行
     */
    private static void clear() {
        // todo: 需要修改
        // 死循环处理
        AbstractThreadList<MemoryCache> threadList = new AbstractThreadList<MemoryCache>(1) {
            @Override
            public void run(MemoryCache item) {
                item.clearTimeout();
            }
        };
        threadList.add(CLEAR_LIST);
        threadList.waitRun(false);
    }
}