RedisUtils

object RedisUtils

Factory methods and extension functions for creating and using Redis connections via Jedis.

Pool sizing and wait time can be configured via system properties or method parameters. The default Redis URL is read from the REDIS_URL environment variable.

A URL without a port uses 6379. A URL that can never work (one that is malformed, has no host, or carries a non-numeric database index or an unknown protocol) is a configuration error: every function that takes a URL throws IllegalArgumentException for it, rather than treating it as a connection failure.

Properties

Link copied to clipboard

System property key for the maximum number of idle connections.

Link copied to clipboard

System property key for the maximum connection pool size.

Link copied to clipboard

System property key for the maximum wait time (in seconds) when borrowing a connection.

Link copied to clipboard

System property key for the minimum number of idle connections.

Link copied to clipboard

Pool size meaning "no limit", as defined by commons-pool2.

Functions

Link copied to clipboard
fun newRedisClient(redisUrl: String = defaultRedisUrl, maxPoolSize: Int = System.getProperty(REDIS_MAX_POOL_SIZE)?.toInt() ?: 10, maxIdleSize: Int = System.getProperty(REDIS_MAX_IDLE_SIZE)?.toInt() ?: 5, minIdleSize: Int = System.getProperty(REDIS_MIN_IDLE_SIZE)?.toInt() ?: 1, maxWaitSecs: Long = System.getProperty(REDIS_MAX_WAIT_SECS)?.toLong() ?: 1L): RedisClient

Creates a new pooled RedisClient with the given configuration.

Link copied to clipboard
fun UnifiedJedis.scanKeys(pattern: String, count: Int = 100): Sequence<String>

Lazily scans Redis keys matching the given pattern using the SCAN command.

Link copied to clipboard
fun <T> withNonNullRedis(redisUrl: String = defaultRedisUrl, printStackTrace: Boolean = false, block: (RedisClient) -> T): T?

Creates a short-lived RedisClient connection, executes block with a non-null client, and closes it.

Link copied to clipboard
fun <T> RedisClient.withNonNullRedisPool(printStackTrace: Boolean = false, block: (RedisClient) -> T): T?

Executes block with this RedisClient, returning null if the connection fails.

Link copied to clipboard
fun <T> withRedis(redisUrl: String = defaultRedisUrl, printStackTrace: Boolean = false, block: (RedisClient?) -> T): T

Creates a short-lived RedisClient connection, executes block, and closes the client.

Link copied to clipboard
fun <T> RedisClient.withRedisPool(printStackTrace: Boolean = false, block: (RedisClient?) -> T): T

Executes block with this RedisClient, passing null if the connection fails.

Link copied to clipboard
suspend fun <T> withSuspendingNonNullRedis(redisUrl: String = defaultRedisUrl, printStackTrace: Boolean = false, block: suspend (RedisClient) -> T): T?

Suspending variant of withNonNullRedis. Creates a short-lived connection, executes a suspending block, and closes it. Returns null if the connection fails. Connecting, pinging and closing run on Dispatchers.IO; block runs in the caller's context.

Link copied to clipboard
suspend fun <T> RedisClient.withSuspendingNonNullRedisPool(printStackTrace: Boolean = false, block: suspend (RedisClient) -> T): T?

Suspending variant of withNonNullRedisPool. Executes a suspending block with this RedisClient, returning null if the connection fails.

Link copied to clipboard
suspend fun <T> withSuspendingRedis(redisUrl: String = defaultRedisUrl, printStackTrace: Boolean = false, block: suspend (RedisClient?) -> T): T

Suspending variant of withRedis. Creates a short-lived connection, executes a suspending block, and closes it.

Link copied to clipboard
suspend fun <T> RedisClient.withSuspendingRedisPool(printStackTrace: Boolean = false, block: suspend (RedisClient?) -> T): T

Suspending variant of withRedisPool. Executes a suspending block with this RedisClient, passing null if the connection fails.