cache

guava cache的使用

package co.yhy.cache;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.concurrent.TimeUnit;
public class PayloadCache {
    private static final long EXPIRE_TIME_AFTER_WRITE = 60;
    private static final long CACHE_LIMIT_SIZE = 2000;
    private static volatile PayloadCache instance;
    private Cache cache;

    private PayloadCache() {
        this.cache = CacheBuilder.newBuilder()
                .expireAfterWrite(EXPIRE_TIME_AFTER_WRITE, TimeUnit.SECONDS)
                .maximumSize(CACHE_LIMIT_SIZE)
                .build();
        DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.MYSQL);
    }

    public static MessagePayloadCache getInstance() {
        if (instance == null) {
            synchronized (MessagePayloadCache.class) {
                if (instance == null) {
                    instance = new MessagePayloadCache();
                }
            }
        }
        return instance;
    }
    public void put(String key, String value) {
        cache.put(key, value);
    }
    public String get(String key) {
        return cache.get(key);
    }
}

你可能感兴趣的:(cache)