Skip to content

Commit 3f563d9

Browse files
committed
refactor: the concurrent caches in a module of their own, :cache
ConcurrentHashMap on the JVM and Android, cachemap on iOS only, for dao and the apps (Zayit's searchkit). Signed-off-by: Elie Gambache <elyahou.hadass@gmail.com>
1 parent 4576d7e commit 3f563d9

9 files changed

Lines changed: 111 additions & 12 deletions

File tree

‎cache/build.gradle.kts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
plugins {
3+
alias(libs.plugins.multiplatform)
4+
alias(libs.plugins.android.library)
5+
alias(libs.plugins.maven.publish)
6+
}
7+
8+
group = "io.github.kdroidfilter.seforimlibrary"
9+
10+
/** Caches read and written from several threads at once, on every platform (ConcurrentCache.kt). */
11+
kotlin {
12+
jvmToolchain(libs.versions.jvmToolchain.get().toInt())
13+
14+
android {
15+
namespace = "io.github.kdroidfilter.seforimlibrary.cache"
16+
compileSdk = 35
17+
minSdk = 21
18+
}
19+
jvm()
20+
iosArm64()
21+
iosSimulatorArm64()
22+
23+
sourceSets {
24+
commonTest.dependencies {
25+
implementation(kotlin("test"))
26+
implementation(libs.kotlinx.coroutines.core)
27+
}
28+
29+
// No java.util.concurrent on iOS: cachemap's map, lock-free reads
30+
iosMain.dependencies {
31+
implementation(libs.cachemap)
32+
}
33+
}
34+
35+
}
36+
37+
//Publishing your Kotlin Multiplatform library to Maven Central
38+
//https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-publish-libraries.html
39+
mavenPublishing {
40+
publishToMavenCentral()
41+
coordinates("io.github.kdroidfilter.seforimlibrary", "cache", "1.0.0")
42+
43+
pom {
44+
name = "SeforimLibrary"
45+
description = "Kotlin Multiplatform library"
46+
url = "github url" //todo
47+
48+
licenses {
49+
license {
50+
name = "MIT"
51+
url = "https://opensource.org/licenses/MIT"
52+
}
53+
}
54+
55+
developers {
56+
developer {
57+
id = "" //todo
58+
name = "" //todo
59+
email = "" //todo
60+
}
61+
}
62+
63+
scm {
64+
url = "github url" //todo
65+
}
66+
}
67+
if (project.hasProperty("signing.keyId")) signAllPublications()
68+
}

dao/src/androidMain/kotlin/io/github/kdroidfilter/seforimlibrary/dao/ConcurrentCache.android.kt renamed to cache/src/androidMain/kotlin/io/github/kdroidfilter/seforimlibrary/cache/ConcurrentCache.android.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package io.github.kdroidfilter.seforimlibrary.dao
1+
package io.github.kdroidfilter.seforimlibrary.cache
22

33
import java.util.concurrent.ConcurrentHashMap
44

5-
internal actual fun <K : Any, V : Any> concurrentCache(): ConcurrentCache<K, V> =
5+
actual fun <K : Any, V : Any> concurrentCache(): ConcurrentCache<K, V> =
66
object : ConcurrentCache<K, V> {
77
private val map = ConcurrentHashMap<K, V>()
88

dao/src/commonMain/kotlin/io/github/kdroidfilter/seforimlibrary/dao/ConcurrentCache.kt renamed to cache/src/commonMain/kotlin/io/github/kdroidfilter/seforimlibrary/cache/ConcurrentCache.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package io.github.kdroidfilter.seforimlibrary.dao
1+
package io.github.kdroidfilter.seforimlibrary.cache
22

33
/** A cache read and written from several threads at once: concurrent lookups of the same key may both load it. */
4-
internal interface ConcurrentCache<K : Any, V : Any> {
4+
interface ConcurrentCache<K : Any, V : Any> {
55
operator fun get(key: K): V?
66

77
operator fun set(
@@ -11,4 +11,4 @@ internal interface ConcurrentCache<K : Any, V : Any> {
1111
}
1212

1313
/** On the JVM and Android, ConcurrentHashMap; on iOS (no java.util.concurrent), cachemap's lock-free reads. */
14-
internal expect fun <K : Any, V : Any> concurrentCache(): ConcurrentCache<K, V>
14+
expect fun <K : Any, V : Any> concurrentCache(): ConcurrentCache<K, V>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.github.kdroidfilter.seforimlibrary.cache
2+
3+
import kotlinx.coroutines.Dispatchers
4+
import kotlinx.coroutines.async
5+
import kotlinx.coroutines.awaitAll
6+
import kotlinx.coroutines.runBlocking
7+
import kotlin.test.Test
8+
import kotlin.test.assertEquals
9+
import kotlin.test.assertNull
10+
11+
class ConcurrentCacheTest {
12+
@Test
13+
fun storesAndReplacesValues() {
14+
val cache = concurrentCache<String, Int>()
15+
assertNull(cache["a"])
16+
cache["a"] = 1
17+
cache["a"] = 2
18+
assertEquals(2, cache["a"])
19+
}
20+
21+
@Test
22+
fun keepsEveryValueWrittenFromSeveralThreads() =
23+
runBlocking {
24+
val cache = concurrentCache<Int, Int>()
25+
(0 until 8)
26+
.map { worker ->
27+
async(Dispatchers.Default) { for (i in 0 until 1_000) cache[worker * 1_000 + i] = i }
28+
}.awaitAll()
29+
for (key in 0 until 8_000) assertEquals(key % 1_000, cache[key])
30+
}
31+
}

dao/src/iosMain/kotlin/io/github/kdroidfilter/seforimlibrary/dao/ConcurrentCache.ios.kt renamed to cache/src/iosMain/kotlin/io/github/kdroidfilter/seforimlibrary/cache/ConcurrentCache.ios.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package io.github.kdroidfilter.seforimlibrary.dao
1+
package io.github.kdroidfilter.seforimlibrary.cache
22

33
import io.github.charlietap.cachemap.cacheMapOf
44

5-
internal actual fun <K : Any, V : Any> concurrentCache(): ConcurrentCache<K, V> =
5+
actual fun <K : Any, V : Any> concurrentCache(): ConcurrentCache<K, V> =
66
object : ConcurrentCache<K, V> {
77
private val map = cacheMapOf<K, V>()
88

dao/src/jvmMain/kotlin/io/github/kdroidfilter/seforimlibrary/dao/ConcurrentCache.jvm.kt renamed to cache/src/jvmMain/kotlin/io/github/kdroidfilter/seforimlibrary/cache/ConcurrentCache.jvm.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package io.github.kdroidfilter.seforimlibrary.dao
1+
package io.github.kdroidfilter.seforimlibrary.cache
22

33
import java.util.concurrent.ConcurrentHashMap
44

5-
internal actual fun <K : Any, V : Any> concurrentCache(): ConcurrentCache<K, V> =
5+
actual fun <K : Any, V : Any> concurrentCache(): ConcurrentCache<K, V> =
66
object : ConcurrentCache<K, V> {
77
private val map = ConcurrentHashMap<K, V>()
88

‎dao/build.gradle.kts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ kotlin {
2525
sourceSets {
2626
commonMain.dependencies {
2727
api(project(":core"))
28+
implementation(project(":cache"))
2829
implementation(libs.kotlinx.coroutines.core)
2930
implementation(libs.kotlinx.serialization.json)
3031
implementation(libs.kotlinx.serialization.protobuf)
@@ -53,8 +54,6 @@ kotlin {
5354

5455
iosMain.dependencies {
5556
implementation(libs.sqlDelight.driver.native)
56-
// The concurrent caches (see ConcurrentCache.ios.kt)
57-
implementation(libs.cachemap)
5857
}
5958

6059
}

‎dao/src/commonMain/kotlin/io/github/kdroidfilter/seforimlibrary/dao/repository/SeforimRepository.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import app.cash.sqldelight.db.SqlCursor
77
import app.cash.sqldelight.db.SqlDriver
88
import co.touchlab.kermit.Logger
99
import co.touchlab.kermit.Severity
10+
import io.github.kdroidfilter.seforimlibrary.cache.concurrentCache
1011
import io.github.kdroidfilter.seforimlibrary.core.models.AltTocEntry
1112
import io.github.kdroidfilter.seforimlibrary.core.models.AltTocStructure
1213
import io.github.kdroidfilter.seforimlibrary.core.models.Author
@@ -22,7 +23,6 @@ import io.github.kdroidfilter.seforimlibrary.core.models.PubPlace
2223
import io.github.kdroidfilter.seforimlibrary.core.models.Source
2324
import io.github.kdroidfilter.seforimlibrary.core.models.TocEntry
2425
import io.github.kdroidfilter.seforimlibrary.core.models.Topic
25-
import io.github.kdroidfilter.seforimlibrary.dao.concurrentCache
2626
import io.github.kdroidfilter.seforimlibrary.dao.extensions.toModel
2727
import io.github.kdroidfilter.seforimlibrary.db.SeforimDb
2828
import io.github.kdroidfilter.seforimlibrary.env.getEnvironmentVariable

‎settings.gradle.kts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ dependencyResolutionManagement {
6767
}
6868
}
6969
include(":core")
70+
include(":cache")
7071
include(":dao")
7172
include(":search")
7273
include(":search-facade")

0 commit comments

Comments
 (0)