260327_01_ENH: 프로젝트 구조 리펙토링 및 README.md 갱신

This commit is contained in:
2026-03-27 11:01:47 +09:00
parent e72d061e58
commit 905906b923
65 changed files with 531 additions and 1581 deletions

View File

@@ -0,0 +1,120 @@
package com.kri.common.feature.cache;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.transaction.annotation.Transactional;
public abstract class AbstractListCache<T> {
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private List<T> list = List.of();
@EventListener(ApplicationReadyEvent.class)
@Transactional(readOnly = true)
public void loadOnStartup() {
List<T> tempList = loadFromDb();
if (tempList != null) {
setList(tempList);
}
}
public List<T> getList() {
lock.readLock().lock();
try {
return list;
} finally {
lock.readLock().unlock();
}
}
public void setList(List<T> next) {
lock.writeLock().lock();
try {
this.list = List.copyOf(next == null ? List.of() : next);
} finally {
lock.writeLock().unlock();
}
}
@Transactional(readOnly = true)
public List<T> reload() {
List<T> tempList = loadFromDb();
if (tempList == null) {
throw new UnsupportedOperationException("This cache does not support reload().");
}
setList(tempList);
return getList();
}
public List<T> clear() {
lock.writeLock().lock();
try {
return list = List.of();
} 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;
}
}

View File

@@ -0,0 +1,118 @@
package com.kri.common.feature.cache;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.transaction.annotation.Transactional;
public abstract class AbstractMapCache<K, V> {
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private Map<K, V> map = Map.of();
@EventListener(ApplicationReadyEvent.class)
@Transactional(readOnly = true)
public void loadOnStartup() {
Map<K, V> tempMap = loadFromDb();
if (tempMap != null) {
setMap(tempMap);
}
}
public Map<K, V> getMap() {
lock.readLock().lock();
try {
return map;
} finally {
lock.readLock().unlock();
}
}
/**
* 수동: 전체 교체(불변 스냅샷으로 보관)
*/
public void setMap(Map<K, V> next) {
lock.writeLock().lock();
try {
this.map = Map.copyOf(next == null ? Map.of() : next);
} finally {
lock.writeLock().unlock();
}
}
public V get(K key) {
lock.readLock().lock();
try {
return map.get(key);
} finally {
lock.readLock().unlock();
}
}
public boolean containsKey(K key) {
lock.readLock().lock();
try {
return map.containsKey(key);
} finally {
lock.readLock().unlock();
}
}
@Transactional(readOnly = true)
public Map<K, V> reload() {
Map<K, V> tempMap = loadFromDb();
if (tempMap == null) {
throw new UnsupportedOperationException("This cache does not support reload().");
}
setMap(tempMap);
return getMap();
}
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;
}
}