Skip to content

Commit

Permalink
feat: Begin Replacing google cache by caffeine
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Nov 23, 2023
1 parent 0e77c5e commit 15350ed
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ hikari = "5.1.0"
exposed = "0.44.1"
sqlite = "3.44.0.0"
mariadb = "3.3.0"
caffeine = "3.1.8"

# gradle plugins
toxopid = "3.2.0"
Expand Down Expand Up @@ -81,6 +82,7 @@ exposed-java-time = { module = "org.jetbrains.exposed:exposed-java-time", versio
exposed-json = { module = "org.jetbrains.exposed:exposed-json", version.ref = "exposed" }
sqlite = { module = "org.xerial:sqlite-jdbc", version.ref = "sqlite" }
mariadb = { module = "org.mariadb.jdbc:mariadb-java-client", version.ref = "mariadb" }
caffeine = { module = "com.github.ben-manes.caffeine:caffeine", version.ref = "caffeine" }

# gradle plugins
toxopid = { module = "fr.xpdustry:toxopid", version.ref = "toxopid" }
Expand Down
1 change: 1 addition & 0 deletions imperium-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
api(libs.rabbitmq.client)
api(libs.snowflake.id)
api(libs.okhttp)
api(libs.caffeine)

api(libs.slf4j.api)
testApi(libs.slf4j.simple)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Imperium, the software collection powering the Xpdustry network.
* Copyright (C) 2023 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.imperium.common.misc

import com.github.benmanes.caffeine.cache.AsyncCache
import com.github.benmanes.caffeine.cache.Caffeine
import com.xpdustry.imperium.common.async.ImperiumScope
import kotlin.time.Duration
import kotlin.time.toJavaDuration
import kotlinx.coroutines.async
import kotlinx.coroutines.future.asCompletableFuture
import kotlinx.coroutines.future.await

fun <K : Any, V : Any> buildAsyncCache(
expireAfterAccess: Duration? = null,
maximumSize: Long = -1
): AsyncCache<K, V> {
val builder = Caffeine.newBuilder()
if (expireAfterAccess != null) {
builder.expireAfterAccess(expireAfterAccess.toJavaDuration())
}
if (maximumSize > 0) {
builder.maximumSize(maximumSize)
}
return builder.buildAsync()
}

suspend fun <K : Any, V : Any> AsyncCache<K, V>.getSuspending(
key: K,
compute: suspend (K) -> V
): V = get(key) { k, _ -> ImperiumScope.IO.async { compute(k) }.asCompletableFuture() }.await()
1 change: 1 addition & 0 deletions imperium-discord/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ tasks.shadowJar {
exclude(dependency(libs.exposed.jdbc.get()))
exclude(dependency(libs.sqlite.get()))
exclude(dependency(libs.mariadb.get()))
exclude(dependency(libs.caffeine.get()))
}
}

Expand Down
1 change: 1 addition & 0 deletions imperium-mindustry/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ tasks.shadowJar {
exclude(dependency("com.sksamuel.hoplite:hoplite-.*:.*"))
exclude(dependency(libs.sqlite.get()))
exclude(dependency(libs.mariadb.get()))
exclude(dependency(libs.caffeine.get()))
}
}

Expand Down

1 comment on commit 15350ed

@JasonP01
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You cant replace google with coffee silly

Please sign in to comment.