-
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.
- Loading branch information
Showing
6 changed files
with
119 additions
and
3 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
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/kr/bb/notification/common/annotation/DuplicateEventHandleAnnotation.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 kr.bb.notification.common.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface DuplicateEventHandleAnnotation { | ||
String eventId(); // Specify eventId for constructing the cache key | ||
|
||
String userId(); // Specify userId for constructing the cache key | ||
|
||
long ttl() default 180; // Default TTL is 3 minutes | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/kr/bb/notification/common/aop/DuplicateEventHandlerAop.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,48 @@ | ||
package kr.bb.notification.common.aop; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.aspectj.lang.JoinPoint; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Aspect | ||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class DuplicateEventHandlerAop { | ||
private final RedisTemplate<String, Object> redisTemplate; | ||
// https://alwayspr.tistory.com/34 | ||
|
||
|
||
@Pointcut("@annotation(kr.bb.notification.common.annotation.DuplicateEventHandleAnnotation)") | ||
public void duplicateEvent() {} | ||
|
||
@Around("duplicateEvent()") | ||
public Object duplicateEventHandlerAop(ProceedingJoinPoint joinPoint) throws Throwable { | ||
String eventId = null; | ||
Object[] args = joinPoint.getArgs(); | ||
|
||
// Check if data exists in Redis | ||
Object cachedData = redisTemplate.opsForValue().get(eventId); | ||
|
||
Object result = joinPoint.proceed(); // run method | ||
|
||
if (cachedData != null) { | ||
// Data exists in cache, return it without executing the method | ||
return cachedData; | ||
} | ||
|
||
// Data doesn't exist in cache, proceed to execute the method | ||
Object result = joinPoint.proceed(); | ||
|
||
// Save the result in Redis with the specified TTL | ||
redisTemplate.opsForValue().set(key, result, duplicateEventHandlerAop.ttl()); | ||
|
||
return result; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/kr/bb/notification/config/RedisConfiguration.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,43 @@ | ||
package kr.bb.notification.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class RedisConfiguration { | ||
@Value("${spring.redis.host}") | ||
private String host; | ||
|
||
@Value("${spring.redis.port}") | ||
private int port; | ||
|
||
@Value("${spring.redis.password}") | ||
private String password; | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); | ||
redisStandaloneConfiguration.setHostName(host); | ||
redisStandaloneConfiguration.setPort(port); | ||
redisStandaloneConfiguration.setPassword(password); | ||
return new LettuceConnectionFactory(redisStandaloneConfiguration); | ||
} | ||
|
||
@Bean | ||
public RedisTemplate<String, Object> redisTemplate( | ||
RedisConnectionFactory redisConnectionFactory) { | ||
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setValueSerializer(new StringRedisSerializer()); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory); | ||
return redisTemplate; | ||
} | ||
} |
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