Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected void successfulAuthentication(HttpServletRequest request, HttpServletR

String accessToken = jwtUtil.createAccessToken(principal, role);
String refreshToken = jwtUtil.createRefreshToken(principal, role);
redisUtil.setDataExpire("refresh:" + jwtUtil.getPrincipal(refreshToken), refreshToken, Constant.REFRESH_TOKEN_EXPIRATION_TIME);
redisUtil.setData("refresh:" + jwtUtil.getPrincipal(refreshToken), refreshToken, Constant.REFRESH_TOKEN_EXPIRATION_TIME);

setBody(role, response);
ResponseCookie accessCookie = createCookie("accessToken", accessToken, Constant.ACCESS_TOKEN_EXPIRATION_TIME, "/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
String accessToken = jwtUtil.createAccessToken(email, "USER");
String refreshToken = jwtUtil.createRefreshToken(email, "USER");

redisUtil.setDataExpire("refresh:" + email, refreshToken, Constant.REFRESH_TOKEN_EXPIRATION_TIME);
redisUtil.setData("refresh:" + email, refreshToken, Constant.REFRESH_TOKEN_EXPIRATION_TIME);

ResponseCookie accessCookie = createCookie("accessToken", accessToken, Constant.ACCESS_TOKEN_EXPIRATION_TIME, "/");
response.addHeader(HttpHeaders.SET_COOKIE, accessCookie.toString());
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/pickyfy/pickyfy/common/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ public class Constant {
public static final long ACCESS_TOKEN_EXPIRATION_TIME = 30 * 60 * 1000;
public static final long REFRESH_TOKEN_EXPIRATION_TIME = 7 * 24 * 60 * 60 * 1000;

public static final long CATEGORIES_EXPIRATION_TIME = 24 * 60 * 60 * 1000;
public static final long MAGAZINES_EXPIRATION_TIME = 60 * 60 * 1000;
public static final long PLACES_EXPIRATION_TIME = 30 * 60 * 1000;
public static final long PLACE_EXPIRATION_TIME = 30 * 60 * 1000;

public static final String REDIS_KEY_PREFIX = "refresh:";
}
46 changes: 46 additions & 0 deletions src/main/java/com/pickyfy/pickyfy/common/config/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
package com.pickyfy.pickyfy.common.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.pickyfy.pickyfy.common.Constant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.transaction.TransactionAwareCacheManagerProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableCaching
public class RedisConfig {

@Value("${spring.data.redis.host}")
Expand All @@ -19,4 +37,32 @@ public class RedisConfig {
public RedisConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory(host, port);
}

@Bean
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());

objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer(objectMapper);
RedisSerializer<String> stringSerializer = new StringRedisSerializer();

RedisCacheConfiguration defaultConfig = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jsonSerializer));

Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<>();
cacheConfigurations.put("place", defaultConfig.entryTtl(Duration.ofMillis(Constant.PLACE_EXPIRATION_TIME)));
cacheConfigurations.put("places", defaultConfig.entryTtl(Duration.ofMillis(Constant.PLACES_EXPIRATION_TIME)));
cacheConfigurations.put("magazines", defaultConfig.entryTtl(Duration.ofMillis(Constant.MAGAZINES_EXPIRATION_TIME)));
cacheConfigurations.put("categories", defaultConfig.entryTtl(Duration.ofMillis(Constant.CATEGORIES_EXPIRATION_TIME)));

RedisCacheManager manager = RedisCacheManager.builder(connectionFactory)
.cacheDefaults(defaultConfig)
.withInitialCacheConfigurations(cacheConfigurations)
.build();

