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.

Only the redis and rediss (TLS) schemes are accepted, in any case. Any other scheme, including TLS spellings such as tls or redis+tls, is rejected rather than connecting without TLS.

A URL without a port uses 6379. A URL that can never work (one that is malformed, has no host, has another scheme or a port that is not a number from 1 to 65535, has a path other than a database index such as /3, carries an unknown protocol, or has an @ in its query or fragment) is a configuration error: every function that takes a URL throws IllegalArgumentException for it, rather than treating it as a connection failure. No such message repeats any part of the URL, which may hold a password.

Percent-escapes in the user name and password are decoded after the user info is split at its first :, so an escaped %3A stays in the user name, and a literal + stays a +. A /, ?, # or @ in either must be percent-encoded (%2F, %3F, %23, %40): unescaped, an @ ends the user info too early, and each of the others ends the authority before the host. An unescaped / leaves a path that is not a database index, and an unescaped ? or # leaves the @ and the real host in the query or fragment, so each such URL is rejected rather than connecting to the user name as the host.

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

Escapes this string for use as literal text in a Redis glob pattern, such as a scanKeys pattern or the pattern of KEYS, SCAN ... MATCH, HSCAN, SSCAN or ZSCAN.

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. Cancellation while connecting closes the client without running block, as in withSuspendingRedis.

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.