Skip to content

Commit

Permalink
uuid/name service, dont require mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
stephendotgg committed Feb 15, 2024
1 parent 6eb5606 commit 566b86f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Maven
<dependency>
<groupId>gg.flyte</groupId>
<artifactId>twilight</artifactId>
<version>1.1.4</version>
<version>1.1.5</version>
</dependency>
```

Expand All @@ -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:
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "gg.flyte"
version = "1.1.4"
version = "1.1.5"

repositories {
mavenLocal()
Expand Down
50 changes: 27 additions & 23 deletions src/main/kotlin/gg/flyte/twilight/data/service/NameCacheService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ object NameCacheService {
private const val MOJANG_UUID_ENDPOINT = "https://api.mojang.com/users/profiles/minecraft"

private val cache = mutableMapOf<UUID, String>()

private var useMongoCache = true
private lateinit var mongoCache: MongoCollection<Document>

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? {
Expand All @@ -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? {
Expand All @@ -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")
}

Expand Down

0 comments on commit 566b86f

Please sign in to comment.