15
15
*/
16
16
package org.springframework.data.redis.core
17
17
18
+ import kotlinx.coroutines.ExperimentalCoroutinesApi
19
+ import kotlinx.coroutines.flow.Flow
20
+ import kotlinx.coroutines.reactive.asFlow
21
+ import kotlinx.coroutines.reactive.asPublisher
18
22
import kotlinx.coroutines.reactive.awaitFirstOrNull
19
23
import kotlinx.coroutines.reactive.awaitSingle
20
24
import org.springframework.data.redis.connection.DataType
25
+ import org.springframework.data.redis.connection.ReactiveRedisConnection
26
+ import org.springframework.data.redis.connection.ReactiveSubscription.*
27
+ import org.springframework.data.redis.core.script.RedisScript
28
+ import org.springframework.data.redis.listener.Topic
29
+ import org.springframework.data.redis.serializer.RedisElementReader
30
+ import org.springframework.data.redis.serializer.RedisElementWriter
21
31
import java.time.Duration
22
32
import java.time.Instant
23
33
34
+ /* *
35
+ * Coroutines variant of [ReactiveRedisOperations.execute].
36
+ *
37
+ * @author Sebastien Deleuze
38
+ * @since 2.2
39
+ */
40
+ @ExperimentalCoroutinesApi
41
+ fun <K : Any , V : Any , T : Any > ReactiveRedisOperations <K , V >.executeAsFlow (action : (ReactiveRedisConnection ) -> Flow <T >): Flow <T > =
42
+ execute { action(it).asPublisher() }.asFlow()
43
+
44
+ /* *
45
+ * Coroutines variant of [ReactiveRedisOperations.execute].
46
+ *
47
+ * @author Sebastien Deleuze
48
+ * @since 2.2
49
+ */
50
+ @ExperimentalCoroutinesApi
51
+ fun <K : Any , V : Any , T : Any > ReactiveRedisOperations <K , V >.executeAsFlow (script : RedisScript <T >, keys : List <K > = emptyList(), args : List <* > = emptyList<Any >()): Flow <T > =
52
+ execute(script, keys, args).asFlow()
53
+
54
+ /* *
55
+ * Coroutines variant of [ReactiveRedisOperations.execute].
56
+ *
57
+ * @author Sebastien Deleuze
58
+ * @since 2.2
59
+ */
60
+ @ExperimentalCoroutinesApi
61
+ fun <K : Any , V : Any , T : Any > ReactiveRedisOperations <K , V >.executeAsFlow (script : RedisScript <T >, keys : List <K > = emptyList(), args : List <* > = emptyList<Any >(), argsWriter : RedisElementWriter <* >, resultReader : RedisElementReader <T >): Flow <T > =
62
+ execute(script, keys, args, argsWriter, resultReader).asFlow()
63
+
24
64
/* *
25
65
* Coroutines variant of [ReactiveRedisOperations.convertAndSend].
26
66
*
@@ -30,6 +70,36 @@ import java.time.Instant
30
70
suspend fun <K : Any , V : Any > ReactiveRedisOperations <K , V >.sendAndAwait (destination : String , message : V ): Long =
31
71
convertAndSend(destination, message).awaitSingle()
32
72
73
+ /* *
74
+ * Coroutines variant of [ReactiveRedisOperations.listenToChannel].
75
+ *
76
+ * @author Sebastien Deleuze
77
+ * @since 2.2
78
+ */
79
+ @ExperimentalCoroutinesApi
80
+ fun <K : Any , V : Any > ReactiveRedisOperations <K , V >.listenToChannelAsFlow (vararg channels : String ): Flow <Message <String , V >> =
81
+ listenToChannel(* channels).asFlow()
82
+
83
+ /* *
84
+ * Coroutines variant of [ReactiveRedisOperations.listenToPattern].
85
+ *
86
+ * @author Sebastien Deleuze
87
+ * @since 2.2
88
+ */
89
+ @ExperimentalCoroutinesApi
90
+ fun <K : Any , V : Any > ReactiveRedisOperations <K , V >.listenToPatternAsFlow (vararg patterns : String ): Flow <Message <String , V >> =
91
+ listenToPattern(* patterns).asFlow()
92
+
93
+ /* *
94
+ * Coroutines variant of [ReactiveRedisOperations.listenTo].
95
+ *
96
+ * @author Sebastien Deleuze
97
+ * @since 2.2
98
+ */
99
+ @ExperimentalCoroutinesApi
100
+ fun <K : Any , V : Any > ReactiveRedisOperations <K , V >.listenToAsFlow (vararg topics : Topic ): Flow <Message <String , V >> =
101
+ listenTo(* topics).asFlow()
102
+
33
103
/* *
34
104
* Coroutines variant of [ReactiveRedisOperations.hasKey].
35
105
*
@@ -48,6 +118,26 @@ suspend fun <K : Any, V : Any> ReactiveRedisOperations<K, V>.hasKeyAndAwait(key:
48
118
suspend fun <K : Any , V : Any > ReactiveRedisOperations <K , V >.typeAndAwait (key : K ): DataType =
49
119
type(key).awaitSingle()
50
120
121
+ /* *
122
+ * Coroutines variant of [ReactiveRedisOperations.keys].
123
+ *
124
+ * @author Sebastien Deleuze
125
+ * @since 2.2
126
+ */
127
+ @ExperimentalCoroutinesApi
128
+ fun <K : Any , V : Any > ReactiveRedisOperations <K , V >.keysAsFlow (pattern : K ): Flow <K > =
129
+ keys(pattern).asFlow()
130
+
131
+ /* *
132
+ * Coroutines variant of [ReactiveRedisOperations.scan].
133
+ *
134
+ * @author Sebastien Deleuze
135
+ * @since 2.2
136
+ */
137
+ @ExperimentalCoroutinesApi
138
+ fun <K : Any , V : Any > ReactiveRedisOperations <K , V >.scanAsFlow (options : ScanOptions = ScanOptions .NONE ): Flow <K > =
139
+ scan(options).asFlow()
140
+
51
141
/* *
52
142
* Coroutines variant of [ReactiveRedisOperations.randomKey].
53
143
*
0 commit comments