-
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.
♻️ 쿠폰 발행 - lua script를 활용한 atomic연산 방식으로 변경
- Loading branch information
1 parent
fd32118
commit 0704515
Showing
4 changed files
with
64 additions
and
29 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
23 changes: 23 additions & 0 deletions
23
src/main/java/kr/bb/store/util/luascript/CouponLockExecutor.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,23 @@ | ||
package kr.bb.store.util.luascript; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.script.DefaultRedisScript; | ||
import org.springframework.data.redis.core.script.RedisScript; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Collections; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class CouponLockExecutor implements RedisLuaScriptExecutor{ | ||
|
||
private final RedisTemplate<String,String> redisTemplate; | ||
|
||
@Override | ||
public Boolean execute(String script, String key, Object... args) { | ||
RedisScript<Boolean> redisScript = new DefaultRedisScript<>(script, Boolean.class); | ||
return redisTemplate.execute(redisScript, Collections.singletonList(key), args[0], String.valueOf(args[1])); | ||
} | ||
|
||
} |
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,19 @@ | ||
package kr.bb.store.util.luascript; | ||
|
||
public class LockScript { | ||
/* | ||
* 모든 쿠폰은 expirationDate 설정을 위해 생성 시점에 DUMMY_DATA를 넣어 redis에 등록됩니다. | ||
* 결과적으로 하나의 데이터(DUMMY_DATA)가 더 들어있기 때문에 이를 고려해 '<='가 아닌 '<'로 개수를 비교해야 | ||
* 쿠폰을 생성할 때 설정한 limitCnt수 만큼 발급받게 할 수 있습니다. | ||
*/ | ||
public static final String script = "local key = KEYS[1]\n" + | ||
"local value = ARGV[1]\n" + | ||
"local limitCnt = tonumber(ARGV[2])\n" + | ||
"local currentCnt = redis.call('SCARD', key)\n" + | ||
"if currentCnt <= limitCnt then\n" + | ||
" redis.call('SADD', key, value)\n" + | ||
" return true\n" + | ||
"else\n" + | ||
" return false\n" + | ||
"end"; | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/kr/bb/store/util/luascript/RedisLuaScriptExecutor.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,5 @@ | ||
package kr.bb.store.util.luascript; | ||
|
||
public interface RedisLuaScriptExecutor { | ||
Object execute(String script, String key, Object... args); | ||
} |