All Downloads are FREE. Search and download functionalities are using the official Maven repository.

club.zhcs.utils.CacheMap Maven / Gradle / Ivy

package club.zhcs.utils;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.nutz.log.Log;
import org.nutz.log.Logs;

/**
 * @author Kerbores([email protected])
 */
public class CacheMap extends AbstractMap {

    private class CacheEntry implements Entry {
        K key;
        long time;
        V value;

        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) {
            this.value = value;
            return value;
        }
    }

    private class ClearThread extends Thread {
        ClearThread() {
            setName("clear cache thread");
        }

        @Override
        public void run() {
            while (true) {
                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);
                            }
                        }
                    }
                    Thread.sleep(cacheTimeout);
                }
                catch (Exception e) {
                    log.error(e);
                }
            }
        }
    }

    private static final long DEFAULT_TIMEOUT = 30000;

    private static CacheMap defaultInstance;

    public static synchronized CacheMap getDefault() {
        if (defaultInstance == null) {
            defaultInstance = new CacheMap<>(DEFAULT_TIMEOUT);
        }
        return defaultInstance;
    }

    private long cacheTimeout;

    Log log = Logs.get();
    private Map map = new HashMap<>();

    public CacheMap(long timeout) {
        this.cacheTimeout = timeout;
        new ClearThread().start();
    }

    @Override
    public Set> entrySet() {
        Set> entrySet = new HashSet<>();
        Set> wrapEntrySet = map.entrySet();
        for (Entry 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;
    }

    @Override
    public boolean equals(Object o) {
        return super.equals(o);
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy