Skip to content

Commit

Permalink
Merge pull request #67 from flytegg/fix/redis
Browse files Browse the repository at this point in the history
Fix/redis
  • Loading branch information
joshbker authored May 19, 2024
2 parents e3c8852 + fcf5abf commit 5cc0c06
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 23 deletions.
41 changes: 30 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Maven
<dependency>
<groupId>gg.flyte</groupId>
<artifactId>twilight</artifactId>
<version>1.1.10</version>
<version>1.1.11</version>
</dependency>
```

Expand All @@ -33,14 +33,14 @@ maven {
url "https://repo.flyte.gg/releases"
}
implementation "gg.flyte:twilight:1.1.10"
implementation "gg.flyte:twilight:1.1.11"
```

Gradle (Kotlin DSL)
```kotlin
maven("https://repo.flyte.gg/releases")

implementation("gg.flyte:twilight:1.1.10")
implementation("gg.flyte:twilight:1.1.11")
```

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 @@ -437,44 +437,63 @@ You can use the following Environment variables for your Redis Server:
REDIS_HOST="your redis server host"
REDIS_PORT="your redis server port"
REDIS_TIMEOUT="your redis connection timeout"
REDIS_USING_PASSWORD="false"
REDIS_USERNAME:""
REDIS_PASSWORD:""
REDIS_AUTHENTICATION="NONE"
```
Alternativley, if your Redis server requires a Username + Password in order to access, you can use the following:
Alternatively, if your Redis server requires a Username + Password in order to access, you can use the following:
```env
REDIS_HOST="your redis server host"
REDIS_PORT="your redis server port"
REDIS_TIMEOUT="your redis connection timeout"
REDIS_USING_PASSWORD="true"
REDIS_AUTHENTICATION="USERNAME_PASSWORD"
REDIS_USERNAME:"coolUsername"
REDIS_PASSWORD:"coolPassword"
```
Alternatively, if your Redis server requires a URL in order to access, you can use the following:
```env
REDIS_HOST="your redis server host"
REDIS_PORT="your redis server port"
REDIS_TIMEOUT="your redis connection timeout"
REDIS_AUTHENTICATION="URL"
REDIS_URL="coolURL"
```

#### Builder
When building your Twilight instance, you can specify your host and port like so:
```kotlin
val twilight = twilight(plugin) {
redis {
authentication = Authentication.NONE
host = "your redis server host"
port = 6379 // Default Redis Port
timeout = 500 // 500 Milliseconds Timeout
isUsingPassword = false // False by default
}
}
```
Alternativley, if your Redis server requires a Username + Password in order to access, you can use the following:
Alternatively, if your Redis server requires a Username + Password in order to access, you can use the following:
```kotlin
val twilight = twilight(plugin) {
redis {
authentication = Authentication.USERNAME_PASSWORD
host = "your redis server host"
port = 6379 // Default Redis Port
timeout = 500 // 500 Milliseconds Timeout
isUsingPassword = true
username = "coolUsername"
password = "coolPassword"
}
}
```
Alternatively, if your Redis server requires a URL in order to access, you can use the following:
```kotlin
val twilight = twilight(plugin) {
redis {
authentication = Authentication.URL
host = "your redis server host"
port = 6379 // Default Redis Port
timeout = 500 // 500 Milliseconds Timeout
url = "coolURL"
}
}
```
#### String Key-Value Pairs
You can Set/Get/Delete String Key-Value pairs on your Redis server like so: (All of those functions are Async and return a CompleteableFuture)
```kotlin
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.10"
version = "1.1.11"

repositories {
mavenLocal()
Expand Down
48 changes: 37 additions & 11 deletions src/main/kotlin/gg/flyte/twilight/data/Redis.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ import java.util.concurrent.Executors

object Redis {
private lateinit var jedis: Jedis
private lateinit var settings: Settings
private val executor: Executor = Executors.newCachedThreadPool()
fun redis(redis: Settings) {
if (redis.isUsingPassword){
val config = DefaultJedisClientConfig.builder().user(redis.username).password(redis.password).timeoutMillis(redis.timeout).build()
jedis = Jedis(redis.host, redis.port, config)
return
}
jedis = Jedis(redis.host, redis.port, redis.timeout)
jedis = getClient(redis)
settings = redis
}

private fun getClient(redis: Settings): Jedis = when (redis.authentication){
Authentication.NONE -> Jedis(redis.host, redis.port)
Authentication.USERNAME_PASSWORD -> Jedis(redis.host, redis.port, DefaultJedisClientConfig.builder().user(redis.username).password(redis.password).timeoutMillis(redis.timeout).build())
Authentication.URL -> Jedis(redis.url)
}

private fun publishSync(channel: String, message: String) = jedis.publish(channel, message)
fun publish(channel: String, message: String): CompletableFuture<Long> = CompletableFuture.supplyAsync({ publishSync(channel, message) }, executor)
private fun setSync(key: String, value: String) = jedis.set(key, value)
Expand All @@ -30,22 +34,44 @@ object Redis {
fun delete(key: String): CompletableFuture<Long> = CompletableFuture.supplyAsync({ deleteSync(key) }, executor)

fun addListener(listener: TwilightRedisListener): TwilightRedisListener {
jedis.subscribe(listener, listener.channel)
val client = getClient(settings)
client.subscribe(listener, listener.channel)
client.close()
return listener
}
fun addListener(channel: String, block: RedisMessage.() -> Unit): TwilightRedisListener {
val client = getClient(settings)
val listener = RedisListener(channel, block)
jedis.subscribe(listener, channel)
client.subscribe(listener, channel)
client.close()
return listener
}
class Settings {
var authentication = if (Twilight.usingEnv) Authentication.fromString(Environment.get("REDIS_AUTHENTICATION")) else Authentication.NONE
var host: String = if (Twilight.usingEnv) Environment.get("REDIS_HOST") else "localhost"
var port: Int = if (Twilight.usingEnv) Environment.get("REDIS_PORT").toInt() else 6379
var timeout: Int = if (Twilight.usingEnv) Environment.get("REDIS_TIMEOUT").toInt() else 0
var isUsingPassword: Boolean = if (Twilight.usingEnv) Environment.get("REDIS_USING_PASSWORD").toBoolean() else false
val username: String = if (Twilight.usingEnv && isUsingPassword) Environment.get("REDIS_USERNAME") else ""
var password: String = if (Twilight.usingEnv && isUsingPassword) Environment.get("REDIS_PASSWORD") else ""
val username: String = if (Twilight.usingEnv && authentication == Authentication.USERNAME_PASSWORD) Environment.get("REDIS_USERNAME") else ""
var password: String = if (Twilight.usingEnv && authentication == Authentication.USERNAME_PASSWORD) Environment.get("REDIS_PASSWORD") else ""
var url: String = if (Twilight.usingEnv && authentication == Authentication.URL) Environment.get("REDIS_URL") else ""
}

enum class Authentication {
NONE,
USERNAME_PASSWORD,
URL;

companion object {
fun fromString(value: String): Authentication = when (value.uppercase()) {
"NONE" -> NONE
"USERNAME_PASSWORD" -> USERNAME_PASSWORD
"URL" -> URL
else -> throw IllegalArgumentException("Invalid authentication type: $value")
}
}

}

}

data class RedisMessage(val channel: String, val message: String, val listener: TwilightRedisListener)
Expand Down

0 comments on commit 5cc0c06

Please sign in to comment.