jodd cache实现缓存超时

public class JoddCache {
    private static final int CACHE_SIZE = 2;
    private final static Cache<Object, Object> CACHE = new LRUCache<>(CACHE_SIZE);

    private JoddCache() {
    }


    /**
     * 缓存一个key 不超时
     *
     * @param key
     * @param value
     */
    public static void put(Object key, Object value) {
        CACHE.put(key, value);
    }


    /**
     * 缓存一个key 指定超时时间(单位:毫秒)
     *
     * @param key
     * @param value
     * @param timeout 超时时间
     */
    public static void put(Object key, Object value, Long timeout) {
        CACHE.put(key, value, timeout);
    }

    /**
     * 缓存指定超时时间
     *
     * @param key
     * @param value
     * @param timeUnit 时间单位 如:秒{@code TimeUnit.SECONDS}
     * @param timeout  单位时间
     */
    public static void put(Object key, Object value, TimeUnit timeUnit, Long timeout) {
        long l = timeUnit.toMillis(timeout);
        CACHE.put(key, value, l);
    }

    /**
     * 根据key获取缓存对象
     * Retrieves an object from the cache. Returns <code>null</code> if object
     * is not longer in cache or if it is expired.
     *
     * @param key
     * @return cached object
     */
    public static Object get(Object key) {
        return CACHE.get(key);
    }
}

原文地址:https://www.cnblogs.com/wilwei/p/10244682.html