캐시네 뭘 ㅇㅇ
하지만 DB인것도 생각하면 DB와 client 계층 강화의 의미도 있겠네
동일한 데이터베이스 레코드를 여러 번 읽지 않도록 메모리에 객체를 보관하는 패턴입니다.
📱 카카오톡 채팅방과 비슷합니다:
public class UserIdentityMap {
// 메모리에 User 객체를 저장할 Map
private static Map<Long, User> identityMap = new HashMap<>();
public static User getUser(Long id) {
// 1. 먼저 메모리에서 찾기
User user = identityMap.get(id);
if (user == null) {
// 2. 메모리에 없으면 DB에서 조회
user = Database.findUserById(id);
// 3. 조회한 User를 메모리에 저장
identityMap.put(id, user);
}
return user;
}
}
// 사용 예시
User user1 = UserIdentityMap.getUser(1L); // DB 조회
User user2 = UserIdentityMap.getUser(1L); // 메모리에서 바로 반환