main.misk.redis.RedisService.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of misk-redis Show documentation
Show all versions of misk-redis Show documentation
Open source application container in Kotlin
package misk.redis
import com.google.common.util.concurrent.AbstractIdleService
import jakarta.inject.Inject
import com.google.inject.Provider
import jakarta.inject.Singleton
/** Controls the connection lifecycle for Redis. */
@Singleton
class RedisService @Inject internal constructor(
private val redisProvider: Provider
) : AbstractIdleService() {
// We initialize the client in startUp because creating the client will connect it to Redis
private lateinit var redis: Redis
override fun startUp() {
// Create the client and connect to the redis instance
redis = redisProvider.get()
}
override fun shutDown() {
if (::redis.isInitialized) {
redis.close()
}
}
}