-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 틈틈 위치 기반 로직 수정 및 테스트 (#139)
* refactor: redis 관련 클래스 리팩토링 (#124) * feat: 요청에 따른 100 m 이내 6명 조회 로직 구현 (#124) * refactor: 위치 기반 DTO, VO 관련 리팩토링 (#124) * refactor: 위치 기반 DTO, VO 관련 리팩토링 (#124) * test: RedisRepository 리팩토링 및 추가 구현 (#124) * test: 위치 기반 관련 테스트 수정 (#124) * fix: sonarCloud 에러 수정 (#124)
- Loading branch information
1 parent
05dcd4c
commit a7bf5b0
Showing
11 changed files
with
145 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 46 additions & 4 deletions
50
src/main/java/net/teumteum/core/security/service/RedisService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,72 @@ | ||
package net.teumteum.core.security.service; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.annotation.PostConstruct; | ||
import java.io.IOException; | ||
import java.time.Duration; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import net.teumteum.teum_teum.domain.UserLocation; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.ValueOperations; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class RedisService { | ||
|
||
|
||
private static final String HASH_KEY = "userLocation"; | ||
private final RedisTemplate<String, String> redisTemplate; | ||
private final ObjectMapper objectMapper; | ||
private ValueOperations<String, String> valueOperations; | ||
|
||
@PostConstruct | ||
void init() { | ||
valueOperations = redisTemplate.opsForValue(); | ||
} | ||
|
||
public String getData(String key) { | ||
return redisTemplate.opsForValue().get(key); | ||
return valueOperations.get(key); | ||
} | ||
|
||
public void setData(String key, String value) { | ||
redisTemplate.opsForValue().set(key, value); | ||
valueOperations.set(key, value); | ||
} | ||
|
||
public void setDataWithExpiration(String key, String value, Long duration) { | ||
Duration expireDuration = Duration.ofSeconds(duration); | ||
redisTemplate.opsForValue().set(key, value, expireDuration); | ||
valueOperations.set(key, value, expireDuration); | ||
} | ||
|
||
public void deleteData(String key) { | ||
redisTemplate.delete(key); | ||
valueOperations.getOperations().delete(key); | ||
} | ||
|
||
public void setUserLocation(UserLocation userLocation, Long duration) { | ||
String key = HASH_KEY + userLocation.id(); | ||
String value; | ||
try { | ||
value = objectMapper.writeValueAsString(userLocation); | ||
} catch (JsonProcessingException e) { | ||
throw new IllegalArgumentException(e); | ||
} | ||
valueOperations.set(key, value, duration); | ||
} | ||
|
||
public Set<UserLocation> getAllUserLocations() { | ||
Set<String> keys = redisTemplate.keys(HASH_KEY + ":*"); | ||
return requireNonNull(keys).stream().map(key -> { | ||
String value = valueOperations.get(key); | ||
try { | ||
return objectMapper.readValue(value, UserLocation.class); | ||
} catch (IOException e) { | ||
throw new IllegalArgumentException(e); | ||
} | ||
}).collect(Collectors.toSet()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
...t/teumteum/teum_teum/domain/UserData.java → ...umteum/teum_teum/domain/UserLocation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 33 additions & 77 deletions
110
src/main/java/net/teumteum/teum_teum/service/TeumTeumService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,59 @@ | ||
package net.teumteum.teum_teum.service; | ||
|
||
import static java.lang.System.currentTimeMillis; | ||
import static java.time.Duration.ofMinutes; | ||
import static java.lang.Math.atan2; | ||
import static java.lang.Math.sin; | ||
import static java.lang.Math.sqrt; | ||
import static java.lang.Math.toRadians; | ||
import static java.util.Comparator.comparingDouble; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.teumteum.teum_teum.domain.UserData; | ||
import net.teumteum.core.security.service.RedisService; | ||
import net.teumteum.teum_teum.domain.UserLocation; | ||
import net.teumteum.teum_teum.domain.request.UserLocationRequest; | ||
import net.teumteum.teum_teum.domain.response.UserAroundLocationsResponse; | ||
import net.teumteum.teum_teum.domain.response.UserAroundLocationsResponse.UserAroundLocationResponse; | ||
import org.springframework.data.geo.Circle; | ||
import org.springframework.data.geo.Distance; | ||
import org.springframework.data.geo.GeoResult; | ||
import org.springframework.data.geo.GeoResults; | ||
import org.springframework.data.geo.Point; | ||
import org.springframework.data.redis.connection.RedisGeoCommands.GeoLocation; | ||
import org.springframework.data.redis.core.GeoOperations; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.domain.geo.Metrics; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class TeumTeumService { | ||
|
||
private static final String KEY = "userLocation"; | ||
private static final int SEARCH_LIMIT = 6; | ||
private static final Duration LOCATION_EXPIRATION = ofMinutes(1); | ||
|
||
private final ObjectMapper objectMapper; | ||
private final RedisService redisService; | ||
|
||
private final RedisTemplate<String, Object> redisTemplate; | ||
|
||
public UserAroundLocationsResponse processingUserAroundLocations(UserLocationRequest request) { | ||
GeoOperations<String, Object> geoValueOperations = redisTemplate.opsForGeo(); | ||
|
||
String userDataJson = null; | ||
try { | ||
userDataJson = objectMapper.writeValueAsString( | ||
request.toUserData()) + ":" + currentTimeMillis(); | ||
} catch (JsonProcessingException e) { | ||
log.error("JsonProcessingException Occurred!"); | ||
} | ||
|
||
geoValueOperations.add(KEY, new Point(request.longitude(), request.latitude()), userDataJson); | ||
|
||
return getUserAroundLocations(geoValueOperations, request.longitude(), request.latitude()); | ||
public UserAroundLocationsResponse saveAndGetUserAroundLocations(UserLocationRequest request) { | ||
redisService.setUserLocation(request.toUserLocation(), 60L); | ||
return getUserAroundLocations(request); | ||
} | ||
|
||
private UserAroundLocationsResponse getUserAroundLocations(GeoOperations<String, Object> geoValueOperations, | ||
Double longitude, Double latitude) { | ||
private UserAroundLocationsResponse getUserAroundLocations(UserLocationRequest request) { | ||
Set<UserLocation> allUserLocations = redisService.getAllUserLocations(); | ||
|
||
GeoResults<GeoLocation<Object>> geoResults | ||
= geoValueOperations.radius(KEY, | ||
new Circle(new Point(longitude, latitude), new Distance(100, Metrics.METERS))); | ||
List<UserLocation> aroundUserLocations = allUserLocations.stream() | ||
.filter(userLocation -> !userLocation.id().equals(request.id())) | ||
.filter(userLocation -> calculateDistance(request.latitude(), request.longitude(), | ||
userLocation.latitude(), userLocation.longitude()) <= 100) | ||
.sorted(comparingDouble(userLocation | ||
-> calculateDistance(request.latitude(), request.longitude(), | ||
userLocation.latitude(), userLocation.longitude())) | ||
).limit(SEARCH_LIMIT) | ||
.toList(); | ||
|
||
return getUserAroundLocationsResponse(geoResults); | ||
return UserAroundLocationsResponse.of(aroundUserLocations); | ||
} | ||
|
||
private UserAroundLocationsResponse getUserAroundLocationsResponse(GeoResults<GeoLocation<Object>> geoResults) { | ||
|
||
List<UserAroundLocationResponse> userAroundLocationResponses = new ArrayList<>(); | ||
|
||
long currentTime = currentTimeMillis(); | ||
int count = 0; | ||
|
||
for (GeoResult<GeoLocation<Object>> geoResult : Objects.requireNonNull(geoResults)) { | ||
String userSavedTime = String.valueOf(geoResult.getContent().getName()).split(":")[5]; | ||
long timestamp = Long.parseLong(userSavedTime); | ||
|
||
if (currentTime - timestamp < LOCATION_EXPIRATION.toMillis()) { | ||
String savedUserLocation = String.valueOf(geoResult.getContent().getName()); | ||
String userDataJson = savedUserLocation.substring(savedUserLocation.lastIndexOf(":") + 1); | ||
|
||
UserData userData = null; | ||
try { | ||
userData = objectMapper.readValue(userDataJson, UserData.class); | ||
} catch (JsonProcessingException e) { | ||
log.error("JsonProcessingException Occurred!"); | ||
} | ||
|
||
UserAroundLocationResponse userAroundLocationResponse | ||
= UserAroundLocationResponse.of(Objects.requireNonNull(userData)); | ||
|
||
userAroundLocationResponses.add(userAroundLocationResponse); | ||
count++; | ||
|
||
if (count >= SEARCH_LIMIT) { | ||
break; | ||
} | ||
} | ||
} | ||
return new UserAroundLocationsResponse(userAroundLocationResponses); | ||
private double calculateDistance(double latitude1, double longitude1, double latitude2, double longitude2) { | ||
final int earthRadius = 6371; | ||
double latDistance = toRadians(latitude2 - latitude1); | ||
double lonDistance = toRadians(longitude2 - longitude1); | ||
double a = sin(latDistance / 2) * sin(latDistance / 2) | ||
+ Math.cos(toRadians(latitude1)) * Math.cos(toRadians(latitude2)) | ||
* sin(lonDistance / 2) * sin(lonDistance / 2); | ||
double c = 2 * atan2(sqrt(a), sqrt(1 - a)); | ||
return earthRadius * c * 1000; | ||
} | ||
} |
Oops, something went wrong.