JAVA 단일 맵 캐 시
4710 단어 Java
package com.xxx.util;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
/**
*
*
*/
public final class SimpleCache {
private static final int MAX_CAPACITY = 1000;//
private static final Hashtable map = new Hashtable<>();
private static SimpleCache cache = null;
private SimpleCache(){};
/**
*
*/
public synchronized static SimpleCache getSimpleCache(){
if(cache==null){
cache = new SimpleCache();
}
return cache;
}
/**
* key
* @param key
* @return true false
*/
public boolean contains(String key) {
return map.contains(key);
}
/**
* key
* @param key
*/
public void remove(String key) {
map.remove(key);
}
/**
*
*/
public void rmoveAll (){
map.clear();
}
/**
* val
* @param key
*
* @return
* key Object
*/
public Object get (String key){
return map.get(key);
}
/**
*
* @return Map
*/
public Map getAll() {
Map hashMap = new HashMap<>();
Enumeration keys = map.keys();
while(keys.hasMoreElements()) {
String key = keys.nextElement();
Object val = map.get(key);
hashMap.put(key, val);
}
return hashMap;
}
/**
* key val
* @param key
*
* @param val
* Object
*/
public void put (String key,Object val){
if (val == null) {
throw new NullPointerException();
}
if(map.size()>= MAX_CAPACITY){
map.clear();
map.put(key, val);
}
if(map.containsKey(key)){
return ;
}
map.put(key, val);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.