-
Notifications
You must be signed in to change notification settings - Fork 2
캐시 원자 단위 의존성 주입 Resolver 추가 및 동적 Bean 등록 #260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7c3b84a
feat: add LuaScriptsFunctionalRegistrar to ApplicationContextInitiali…
polyglot-k 546d6ce
feat: rename Lua script files for clarity and consistency
polyglot-k 9d6003d
feat: add AtomicOperatorAutoRegistrar for dynamic registration of Cac…
polyglot-k 6c4dcd2
feat: add LuaScriptsFunctionalRegistrar for dynamic loading of Lua sc…
polyglot-k 8db0333
feat: refactor AtomicExamQuota operators to use dynamic Lua script lo…
polyglot-k 7c0d2b7
feat: add @Lazy annotation to examQuotaCacheAtomicOperatorMap injecti…
polyglot-k fb7a598
Merge branch 'develop' of https://github.com/mosu-dev/mosu-server int…
polyglot-k e5076ec
chore: add .gitkeep files to maintain empty directory structure
polyglot-k 211511b
feat: ensure Redis scripts for quota operations are not null
polyglot-k 1daba52
fix: correct index calculation for script path in LuaScriptsFunctiona…
polyglot-k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
66 changes: 0 additions & 66 deletions
66
src/main/java/life/mosu/mosuserver/global/config/ExamQuotaAtomicOperationConfig.java
This file was deleted.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
src/main/java/life/mosu/mosuserver/infra/config/AtomicOperatorAutoRegistrar.java
This file contains hidden or 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,50 @@ | ||
| package life.mosu.mosuserver.infra.config; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
| import life.mosu.mosuserver.infra.persistence.redis.operator.CacheAtomicOperator; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.SmartInitializingSingleton; | ||
| import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
| import org.springframework.beans.factory.support.DefaultListableBeanFactory; | ||
| import org.springframework.beans.factory.support.GenericBeanDefinition; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| @Slf4j | ||
| public class AtomicOperatorAutoRegistrar implements SmartInitializingSingleton { | ||
|
|
||
| private final ConfigurableListableBeanFactory beanFactory; | ||
|
|
||
| public AtomicOperatorAutoRegistrar(ConfigurableListableBeanFactory beanFactory) { | ||
| this.beanFactory = beanFactory; | ||
| } | ||
|
|
||
| @Override | ||
| public void afterSingletonsInstantiated() { | ||
| Map<String, CacheAtomicOperator> allOperators = beanFactory.getBeansOfType( | ||
| CacheAtomicOperator.class); | ||
|
|
||
| Map<String, List<CacheAtomicOperator>> grouped = allOperators.values().stream() | ||
| .collect(Collectors.groupingBy(CacheAtomicOperator::getName)); | ||
|
|
||
| DefaultListableBeanFactory registry = (DefaultListableBeanFactory) beanFactory; | ||
| for (Map.Entry<String, List<CacheAtomicOperator>> entry : grouped.entrySet()) { | ||
| String domain = entry.getKey(); | ||
| List<CacheAtomicOperator> operators = entry.getValue(); | ||
|
|
||
| Map<String, CacheAtomicOperator> mapValue = operators.stream() | ||
| .collect(Collectors.toMap(CacheAtomicOperator::getActionName, | ||
| Function.identity())); | ||
|
|
||
| String beanName = domain + "CacheAtomicOperatorMap"; | ||
| GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); | ||
| beanDefinition.setBeanClass(Map.class); | ||
| beanDefinition.setInstanceSupplier(() -> mapValue); | ||
|
|
||
| registry.registerBeanDefinition(beanName, beanDefinition); | ||
| } | ||
| } | ||
| } |
90 changes: 90 additions & 0 deletions
90
...a/life/mosu/mosuserver/infra/persistence/redis/support/LuaScriptsFunctionalRegistrar.java
This file contains hidden or 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,90 @@ | ||
| package life.mosu.mosuserver.infra.persistence.redis.support; | ||
|
|
||
| import java.io.InputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.context.ApplicationContextInitializer; | ||
| import org.springframework.context.support.GenericApplicationContext; | ||
| import org.springframework.core.io.Resource; | ||
| import org.springframework.core.io.support.PathMatchingResourcePatternResolver; | ||
| import org.springframework.data.redis.core.script.DefaultRedisScript; | ||
|
|
||
| /** | ||
| * FQCN | ||
| */ | ||
| @Slf4j | ||
| public class LuaScriptsFunctionalRegistrar implements | ||
| ApplicationContextInitializer<GenericApplicationContext> { | ||
|
|
||
| private static final String SCRIPT_BASE_PATH = "classpath:scripts/"; | ||
| private static final String SCRIPT_PATH_PREFIX = "/scripts/"; | ||
|
|
||
| @Override | ||
| public void initialize(GenericApplicationContext context) { | ||
| try { | ||
| PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); | ||
| Resource[] resources = resolver.getResources(SCRIPT_BASE_PATH + "**/*.lua"); | ||
|
|
||
| Map<String, Map<String, DefaultRedisScript<Long>>> domainScriptsMap = new HashMap<>(); | ||
|
|
||
| for (Resource resource : resources) { | ||
| String path = resource.getURL().getPath(); | ||
| int idx = path.lastIndexOf(SCRIPT_PATH_PREFIX); | ||
| if (idx < 0) { | ||
| continue; | ||
| } | ||
|
|
||
| String relativePath = path.substring(idx + SCRIPT_PATH_PREFIX.length()); | ||
| String[] parts = relativePath.split("/"); | ||
| if (parts.length < 2) { | ||
| continue; | ||
| } | ||
|
|
||
| String domain = parts[0]; | ||
| String filename = parts[1]; | ||
| String scriptName = toCamelCase(filename.replace(".lua", "")); | ||
|
|
||
| DefaultRedisScript<Long> script = new DefaultRedisScript<>(); | ||
| script.setResultType(Long.class); | ||
|
|
||
| try (InputStream is = resource.getInputStream()) { | ||
| String lua = new String(is.readAllBytes(), StandardCharsets.UTF_8); | ||
| script.setScriptText(lua); | ||
| } | ||
|
|
||
| domainScriptsMap | ||
| .computeIfAbsent(domain, k -> new HashMap<>()) | ||
| .put(scriptName, script); | ||
| } | ||
|
|
||
| for (Map.Entry<String, Map<String, DefaultRedisScript<Long>>> entry : domainScriptsMap.entrySet()) { | ||
| String beanName = entry.getKey() + "LuaScripts"; | ||
| log.info("Registering Lua scripts for domain: {}", beanName); | ||
| Map<String, DefaultRedisScript<Long>> scripts = entry.getValue(); | ||
|
|
||
| String keys = String.join(", ", scripts.keySet()); | ||
| log.info("Lua script keys: [{}]", keys); | ||
|
|
||
| context.registerBean(beanName, Map.class, () -> scripts); | ||
| } | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to load Lua scripts", e); | ||
| } | ||
| } | ||
|
|
||
| private String toCamelCase(String snakeCase) { | ||
| StringBuilder result = new StringBuilder(); | ||
| boolean toUpper = false; | ||
| for (char c : snakeCase.toCharArray()) { | ||
| if (c == '_') { | ||
| toUpper = true; | ||
| } else { | ||
| result.append(toUpper ? Character.toUpperCase(c) : c); | ||
| toUpper = false; | ||
| } | ||
| } | ||
| return result.toString(); | ||
| } | ||
| } | ||
Empty file.
This file contains hidden or 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,2 @@ | ||
| org.springframework.context.ApplicationContextInitializer=\ | ||
| life.mosu.mosuserver.infra.persistence.redis.support.LuaScriptsFunctionalRegistrar |
Empty file.
File renamed without changes.
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.