-
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.
feat: Cache Orchestrator �by orchestratorId
- Loading branch information
Showing
3 changed files
with
119 additions
and
36 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
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/org/rooftop/netx/engine/OrchestratorCache.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,25 @@ | ||
package org.rooftop.netx.engine | ||
|
||
import org.rooftop.netx.api.Orchestrator | ||
|
||
internal object OrchestratorCache { | ||
|
||
private val cache: MutableMap<String, Orchestrator<*, *>> = mutableMapOf() | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
internal fun <T : Any, V : Any> cache( | ||
key: String, | ||
behavior: () -> Orchestrator<*, *>, | ||
): Orchestrator<T, V> { | ||
if (cache.contains(key)) { | ||
return cache[key] as Orchestrator<T, V> | ||
} | ||
synchronized(key) { | ||
if (cache.contains(key)) { | ||
return cache[key] as Orchestrator<T, V> | ||
} | ||
cache[key] = behavior.invoke() | ||
return cache[key] as Orchestrator<T, V> | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/test/kotlin/org/rooftop/netx/engine/OrchestratorCacheTest.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,54 @@ | ||
package org.rooftop.netx.engine | ||
|
||
import io.kotest.core.annotation.DisplayName | ||
import io.kotest.core.spec.style.DescribeSpec | ||
import io.kotest.matchers.equals.shouldBeEqual | ||
import io.kotest.matchers.equals.shouldNotBeEqual | ||
import org.rooftop.netx.api.Orchestrator | ||
import org.rooftop.netx.factory.OrchestratorFactory | ||
import org.rooftop.netx.meta.EnableDistributedTransaction | ||
import org.rooftop.netx.redis.RedisContainer | ||
import org.springframework.test.context.ContextConfiguration | ||
import org.springframework.test.context.TestPropertySource | ||
|
||
@EnableDistributedTransaction | ||
@DisplayName("OrchestratorCache 클래스의") | ||
@ContextConfiguration(classes = [RedisContainer::class]) | ||
@TestPropertySource("classpath:application.properties") | ||
internal class OrchestratorCacheTest( | ||
private val orchestratorFactory: OrchestratorFactory, | ||
) : DescribeSpec({ | ||
|
||
describe("cache 메소드는") { | ||
context("같은 orchestratorId를 가진 Orchestrator 를 생성하려하면,") { | ||
val expected = orchestratorFactory.createIntOrchestrator("same") | ||
|
||
it("동일한 Orchestrator 를 반환한다.") { | ||
val sameOrchestrator = orchestratorFactory.createIntOrchestrator("same") | ||
|
||
expected shouldBeEqual sameOrchestrator | ||
} | ||
} | ||
|
||
context("다른 orchestratorId를 가진 Orchestrator 를 생성하려하면,") { | ||
val expected = orchestratorFactory.createIntOrchestrator("same") | ||
|
||
it("동일하지 않은 Orchestrator 를 반환한다.") { | ||
val notSameOrchestrator = orchestratorFactory.createIntOrchestrator("notSame") | ||
|
||
expected shouldNotBeEqual notSameOrchestrator | ||
} | ||
} | ||
} | ||
|
||
}) { | ||
|
||
companion object { | ||
|
||
private fun OrchestratorFactory.createIntOrchestrator(orchestratorId: String): Orchestrator<Int, Int> { | ||
return this.create<Int>(orchestratorId) | ||
.start({ it + 1 }) | ||
.commit({ it + 1 }) | ||
} | ||
} | ||
} |