• cacheMap用来清除过期时间的对象。


    cacheMap定时缓存

    /**
     * 用来存储短暂对象的缓存类,实现Map接口,内部有一个定时器用来清除过期(30秒)的对象。
     * 为避免创建过多线程,没有特殊要求请使用getDefault()方法来获取本类的实例。
     * 
     * @param <K>
     * @param <V>
     */
    
    public class CacheMap<K, V> extends AbstractMap<K, V> {
    
        private static Logger logger = Logger.getLogger(CacheMap.class);
    
        //30s清除一次缓存
        private static final long DEFAULT_TIMEOUT = 30000;
        private static CacheMap<Object, Object> defaultInstance;
    
        public static synchronized CacheMap<Object, Object> getDefault() {
            if (defaultInstance == null) {
                defaultInstance = new CacheMap<Object, Object>(DEFAULT_TIMEOUT);
            }
            return defaultInstance;
        }
    
        private class CacheEntry implements Entry<K, V> {
            long time;
            V value;
            K key;
    
            CacheEntry(K key, V value) {
                super();
                this.value = value;
                this.key = key;
                this.time = System.currentTimeMillis();
            }
    
            @Override
            public K getKey() {
                return key;
            }
    
            @Override
            public V getValue() {
                return value;
            }
    
            @Override
            public V setValue(V value) {
                return this.value = value;
            }
        }
    
        private class ClearThread extends Thread {
            ClearThread() {
                setName("clear cache thread");
            }
    
            @Override
            public void run() {
                while (true) {
                    logger.info("准备清理。。。。。。");
                    try {
                        long now = System.currentTimeMillis();
                        Object[] keys = map.keySet().toArray();
                        for (Object key : keys) {
                            CacheEntry entry = map.get(key);
                            if (now - entry.time >= cacheTimeout) {
                                synchronized (map) {
                                    map.remove(key);
                                    logger.info("清除了key:" + key);
                                }
                            }
                        }
                        Thread.sleep(cacheTimeout);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        private long cacheTimeout;
        private Map<K, CacheEntry> map = new HashMap<K, CacheEntry>();
    
        public CacheMap(long timeout) {
            this.cacheTimeout = timeout;
            new ClearThread().start();
        }
    
        public CacheMap() {
            this(DEFAULT_TIMEOUT);
        }
    
        @Override
        public Set<Entry<K, V>> entrySet() {
            Set<Entry<K, V>> entrySet = new HashSet<Entry<K, V>>();
            Set<Entry<K, CacheEntry>> wrapEntrySet = map.entrySet();
            for (Entry<K, CacheEntry> entry : wrapEntrySet) {
                entrySet.add(entry.getValue());
            }
            return entrySet;
        }
    
        @Override
        public V get(Object key) {
            CacheEntry entry = map.get(key);
            return entry == null ? null : entry.value;
        }
    
        @Override
        public V put(K key, V value) {
            CacheEntry entry = new CacheEntry(key, value);
            synchronized (map) {
                map.put(key, entry);
            }
            return value;
        }
    
    }
  • 相关阅读:
    HDU Problem 1811 Rank of Tetris【拓扑排序+并查集】
    POJ Problem 2367 Genealogical tree【拓扑排序】
    HDU Problem 2647 Reward【拓扑排序】
    HDU Problem 1285 确定比赛名次【拓扑排序】
    HDU Problem HDU Today 【最短路】
    HDU Problem 3665 Seaside【最短路】
    HDU Problem 一个人的旅行 【最短路dijkstra】
    HDU Problem 1596 find the safest road【最短路dijkstra】
    Beyond Compare文本合并进行内容替换要注意什么
    用这些工具都可以比较代码的差异
  • 原文地址:https://www.cnblogs.com/zchok/p/11813320.html
Copyright © 2020-2023  润新知