这段代码写的有什么问题,求解!!!!
public class Caches {
private Caches() {
}
private static class CacheUtil{
private static final Caches CACHE = new Caches();
private static final Map<String,String> KEY_MAP = new ConcurrentHashMap<>( );
private static final Map<String,Cache<String, Object>> CACHE_MAP = new ConcurrentHashMap<>( );
}
public void put(String key,Object val,long duration, TimeUnit unit){
String cacheKey = String.valueOf( unit ).concat( ":" ).concat( String.valueOf( duration ) );
CacheUtil.KEY_MAP.put( key,cacheKey );
CacheUtil.CACHE_MAP.putIfAbsent( cacheKey, Caffeine.newBuilder()
.expireAfterWrite( duration, unit )
.maximumSize(1000)
.build());
CacheUtil.CACHE_MAP.get( cacheKey ).put( key,val );
}
//默认1小时
public void put(String key,Object val){
long duration = 1000*60*60;
TimeUnit unit = TimeUnit.MILLISECONDS;
String cacheKey = String.valueOf( unit ).concat( ":" ).concat( String.valueOf( duration ) );
CacheUtil.KEY_MAP.put( key,cacheKey );
CacheUtil.CACHE_MAP.putIfAbsent( cacheKey, Caffeine.newBuilder()
.expireAfterWrite( duration, unit )
.maximumSize(1000)
.build());
CacheUtil.CACHE_MAP.get( cacheKey ).put( key,val );
}
public Object get(String key){
if( null == CacheUtil.KEY_MAP.get( key )){
return null;
}
return CacheUtil.CACHE_MAP.get( CacheUtil.KEY_MAP.get( key ) ).getIfPresent( key );
}
public static Caches buildCache(){
return CacheUtil.CACHE;
}
}
你先说说你遇到什么问题了吧,要不上来让别人帮你review代码?
https://blog.csdn.net/zhuyu19911016520/article/details/81946202