260210_03_ENH: Map, List 캐시 추상 클래스 일부 기능 보완 및 추가
This commit is contained in:
@@ -4,7 +4,7 @@ plugins {
|
||||
}
|
||||
|
||||
//버전 변경은 여기서
|
||||
def commonVer = "beta.1.0.61"
|
||||
def commonVer = "beta.1.0.63"
|
||||
|
||||
group = 'com.kri.common'
|
||||
version = commonVer
|
||||
|
||||
@@ -4,6 +4,8 @@ import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
@@ -15,7 +17,8 @@ public abstract class AbstractListCache<T> {
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
@Transactional(readOnly = true)
|
||||
public void loadOnStartup() {
|
||||
doReload();
|
||||
List<T> tempList = loadFromDb();
|
||||
if(tempList != null) setList(tempList);
|
||||
}
|
||||
|
||||
public List<T> getList() {
|
||||
@@ -29,20 +32,84 @@ public abstract class AbstractListCache<T> {
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<T> reload() {
|
||||
doReload();
|
||||
List<T> tempList = loadFromDb();
|
||||
if(tempList == null) {
|
||||
throw new UnsupportedOperationException("This cache does not support reload().");
|
||||
}
|
||||
setList(tempList);
|
||||
return getList();
|
||||
}
|
||||
|
||||
// 서브클래스가 "DB에서 뭘 읽어올지"만 구현
|
||||
protected abstract List<T> loadFromDb();
|
||||
|
||||
private void doReload() {
|
||||
public List<T> clear() {
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
this.list = List.copyOf(loadFromDb()); // 불변 리스트로 교체
|
||||
try{
|
||||
return list = List.of();
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void setList(List<T> next) {
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
this.list = List.copyOf(next == null ? List.of() : next);
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void addAll(List<T> items) {
|
||||
if (items == null || items.isEmpty()) return;
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
List<T> merged = new java.util.ArrayList<>(this.list);
|
||||
merged.addAll(items);
|
||||
this.list = List.copyOf(merged);
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void addAll(int index, Collection<? extends T> items) {
|
||||
if (items == null || items.isEmpty()) return;
|
||||
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
ArrayList<T> next = new ArrayList<>(this.list);
|
||||
int i = Math.max(0, Math.min(index, next.size())); // 0 ~ size로 보정
|
||||
next.addAll(i, new ArrayList<>(items));
|
||||
this.list = List.copyOf(next);
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void add(T item) {
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
List<T> merged = new java.util.ArrayList<>(this.list);
|
||||
merged.add(item);
|
||||
this.list = List.copyOf(merged);
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void add(int index, T item){
|
||||
lock.writeLock().lock();
|
||||
|
||||
try{
|
||||
ArrayList<T> next = new ArrayList<>(this.list);
|
||||
int i = Math.max(0, Math.min(index, next.size())); // 0 ~ size로 보정
|
||||
next.add(i, item);
|
||||
this.list = List.copyOf(next);
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
protected List<T> loadFromDb() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
@@ -15,7 +16,8 @@ public abstract class AbstractMapCache<K, V> {
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
@Transactional(readOnly = true)
|
||||
public void loadOnStartup() {
|
||||
doReload();
|
||||
Map<K, V> tempMap = loadFromDb();
|
||||
if(tempMap != null) setMap(tempMap);
|
||||
}
|
||||
|
||||
public Map<K, V> getMap() {
|
||||
@@ -47,20 +49,62 @@ public abstract class AbstractMapCache<K, V> {
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Map<K, V> reload() {
|
||||
doReload();
|
||||
Map<K, V> tempMap = loadFromDb();
|
||||
if(tempMap == null) {
|
||||
throw new UnsupportedOperationException("This cache does not support reload().");
|
||||
}
|
||||
setMap(tempMap);
|
||||
return getMap();
|
||||
}
|
||||
|
||||
// 서브클래스가 "DB에서 어떤 Map을 만들지"만 구현
|
||||
protected abstract Map<K, V> loadFromDb();
|
||||
|
||||
private void doReload() {
|
||||
/** 수동: 전체 교체(불변 스냅샷으로 보관) */
|
||||
public void setMap(Map<K, V> next) {
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
this.map = Map.copyOf(loadFromDb()); // 불변 맵으로 교체
|
||||
this.map = Map.copyOf(next == null ? Map.of() : next);
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void put(K key, V value) {
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
Map<K, V> merged = new HashMap<>(this.map);
|
||||
merged.put(key, value);
|
||||
this.map = Map.copyOf(merged);
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/** 수동: 부분 적재(기존 + entries 합쳐서 스냅샷 교체) */
|
||||
public void putAll(Map<K, V> entries) {
|
||||
if (entries == null || entries.isEmpty()) return;
|
||||
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
Map<K, V> merged = new HashMap<>(this.map);
|
||||
merged.putAll(entries);
|
||||
this.map = Map.copyOf(merged);
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/** clear: 완전 비우기 */
|
||||
public void clear() {
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
this.map = Map.of();
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
// 서브클래스가 "DB에서 어떤 Map을 만들지"만 구현
|
||||
protected Map<K, V> loadFromDb() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user