com.taobao.diamond.utils.SimpleCache Maven / Gradle / Ivy
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;
}
}