-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22d0f05
commit 34f6813
Showing
63 changed files
with
16,352 additions
and
54 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
= Kotlin API | ||
|
||
=== Suspendable API based on either reactive or async operations | ||
|
||
Example for retrieving commands and using it: | ||
|
||
[source,kt] | ||
---- | ||
val cmd1: RedisSuspendableCommands<String, String> = connection.async().asSuspendable() | ||
val cmd2: RedisSuspendableCommands<String, String> = connection.reactive().asSuspendable() | ||
val foo1 = cmd1.set("foo", "bar") | ||
val foo2 = cmd2.keys("fo*") | ||
---- | ||
|
||
=== Extensions for existing APIs | ||
|
||
==== Transactions DSL | ||
|
||
Example for sync: | ||
|
||
[source,kt] | ||
---- | ||
val result: TransactionResult? = connection.sync().multi { | ||
set("foo", "bar") | ||
get("foo") | ||
} | ||
---- | ||
|
||
Example for async with coroutines: | ||
|
||
[source,kt] | ||
---- | ||
val result: TransactionResult? = connection.async().multi { | ||
set("foo", "bar").await() | ||
get("foo").await() | ||
} | ||
---- |
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/io/lettuce/core/ExperimentalLettuceCoroutinesApi.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.lettuce.core | ||
|
||
import kotlin.RequiresOptIn.Level.WARNING | ||
import kotlin.annotation.AnnotationRetention.BINARY | ||
import kotlin.annotation.AnnotationTarget.* | ||
|
||
/** | ||
* @author sokomishalov | ||
*/ | ||
@RequiresOptIn(level = WARNING) | ||
@Retention(BINARY) | ||
@Target( | ||
CLASS, | ||
ANNOTATION_CLASS, | ||
PROPERTY, | ||
FIELD, | ||
LOCAL_VARIABLE, | ||
VALUE_PARAMETER, | ||
CONSTRUCTOR, | ||
FUNCTION, | ||
PROPERTY_GETTER, | ||
PROPERTY_SETTER, | ||
TYPEALIAS | ||
) | ||
annotation class ExperimentalLettuceCoroutinesApi |
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/io/lettuce/core/api/async/RedisAsyncCommandsExtensions.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.lettuce.core.api.async | ||
|
||
import io.lettuce.core.ExperimentalLettuceCoroutinesApi | ||
import io.lettuce.core.TransactionResult | ||
import io.lettuce.core.api.coroutines.RedisSuspendableCommands | ||
import io.lettuce.core.api.coroutines.async.RedisSuspendableAsyncCommandsImpl | ||
import kotlinx.coroutines.future.await | ||
|
||
|
||
/** | ||
* Extension for [RedisAsyncCommands] to create [RedisSuspendableCommands] | ||
* | ||
* @author Mikhael Sokolov | ||
* @since 6.0 | ||
*/ | ||
@ExperimentalLettuceCoroutinesApi | ||
fun <K, V> RedisAsyncCommands<K, V>.asSuspendable(): RedisSuspendableCommands<K, V> { | ||
return RedisSuspendableAsyncCommandsImpl(this) | ||
} | ||
|
||
/** | ||
* Allows to create transaction DSL block with [RedisAsyncCommands]. | ||
* | ||
* @author Mikhael Sokolov | ||
* @since 6.0 | ||
*/ | ||
@ExperimentalLettuceCoroutinesApi | ||
suspend inline fun <K, V> RedisAsyncCommands<K, V>.multi(action: RedisAsyncCommands<K, V>.() -> Unit): TransactionResult? { | ||
multi().await() | ||
runCatching { | ||
action.invoke(this) | ||
}.onFailure { | ||
discard() | ||
} | ||
return exec().await() | ||
} |
Oops, something went wrong.