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

org.springframework.data.redis.core.ReactiveRedisOperationsExtensions.kt Maven / Gradle / Ivy

There is a newer version: 3.2.5
Show newest version
/*
 * Copyright 2019-2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.data.redis.core

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.reactive.asFlow
import kotlinx.coroutines.reactive.asPublisher
import kotlinx.coroutines.reactive.awaitFirstOrNull
import kotlinx.coroutines.reactive.awaitSingle
import org.springframework.data.redis.connection.DataType
import org.springframework.data.redis.connection.ReactiveRedisConnection
import org.springframework.data.redis.connection.ReactiveSubscription.*
import org.springframework.data.redis.core.script.RedisScript
import org.springframework.data.redis.listener.Topic
import org.springframework.data.redis.serializer.RedisElementReader
import org.springframework.data.redis.serializer.RedisElementWriter
import java.time.Duration
import java.time.Instant

/**
 * Coroutines variant of [ReactiveRedisOperations.execute].
 *
 * @author Sebastien Deleuze
 * @since 2.2
 */
fun  ReactiveRedisOperations.executeAsFlow(action: (ReactiveRedisConnection) -> Flow): Flow =
		execute { action(it).asPublisher() }.asFlow()

/**
 * Coroutines variant of [ReactiveRedisOperations.execute].
 *
 * @author Sebastien Deleuze
 * @since 2.2
 */
fun  ReactiveRedisOperations.executeAsFlow(script: RedisScript, keys: List = emptyList(), args: List<*> = emptyList()): Flow =
		execute(script, keys, args).asFlow()

/**
 * Coroutines variant of [ReactiveRedisOperations.execute].
 *
 * @author Sebastien Deleuze
 * @since 2.2
 */
fun  ReactiveRedisOperations.executeAsFlow(script: RedisScript, keys: List = emptyList(), args: List<*> = emptyList(), argsWriter: RedisElementWriter<*>, resultReader: RedisElementReader): Flow =
		execute(script, keys, args, argsWriter, resultReader).asFlow()

/**
 * Coroutines variant of [ReactiveRedisOperations.convertAndSend].
 *
 * @author Mark Paluch
 * @since 2.2
 */
suspend fun  ReactiveRedisOperations.sendAndAwait(destination: String, message: V): Long =
		convertAndSend(destination, message).awaitSingle()

/**
 * Coroutines variant of [ReactiveRedisOperations.listenToChannel].
 *
 * @author Sebastien Deleuze
 * @since 2.2
 */
fun  ReactiveRedisOperations.listenToChannelAsFlow(vararg channels: String): Flow> =
		listenToChannel(*channels).asFlow()

/**
 * Coroutines variant of [ReactiveRedisOperations.listenToPattern].
 *
 * @author Sebastien Deleuze
 * @since 2.2
 */
fun  ReactiveRedisOperations.listenToPatternAsFlow(vararg patterns: String): Flow> =
		listenToPattern(*patterns).asFlow()

/**
 * Coroutines variant of [ReactiveRedisOperations.listenTo].
 *
 * @author Sebastien Deleuze
 * @since 2.2
 */
fun  ReactiveRedisOperations.listenToAsFlow(vararg topics: Topic): Flow> =
		listenTo(*topics).asFlow()

/**
 * Coroutines variant of [ReactiveRedisOperations.hasKey].
 *
 * @author Mark Paluch
 * @since 2.2
 */
suspend fun  ReactiveRedisOperations.hasKeyAndAwait(key: K): Boolean =
		hasKey(key).awaitSingle()

/**
 * Coroutines variant of [ReactiveRedisOperations.type].
 *
 * @author Mark Paluch
 * @since 2.2
 */
suspend fun  ReactiveRedisOperations.typeAndAwait(key: K): DataType =
		type(key).awaitSingle()

/**
 * Coroutines variant of [ReactiveRedisOperations.keys].
 *
 * @author Sebastien Deleuze
 * @since 2.2
 */
fun  ReactiveRedisOperations.keysAsFlow(pattern: K): Flow =
		keys(pattern).asFlow()

/**
 * Coroutines variant of [ReactiveRedisOperations.scan].
 *
 * @author Sebastien Deleuze
 * @since 2.2
 */
fun  ReactiveRedisOperations.scanAsFlow(options: ScanOptions = ScanOptions.NONE): Flow =
		scan(options).asFlow()

/**
 * Coroutines variant of [ReactiveRedisOperations.randomKey].
 *
 * @author Mark Paluch
 * @since 2.2
 */
suspend fun  ReactiveRedisOperations.randomKeyAndAwait(): K? =
		randomKey().awaitFirstOrNull()

/**
 * Coroutines variant of [ReactiveRedisOperations.rename].
 *
 * @author Mark Paluch
 * @since 2.2
 */
suspend fun  ReactiveRedisOperations.renameAndAwait(oldKey: K, newKey: K): Boolean =
		rename(oldKey, newKey).awaitSingle()

/**
 * Coroutines variant of [ReactiveRedisOperations.renameIfAbsent].
 *
 * @author Mark Paluch
 * @since 2.2
 */
suspend fun  ReactiveRedisOperations.renameIfAbsentAndAwait(oldKey: K, newKey: K): Boolean =
		renameIfAbsent(oldKey, newKey).awaitSingle()

/**
 * Coroutines variant of [ReactiveRedisOperations.delete].
 *
 * @author Mark Paluch
 * @since 2.2
 */
suspend fun  ReactiveRedisOperations.deleteAndAwait(vararg key: K): Long =
		delete(*key).awaitSingle()

/**
 * Coroutines variant of [ReactiveRedisOperations.unlink].
 *
 * @author Mark Paluch
 * @since 2.2
 */
suspend fun  ReactiveRedisOperations.unlinkAndAwait(vararg key: K): Long =
		unlink(*key).awaitSingle()

/**
 * Coroutines variant of [ReactiveRedisOperations.expire].
 *
 * @author Mark Paluch
 * @since 2.2
 */
suspend fun  ReactiveRedisOperations.expireAndAwait(key: K, timeout: Duration): Boolean =
		expire(key, timeout).awaitSingle()

/**
 * Coroutines variant of [ReactiveRedisOperations.expireAt].
 *
 * @author Mark Paluch
 * @since 2.2
 */
suspend fun  ReactiveRedisOperations.expireAtAndAwait(key: K, expireAt: Instant): Boolean = expireAt(key, expireAt).awaitSingle()


/**
 * Coroutines variant of [ReactiveRedisOperations.persist].
 *
 * @author Mark Paluch
 * @since 2.2
 */
suspend fun  ReactiveRedisOperations.persistAndAwait(key: K): Boolean =
		persist(key).awaitSingle()

/**
 * Coroutines variant of [ReactiveRedisOperations.move].
 *
 * @author Mark Paluch
 * @since 2.2
 */
suspend fun  ReactiveRedisOperations.moveAndAwait(key: K, dbIndex: Int): Boolean = move(key, dbIndex).awaitSingle()

/**
 * Coroutines variant of [ReactiveRedisOperations.getExpire].
 *
 * @author Mark Paluch
 * @since 2.2
 */
suspend fun  ReactiveRedisOperations.getExpireAndAwait(key: K): Duration? = getExpire(key).awaitFirstOrNull()




© 2015 - 2024 Weber Informatics LLC | Privacy Policy