-
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.
Merge pull request #13 from selab-hs/develop
[Develop] redis cache 적용
- Loading branch information
Showing
9 changed files
with
207 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.service.urlshortener.cache; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.Duration; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class Cache<T> { | ||
private String key; | ||
private Class<T> type; | ||
private Duration duration; | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/org/service/urlshortener/cache/CacheFactory.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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.service.urlshortener.cache; | ||
|
||
import org.service.urlshortener.shortener.dto.ShortUrlModel; | ||
|
||
import java.time.Duration; | ||
|
||
public class CacheFactory { | ||
public static Cache<ShortUrlModel> makeCachedQuiz(Long id){ | ||
return new Cache<>( | ||
"url:short:"+id, | ||
ShortUrlModel.class, | ||
Duration.ofMinutes(60) | ||
); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/org/service/urlshortener/cache/CacheService.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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.service.urlshortener.cache; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.SneakyThrows; | ||
import org.service.urlshortener.util.MapperUtil; | ||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CacheService { | ||
private final StringRedisTemplate redisTemplate; | ||
|
||
public <T> T getOrNull(Cache<T> cache) { | ||
var data = redisTemplate.opsForValue().get(cache.getKey()); | ||
return (data != null) ? MapperUtil.readValue(data, cache.getType()) : null; | ||
} | ||
|
||
@SneakyThrows | ||
public <T> T get(Cache<T> cache, Callable<T> callable) { | ||
var data = getOrNull(cache); | ||
|
||
if (data == null) { | ||
var calledData = callable.call(); | ||
|
||
asyncSet(cache, calledData); | ||
|
||
return calledData; | ||
} else { | ||
return data; | ||
} | ||
} | ||
|
||
public <T> void set(Cache<T> cache, T data) { | ||
redisTemplate.opsForValue().set( | ||
cache.getKey(), | ||
MapperUtil.writeValueAsString(data), | ||
cache.getDuration() | ||
); | ||
} | ||
|
||
public <T> void asyncSet(Cache<T> cache, T data) { | ||
CompletableFuture.runAsync(() -> set(cache, data)); | ||
} | ||
|
||
public <T> void delete(Cache<T> cache) { | ||
redisTemplate.delete(cache.getKey()); | ||
} | ||
|
||
public <T> void asyncDelete(Cache<T> cache) { | ||
CompletableFuture.runAsync(() -> delete(cache)); | ||
} | ||
} |
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
9 changes: 9 additions & 0 deletions
9
src/main/java/org/service/urlshortener/error/exception/InvalidJsonDataException.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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.service.urlshortener.error.exception; | ||
|
||
import org.service.urlshortener.error.dto.ErrorMessage; | ||
|
||
public class InvalidJsonDataException extends BusinessException { | ||
public InvalidJsonDataException(ErrorMessage message) { | ||
super(message); | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
src/main/java/org/service/urlshortener/shortener/dto/ShortUrlModel.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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.service.urlshortener.shortener.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class ShortUrlModel { | ||
private Long id; | ||
private String originalUrl; | ||
private LocalDateTime createAtl; | ||
} |
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
69 changes: 69 additions & 0 deletions
69
src/main/java/org/service/urlshortener/util/MapperUtil.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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.service.urlshortener.util; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.service.urlshortener.error.dto.ErrorMessage; | ||
import org.service.urlshortener.error.exception.InvalidJsonDataException; | ||
|
||
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL; | ||
import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS; | ||
|
||
@Slf4j | ||
public class MapperUtil { | ||
private static ObjectMapper mapper = new ObjectMapper(); | ||
|
||
/** | ||
* @return ObjectMapper | ||
* @apiNote object mapper | ||
**/ | ||
public static ObjectMapper mapper() { | ||
var deserializationFeature = DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES; | ||
var serializationFeature = SerializationFeature.FAIL_ON_EMPTY_BEANS; | ||
|
||
mapper | ||
.setSerializationInclusion(NON_NULL); | ||
|
||
mapper | ||
.configure(deserializationFeature, false) | ||
.configure(serializationFeature, false); | ||
|
||
mapper | ||
.registerModule(new JavaTimeModule()) | ||
.disable(WRITE_DATES_AS_TIMESTAMPS); | ||
|
||
return mapper; | ||
} | ||
|
||
public static String writeValueAsString(Object object) { | ||
try { | ||
return mapper().writeValueAsString(object); | ||
} catch (JsonProcessingException e) { | ||
log.error("[ERROR] Exception -> {}", e.getMessage()); | ||
throw new InvalidJsonDataException(ErrorMessage.INVALID_JSON_DATA_ERROR); | ||
} | ||
} | ||
|
||
public static <T> T readValue(String json, TypeReference<T> typeReference) { | ||
try { | ||
return mapper().readValue(json, typeReference); | ||
} catch (JsonProcessingException e) { | ||
log.error("[ERROR] Exception -> {}", e.getMessage()); | ||
throw new InvalidJsonDataException(ErrorMessage.INVALID_JSON_DATA_ERROR); | ||
} | ||
} | ||
|
||
public static <T> T readValue(String json, Class<T> clazz) { | ||
try { | ||
log.info("json = {}", json); | ||
return mapper().readValue(json, clazz); | ||
} catch (JsonProcessingException e) { | ||
log.error("[ERROR] Exception -> {}", e.getMessage()); | ||
throw new InvalidJsonDataException(ErrorMessage.INVALID_JSON_DATA_ERROR); | ||
} | ||
} | ||
} |