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

com.taobao.diamond.utils.SimpleCache Maven / Gradle / Ivy

There is a newer version: 3.2.4
Show newest version
package com.taobao.diamond.utils;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;


public class SimpleCache {

    final ConcurrentMap> cache = new ConcurrentHashMap>();

    private static class CacheEntry {
        final long expireTime;
        final E value;

        public CacheEntry(E value, long expire) {
            this.expireTime = expire;
            this.value = value;
        }
    }
    public void put(String key, E e, long ttlMs) {
        if (key == null || e == null) {
            return;
        }
        CacheEntry entry = new CacheEntry(e, System.currentTimeMillis() + ttlMs);
        cache.put(key, entry);
    }

    public E get(String key) {
        CacheEntry entry = cache.get(key);
        if (entry != null && entry.expireTime > System.currentTimeMillis()) {
            return entry.value;
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy