Skip to content

Commit

Permalink
[feat] KakaoPlatformPlaceService.search()에서 카카오맵 검색 결과에 대해 filterById…
Browse files Browse the repository at this point in the history
…() 거치도록 수정

- 그 과정에서 캐시를 킥으로 사용해봤습니다. 안되면 말고.
  • Loading branch information
jin.geonwoo committed Jan 9, 2025
1 parent 2610807 commit f76f71c
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;

import java.util.List;

public interface PlaceRepository {

// command
Place save(Place place);
void cache(Place place);

// query
boolean existsByPlatformId(String platformId);
Place findById(long id);
Place findByName(String name);
List<Place> findAll();
PlaceGetResponse findDetailById(Long id);
Slice<PlaceGetResponse> findAllOrderByExhibitionStartDateDesc(Pageable pageable);
Slice<PlaceGetResponse> findAllByLocationOrderByExhibitionStartDateDesc(Pageable pageable, Point currentLocation, double distance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ public Long findByName(String name) {
return findIdWith(placeEntity.name.eq(name));
}

public List<PlaceEntity> findAll() {
return jpaQueryFactory
.select(placeEntity)
.from(placeEntity)
.join(placeEntity.location).fetchJoin()
.leftJoin(exhibitionEntity).on(exhibitionEntity.place.eq(placeEntity)).fetchJoin()
.fetch();
}

private Long findIdWith(BooleanExpression... expressions) {
return jpaQueryFactory
.select(placeEntity.id)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.recordy.server.place.repository.impl;

import org.recordy.server.place.domain.PlaceEntity;
import org.springframework.data.repository.CrudRepository;

public interface PlaceRedisRepository extends CrudRepository<PlaceEntity, Long> {

boolean existsByPlatformId(String platformId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@RequiredArgsConstructor
Expand All @@ -22,6 +23,7 @@ public class PlaceRepositoryImpl implements PlaceRepository {

private final PlaceJpaRepository placeJpaRepository;
private final PlaceQueryDslRepository placeQueryDslRepository;
private final PlaceRedisRepository placeRedisRepository;

@Transactional
@Override
Expand All @@ -31,8 +33,17 @@ public Place save(Place place) {
return Place.from(entity);
}

@Override
public void cache(Place place) {
placeRedisRepository.save(PlaceEntity.create(place));
}

@Override
public boolean existsByPlatformId(String platformId) {
if (placeRedisRepository.existsByPlatformId(platformId)) {
return true;
}

return placeQueryDslRepository.existsByPlatformId(platformId);
}

Expand All @@ -58,6 +69,13 @@ public Place findByName(String name) {
return Place.from(id);
}

@Override
public List<Place> findAll() {
return placeQueryDslRepository.findAll().stream()
.map(Place::from)
.toList();
}

@Override
public PlaceGetResponse findDetailById(Long id) {
return placeQueryDslRepository.findById(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.recordy.server.common.message.ErrorMessage;
import org.recordy.server.place.controller.dto.response.PlatformPlaceSearchResponse;
import org.recordy.server.place.exception.PlaceException;
import org.recordy.server.place.repository.PlaceRepository;
import org.recordy.server.place.service.PlatformPlaceService;
import org.recordy.server.place.service.dto.kakao.KakaoPlaceSearch;
import org.recordy.server.place.service.dto.kakao.KakaoPlaceSearchResponse;
Expand Down Expand Up @@ -32,23 +33,27 @@ public class KakaoPlatformPlaceService implements PlatformPlaceService {
private final double latitude;
private final int radius;
private final RestTemplate restTemplate;
private final PlaceRepository placeRepository;

public KakaoPlatformPlaceService(
@Value("${place.kakao.key}") String key,
@Value("${place.kakao.longitude}") double longitude,
@Value("${place.kakao.latitude}") double latitude,
@Value("${place.kakao.radius}") int radius
@Value("${place.kakao.radius}") int radius,
PlaceRepository placeRepository
) {
this.key = key;
this.longitude = longitude;
this.latitude = latitude;
this.radius = radius;
this.restTemplate = new RestTemplate();
this.placeRepository = placeRepository;
}

@Override
public List<PlatformPlaceSearchResponse> search(String query, int page) {
return searchKakaoPlaces(query, page).stream()
.filter(place -> !placeRepository.existsByPlatformId(place.id()))
.map(PlatformPlaceSearchResponse::from)
.toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.recordy.server.search.repository.SearchRepository;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -54,4 +55,11 @@ public Slice<PlaceGetResponse> getAllByGeography(Pageable pageable, double latit
public PlaceGetResponse getDetailById(Long id) {
return placeRepository.findDetailById(id);
}

@Scheduled(cron = "0 0 * * * *")
@Transactional
public void cacheAll() {
placeRepository.findAll()
.forEach(placeRepository::cache);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -31,6 +32,9 @@ public Place save(Place place) {
return realPlace;
}

@Override
public void cache(Place place) {}

@Override
public boolean existsByPlatformId(String platformId) {
return false;
Expand All @@ -46,6 +50,11 @@ public Place findByName(String name) {
return null;
}

@Override
public List<Place> findAll() {
return null;
}

@Override
public PlaceGetResponse findDetailById(Long id) {
return null;
Expand Down

0 comments on commit f76f71c

Please sign in to comment.