Skip to content

Instantly share code, notes, and snippets.

@automationhacks
Created May 30, 2020 01:55
Show Gist options
  • Save automationhacks/38a345e245585904f27b9d5f67377e5f to your computer and use it in GitHub Desktop.
Save automationhacks/38a345e245585904f27b9d5f67377e5f to your computer and use it in GitHub Desktop.
Simple Kotlin file with an abstraction over jedis (JVM support library for redis)
import core.logging.Logger
import redis.clients.jedis.Jedis
class RedisHandler(val host: String = "127.0.0.1", private val port: Int = 6379) {
private var jedis = Jedis(host, port)
private fun refreshConnection() {
jedis = Jedis(host, port)
}
private fun closeConnection() {
jedis.close()
}
fun get(key: String): String? {
refreshConnection()
val result = jedis.get(key)
Logger.log("[Redis] Searching server=<$host> with key=<$key> gave result=<$result>")
closeConnection()
return result
}
fun set(key: String, value: String) {
refreshConnection()
jedis.set(key, value)
Logger.log("[Redis] Updated key=$key, value=$value pair in redis=$host")
closeConnection()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment