-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
155 additions
and
7 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/com/tiketeer/TiketeerWaiting/annotation/RedisCacheable.kt
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,8 @@ | ||
package com.tiketeer.TiketeerWaiting.annotation | ||
|
||
|
||
@Target(AnnotationTarget.FUNCTION) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class RedisCacheable( | ||
val key: String = "" | ||
) |
76 changes: 76 additions & 0 deletions
76
src/main/kotlin/com/tiketeer/TiketeerWaiting/aspect/RedisCacheAspect.kt
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,76 @@ | ||
package com.tiketeer.TiketeerWaiting.aspect | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.tiketeer.TiketeerWaiting.annotation.RedisCacheable | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.aspectj.lang.ProceedingJoinPoint | ||
import org.aspectj.lang.annotation.Around | ||
import org.aspectj.lang.annotation.Aspect | ||
import org.aspectj.lang.reflect.MethodSignature | ||
import org.springframework.data.redis.core.ReactiveRedisTemplate | ||
import org.springframework.stereotype.Component | ||
import reactor.core.publisher.Flux | ||
import reactor.core.publisher.Mono | ||
import java.util.stream.Collectors | ||
import com.tiketeer.TiketeerWaiting.util.AspectUtils | ||
|
||
private val logger = KotlinLogging.logger {} | ||
|
||
@Aspect | ||
@Component | ||
class RedisCacheAspect( | ||
private val redisTemplate: ReactiveRedisTemplate<String, String>, | ||
private val objectMapper: ObjectMapper, | ||
private val aspectUtils: AspectUtils) { | ||
@Around("execution(public * *(..)) && @annotation(com.tiketeer.TiketeerWaiting.annotation.RedisCacheable)") | ||
fun redisReactiveCacheable(joinPoint: ProceedingJoinPoint): Any { | ||
val methodSignature = joinPoint.signature as MethodSignature | ||
val method = methodSignature.method | ||
|
||
val returnType = method.returnType | ||
val annotation = method.getAnnotation(RedisCacheable::class.java) | ||
|
||
val key = aspectUtils.resolveKey(joinPoint, annotation.key) | ||
|
||
val typeReference = aspectUtils.getTypeReference(method) | ||
|
||
logger.info { "Evaluated Redis cacheKey: $key" } | ||
|
||
val cachedValue = redisTemplate.opsForValue().get(key) | ||
|
||
if (returnType.isAssignableFrom(Mono::class.java)) { | ||
return cachedValue | ||
.map { v -> | ||
objectMapper.readValue(v, typeReference) | ||
} | ||
.switchIfEmpty(Mono.defer { | ||
(joinPoint.proceed(joinPoint.args) as Mono<*>) | ||
.map { t -> | ||
redisTemplate.opsForValue().set(key, objectMapper.writeValueAsString(t)).subscribe() | ||
t | ||
} | ||
}) | ||
} else if (returnType.isAssignableFrom(Flux::class.java)) { | ||
return cachedValue | ||
.flatMapMany { v -> | ||
Flux.fromIterable( | ||
(v as List<String>).stream() | ||
.map { e -> objectMapper.readValue(e, typeReference) } | ||
.collect(Collectors.toList()) as List<*> | ||
) | ||
|
||
} | ||
.switchIfEmpty(Flux.defer { | ||
(joinPoint.proceed(joinPoint.args) as Flux<*>) | ||
.collectList() | ||
.map { t -> | ||
redisTemplate.opsForValue().set(key, objectMapper.writeValueAsString(t)).subscribe() | ||
t | ||
} | ||
.flatMapMany { Flux.fromIterable(it) } | ||
}) | ||
} | ||
|
||
throw RuntimeException("RedisReactiveCacheGet: Annotated method has unsupported return type, expected Mono<?> or Flux<?>") | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/tiketeer/TiketeerWaiting/configuration/SpelConfig.kt
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,13 @@ | ||
package com.tiketeer.TiketeerWaiting.configuration | ||
|
||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.expression.spel.standard.SpelExpressionParser | ||
|
||
@Configuration | ||
class SpelConfig { | ||
@Bean | ||
fun spelExpressionParser(): SpelExpressionParser { | ||
return SpelExpressionParser() | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...in/kotlin/com/tiketeer/TiketeerWaiting/domain/ticketing/repository/TicketingRepository.kt
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,8 +1,13 @@ | ||
package com.tiketeer.TiketeerWaiting.domain.ticketing.repository | ||
|
||
import com.tiketeer.TiketeerWaiting.annotation.RedisCacheable | ||
import com.tiketeer.TiketeerWaiting.domain.ticketing.Ticketings | ||
import org.springframework.cache.annotation.Cacheable | ||
import org.springframework.data.repository.reactive.ReactiveCrudRepository | ||
import reactor.core.publisher.Mono | ||
import java.util.UUID | ||
|
||
interface TicketingRepository : ReactiveCrudRepository<Ticketings, UUID> { | ||
@RedisCacheable(key = "#id") | ||
override fun findById(id: UUID) : Mono<Ticketings> | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/kotlin/com/tiketeer/TiketeerWaiting/util/AspectUtils.kt
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,49 @@ | ||
package com.tiketeer.TiketeerWaiting.util | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference | ||
import org.aspectj.lang.JoinPoint | ||
import org.aspectj.lang.reflect.CodeSignature | ||
import org.springframework.expression.spel.standard.SpelExpressionParser | ||
import org.springframework.expression.spel.support.StandardEvaluationContext | ||
import org.springframework.stereotype.Component | ||
import org.springframework.util.StringUtils | ||
import java.lang.reflect.Method | ||
import java.lang.reflect.ParameterizedType | ||
import java.lang.reflect.Type | ||
|
||
@Component | ||
class AspectUtils(private val expressionParser: SpelExpressionParser) { | ||
fun resolveKey(joinPoint: JoinPoint, key: String): String { | ||
if (StringUtils.hasText(key)) { | ||
if (key.contains("#") || key.contains("'")) { | ||
val parameterNames: Array<String> = getParamNames(joinPoint) | ||
val args = joinPoint.args | ||
val context = StandardEvaluationContext() | ||
for (i in parameterNames.indices) { | ||
context.setVariable(parameterNames[i], args[i]) | ||
} | ||
val value = expressionParser.parseExpression(key).getValue(context) | ||
return value.toString() | ||
} | ||
return key | ||
} | ||
throw RuntimeException("RedisReactiveCache annotation missing key") | ||
} | ||
|
||
private fun getParamNames(joinPoint: JoinPoint): Array<String> { | ||
val codeSignature = joinPoint.signature as CodeSignature | ||
return codeSignature.parameterNames | ||
} | ||
|
||
fun getTypeReference(method: Method): TypeReference<Any> { | ||
return object : TypeReference<Any>() { | ||
override fun getType(): Type { | ||
return getMethodActualReturnType(method) | ||
} | ||
} | ||
} | ||
|
||
private fun getMethodActualReturnType(method: Method): Type { | ||
return (method.genericReturnType as ParameterizedType).actualTypeArguments[0] | ||
} | ||
} |
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