return new TransactionAwareCacheManagerProxy(manager);
}
}
20 changes: 9 additions & 11 deletions src/main/java/com/pickyfy/pickyfy/common/util/RedisUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,24 @@ public class RedisUtil {

private final StringRedisTemplate stringRedisTemplate;

public void setDataExpire(String key, String value, long duration) {
public String getData(String key) {
try {
ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
Duration expireDuration = Duration.ofMillis(duration);
valueOperations.set(key, value, expireDuration);
}catch(DataAccessException e){
throw new ExceptionHandler(ErrorStatus._INTERNAL_SERVER_ERROR);
return valueOperations.get(key);
} catch (DataAccessException e) {
throw new ExceptionHandler(ErrorStatus.KEY_NOT_FOUNT);
}
}

public String getData(String key){
public void setData(String key, String value, long duration) {
try {
ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
return valueOperations.get(key);
} catch(DataAccessException e){
throw new ExceptionHandler(ErrorStatus.KEY_NOT_FOUNT);
valueOperations.set(key, value, Duration.ofMillis(duration));
} catch (DataAccessException e) {
throw new ExceptionHandler(ErrorStatus._INTERNAL_SERVER_ERROR);
}
}

public void deleteRefreshToken(String redisKey) {
public void deleteData(String redisKey) {
try {
stringRedisTemplate.delete(redisKey);
} catch (DataAccessException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class AuthServiceImpl implements AuthService{

@Override
public void logout(String refreshToken){
redisUtil.deleteRefreshToken(Constant.REDIS_KEY_PREFIX + jwtUtil.getPrincipal(refreshToken));
redisUtil.deleteData(Constant.REDIS_KEY_PREFIX + jwtUtil.getPrincipal(refreshToken));
}

@Override
Expand All @@ -28,7 +28,7 @@ public AuthResponse reIssue(String token){
String accessToken = jwtUtil.createAccessToken(principal, jwtUtil.getRole(token));
String refreshToken = jwtUtil.createRefreshToken(principal, jwtUtil.getRole(token));

redisUtil.setDataExpire("refresh:" + jwtUtil.getPrincipal(refreshToken), refreshToken, Constant.REFRESH_TOKEN_EXPIRATION_TIME);
redisUtil.setData("refresh:" + jwtUtil.getPrincipal(refreshToken), refreshToken, Constant.REFRESH_TOKEN_EXPIRATION_TIME);
return AuthResponse.from(accessToken, refreshToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -40,12 +41,14 @@ public CategoryResponse getCategory(Long id) {
}

@Override
@Cacheable(value = "categories", key = "'all'", unless = "#result.isEmpty()")
public List<CategoryResponse> getAllCategories() {
return categoryRepository.findAll().stream()
.map(CategoryResponse::from)
.collect(Collectors.toList());
}


@Override
@Transactional
public void updateCategory(Long id, CategoryTypeRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private void sendVerificationEmail(String code, String email) {
throw new ExceptionHandler(ErrorStatus._INTERNAL_SERVER_ERROR);
}

redisUtil.setDataExpire("email:" + email, code, Constant.EMAIL_TOKEN_EXPIRATION_TIME);
redisUtil.setData("email:" + email, code, Constant.EMAIL_TOKEN_EXPIRATION_TIME);
}

private MimeMessage createMimeMessage(String code, String email) throws MessagingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -40,6 +41,7 @@ public MagazineResponse getMagazine(Long id) {
}

@Override
@Cacheable(value = "magazines", key = "'all'", unless = "#result.isEmpty()")
public List<MagazineResponse> getAllMagazines() {
return magazineRepository.findAll().stream()
.map(MagazineResponse::from)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.util.*;

import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -42,6 +44,7 @@ public class PlaceServiceImpl implements PlaceService {
* 특정 유저가 저장한 Place 전체 조회
*/
@Override
@Cacheable(value = "place", key = "#email", unless = "#result.isEmpty()")
public List<PlaceSearchResponse> getUserSavePlace(String email) {
// 유저 조회
User user = findUserByEmail(email);
Expand Down Expand Up @@ -89,7 +92,9 @@ public PlaceSearchResponse getPlace(Long placeId) {
/**
* 유저 Place 저장 및 저장취소 (toggle)
*/
@Override
@Transactional
@CacheEvict(value = "place", key = "#email")
public boolean togglePlaceUser(String email, Long placeId) {
Place place = findPlaceById(placeId);
User user = findUserByEmail(email);
Expand All @@ -114,6 +119,7 @@ public List<Place> searchNearbyPlaces(NearbyPlaceSearchRequest request) {
}

@Transactional
@CacheEvict(value = "places", key = "'all'")
public Long createPlace(PlaceCreateRequest request, List<MultipartFile> imageList) {
if (placeRepository.existsPlaceByName(request.name())) {
throw new EntityExistsException(ErrorStatus.PLACE_NAME_DUPLICATED.getMessage());
Expand Down Expand Up @@ -150,6 +156,7 @@ public Long createPlace(PlaceCreateRequest request, List<MultipartFile> imageLis

@Override
@Transactional
@CacheEvict(value = "places", key = "'all'")
public Long updatePlace(Long placeId, PlaceCreateRequest request, List<MultipartFile> imageList) {
Place place = findPlaceById(placeId);

Expand All @@ -162,6 +169,7 @@ public Long updatePlace(Long placeId, PlaceCreateRequest request, List<Multipart

@Override
@Transactional
@CacheEvict(value = "places", key = "'all'")
public void deletePlace(Long placeId) {
Place place = findPlaceById(placeId);
for (PlaceImage image : place.getPlaceImages()) {
Expand All @@ -172,6 +180,7 @@ public void deletePlace(Long placeId) {

@Override
@Transactional(readOnly = true)
@Cacheable(value = "places", key = "'all'", unless = "#result.isEmpty()")
public List<PlaceSearchResponse> getAllPlaces() {
List<Place> allPlaceList = placeRepository.findAll();
return allPlaceList.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void verifyByEmail(String email){
}

private void invalidateTokens(String userEmail){
redisUtil.deleteRefreshToken(Constant.REDIS_KEY_PREFIX + userEmail);
redisUtil.deleteData(Constant.REDIS_KEY_PREFIX + userEmail);
}

private void validateEmailToken(String email, String token){
Expand Down