All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sksamuel.cohort.lettuce.RedisConnectionWarmup.kt Maven / Gradle / Ivy

There is a newer version: 2.6.0
Show newest version
package com.sksamuel.cohort.lettuce

import com.sksamuel.cohort.Warmup
import io.lettuce.core.api.StatefulRedisConnection
import kotlinx.coroutines.future.await
import kotlin.random.Random

/**
 * A Cohort [Warmup] that uses a supplied Lettuce [StatefulRedisConnection]
 * to execute commands against a standalone redis instance.
 *
 * By default, the [eval] function will set elements with a 1 second TTL under random keys with
 * the prefix "cohort_warmup". Any chain of commands can be used by providing a custom [eval] function.
 */
class RedisConnectionWarmup(
   private val conn: StatefulRedisConnection,
   private val eval: suspend (StatefulRedisConnection) -> Unit,
) : Warmup {

   companion object {
      operator fun invoke(
         conn: StatefulRedisConnection,
      ): RedisConnectionWarmup {
         return RedisConnectionWarmup(conn) {
            val key = "cohort_warmup_" + Random.nextInt()
            it.async().incr(key).await()
            it.async().expire(key, 1).await()
            it.async().get(key).await()
         }
      }
   }

   override val name: String = "redis_warmup"

   override suspend fun warm(iteration: Int) {
      eval(conn)
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy