From 566b86fedf69d60600c697f756c3f34bea874a44 Mon Sep 17 00:00:00 2001 From: Stephen <138625055+stephendotgg@users.noreply.github.com> Date: Thu, 15 Feb 2024 20:07:53 +0000 Subject: [PATCH] uuid/name service, dont require mongo --- README.md | 8 +-- build.gradle.kts | 2 +- .../twilight/data/service/NameCacheService.kt | 50 ++++++++++--------- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 717b6e4..c67fe13 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Maven gg.flyte twilight - 1.1.4 + 1.1.5 ``` @@ -31,14 +31,14 @@ maven { url "https://repo.flyte.gg/releases" } -implementation "gg.flyte:twilight:1.1.4" +implementation "gg.flyte:twilight:1.1.5" ``` Gradle (Kotlin DSL) ```kotlin maven("https://repo.flyte.gg/releases") -implementation("gg.flyte:twilight:1.1.4") +implementation("gg.flyte:twilight:1.1.5") ``` Certain features of Twilight require configuration, which can be done via the Twilight class. To setup a Twilight class instance, you can use the `twilight` function as shown below: @@ -357,7 +357,7 @@ Similarly, if you have a name and want to get the UUID, you can call `uuidFromNa NameCacheService.uuidFromName("stxphen") ``` -Currently the only way to configure your MongoDB "cache" for UUIDs and names, is to have an Environment variable called `NAME_CACHE_COLLECTION` with the value being what you want to call the collection. +Currently the only way to configure your MongoDB "cache" for UUIDs and names, is to have an Environment variable called `NAME_CACHE_COLLECTION` with the value being what you want to call the collection. Don't want to use the Mongo cache? Disable `useMongoCache` in the settings. ### Files Extensions diff --git a/build.gradle.kts b/build.gradle.kts index d595caf..0710d23 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,7 +4,7 @@ plugins { } group = "gg.flyte" -version = "1.1.4" +version = "1.1.5" repositories { mavenLocal() diff --git a/src/main/kotlin/gg/flyte/twilight/data/service/NameCacheService.kt b/src/main/kotlin/gg/flyte/twilight/data/service/NameCacheService.kt index a05a1c2..469b403 100644 --- a/src/main/kotlin/gg/flyte/twilight/data/service/NameCacheService.kt +++ b/src/main/kotlin/gg/flyte/twilight/data/service/NameCacheService.kt @@ -21,16 +21,19 @@ object NameCacheService { private const val MOJANG_UUID_ENDPOINT = "https://api.mojang.com/users/profiles/minecraft" private val cache = mutableMapOf() + + private var useMongoCache = true private lateinit var mongoCache: MongoCollection - fun nameCache(nameCache: Settings) { - mongoCache = MongoDB.collection(nameCache.collectionName) + fun nameCache(settings: Settings) { + useMongoCache = settings.useMongoCache + if (useMongoCache) { + mongoCache = MongoDB.collection(settings.collectionName) + } } fun nameFromUUID(uuid: UUID): String { - return cache[uuid] ?: queryMongoNameByUUID( - uuid - ) ?: queryMojangNameByUUID(uuid) + return cache[uuid] ?: useMongoCache.takeIf { it }?.let { queryMongoNameByUUID(uuid) } ?: queryMojangNameByUUID(uuid) } private fun queryMongoNameByUUID(uuid: UUID): String? { @@ -46,28 +49,26 @@ object NameCacheService { } private fun queryMojangNameByUUID(uuid: UUID): String { - val connection = - URL("$MOJANG_PROFILE_ENDPOINT/$uuid").openConnection() as HttpsURLConnection + val connection = URL("$MOJANG_PROFILE_ENDPOINT/$uuid").openConnection() as HttpsURLConnection connection.doOutput = true val name = JsonParser.parseReader(connection.inputStream.reader()).asJsonObject.get("name").asString connection.disconnect() cache[uuid] = name - mongoCache.insertOne( - Document( - mapOf( - "_id" to uuid.toString(), - "name" to name + if (useMongoCache) { + mongoCache.insertOne( + Document( + mapOf( + "_id" to uuid.toString(), + "name" to name + ) ) ) - ) + } return name } fun uuidFromName(name: String): UUID { - return cache.findKeyByValue(name) - ?: queryMongoUUIDByName( - name - ) ?: queryMojangUUIDByName(name) + return cache.findKeyByValue(name) ?: useMongoCache.takeIf { it }?.let { queryMongoUUIDByName(name) } ?: queryMojangUUIDByName(name) } private fun queryMongoUUIDByName(name: String): UUID? { @@ -93,18 +94,21 @@ object NameCacheService { val uuid = JsonParser.parseReader(connection.inputStream.reader()).asJsonObject.get("id").asString.toUUID() connection.disconnect() cache[uuid] = name - mongoCache.insertOne( - Document( - mapOf( - "_id" to uuid.toString(), - "name" to name + if (useMongoCache) { + mongoCache.insertOne( + Document( + mapOf( + "_id" to uuid.toString(), + "name" to name + ) ) ) - ) + } return uuid } class Settings { + var useMongoCache = true var collectionName: String = if (Twilight.usingEnv) Environment.get("NAME_CACHE_COLLECTION") else ("name-cache") }