Created
May 30, 2020 01:55
-
-
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)
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
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