-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from flytegg/pr/redis
Pr/redis
- Loading branch information
Showing
5 changed files
with
201 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package gg.flyte.twilight.data | ||
|
||
import gg.flyte.twilight.Twilight | ||
import gg.flyte.twilight.environment.Environment | ||
import redis.clients.jedis.DefaultJedisClientConfig | ||
import redis.clients.jedis.Jedis | ||
import redis.clients.jedis.JedisPubSub | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.concurrent.Executor | ||
import java.util.concurrent.Executors | ||
|
||
object Redis { | ||
private lateinit var jedis: Jedis | ||
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) | ||
} | ||
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) | ||
fun set(key: String, value: String): CompletableFuture<String> = CompletableFuture.supplyAsync({ setSync(key, value) }, executor) | ||
private fun getSync(key: String) = jedis.get(key) | ||
fun get(key: String): CompletableFuture<String> = CompletableFuture.supplyAsync({ getSync(key) }, executor) | ||
private fun deleteSync(key: String) = jedis.del(key) | ||
fun delete(key: String): CompletableFuture<Long> = CompletableFuture.supplyAsync({ deleteSync(key) }, executor) | ||
|
||
fun addListener(listener: TwilightRedisListener): TwilightRedisListener { | ||
jedis.subscribe(listener, listener.channel) | ||
return listener | ||
} | ||
fun addListener(channel: String, block: RedisMessage.() -> Unit): TwilightRedisListener { | ||
val listener = RedisListener(channel, block) | ||
jedis.subscribe(listener, channel) | ||
return listener | ||
} | ||
class Settings { | ||
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 "" | ||
} | ||
} | ||
|
||
data class RedisMessage(val channel: String, val message: String, val listener: TwilightRedisListener) | ||
|
||
abstract class TwilightRedisListener(val channel: String) : JedisPubSub() { | ||
override fun onMessage(channel: String?, message: String?) { | ||
channel ?: return | ||
message?: return | ||
if (channel == this.channel) onMessage(message) | ||
} | ||
abstract fun onMessage(message: String) | ||
fun unregister() = unsubscribe() | ||
} | ||
|
||
class RedisListener(channel: String, val block: RedisMessage.() -> Unit): TwilightRedisListener(channel) { | ||
override fun onMessage(message: String) { | ||
block(RedisMessage(channel, message, this)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package gg.flyte.twilight | ||
|
||
import gg.flyte.twilight.data.Redis | ||
|
||
fun addingListenersTest(){ | ||
|
||
val listener = Redis.addListener("cool-channel"){ | ||
println("The following message was received: '$message' on channel '$channel'") | ||
this.listener.unregister() | ||
} | ||
|
||
|
||
Redis.set("cool-key", "super-secret-value") | ||
|
||
val future = Redis.get("cool-key") // Returns a Completable Future | ||
|
||
future.thenApplyAsync { | ||
value -> println("The value is: $value") // Prints: "The value is: super-secret-value" | ||
}.exceptionally { | ||
e -> println("An exception occurred: ${e.message}") // Handle the Exception | ||
} | ||
|
||
Thread.sleep(1000) | ||
|
||
Redis.delete("cool-key") | ||
|
||
|
||
|
||
|
||
} |