redis代码迁徙到redis-reactive

本文介绍了如何将传统的Spring Data Redis操作转换为响应式风格,使用ReactiveRedisTemplate进行数据缓存和检索,包括字符串、列表、集合、有序集合和哈希表的增删查改操作,并涉及到过期时间设置。代码示例展示了关键方法的实现,适用于需要高性能数据访问的场景。
摘要由CSDN通过智能技术生成

原来获取方式(部分示例):

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.stereotype.Repository
import java.time.Duration
import java.util.concurrent.TimeUnit
/**
* 记得订阅,记得订阅,记得订阅
*/
@Repository
class RedisData(
    @Autowired val redisTemplate: RedisTemplate<String, String>
) {
    //缓存到Redis,expireSecond秒后过期 记得订阅
    fun saveString(key: String,value: String,expireSecond: Long? = null) =
        expireSecond?.let {
            redisTemplate.opsForValue().set(key, value, Duration.ofSeconds(it))
        }?: redisTemplate.opsForValue().set(key,value)
    

    //从Redis获取缓存值
    fun getString(key: String):String?=redisTemplate.opsForValue().get(key)

    //将List缓存到Redis,expireSecond秒后过期
    fun saveList(key: String,values: Flux<String>,duration: Duration?= null) =
        values.flatMap { redisTemplate.opsForList().leftPush(key,it) }
            .flatMap { if (it>0) {expire(key,duration) ?: Mono.just(it)} else Mono.just(false) }

    //从Redis获取List中的元素,可以指定元素的范围
    fun getListValue(key: String,start: Long,end: Long): List<String>? =
        redisTemplate.opsForList().range(key,start,end)

    //将Set缓存到Redis,expireSecond秒后过期
    fun saveSet(key: String,values: Array<String>,duration: Duration?= null) =
        redisTemplate.opsForSet().add(key,*values).doOnNext { duration?.let { expire(key,duration) } }

    //从Redis获取缓存的set集合
    fun getSetValues(key: String):Set<String>?= redisTemplate.opsForSet().members(key)

    //从Redis获取两个set中不相同的元素
    fun getSetDiff(key1:String, key2:String):Set<String>?= redisTemplate.opsForSet().difference(key1,key2)

    //将一组pair元素缓存到Redis,expireSecond秒后过期
    fun saveZset(key: String,values: Array<Pair<String,Double>>,expireSecond: Long?= null){
        values.forEach {
            redisTemplate.opsForZSet().add(key,it.first,it.second)
        }
        expire(key,expireSecond)
    }

    //从redis获取在某个区间的值
    fun getZsetRangeByScore(key: String,minScore:Double,maxScore: Double): Set<String>?=
            redisTemplate.opsForZSet().rangeByScore(key,minScore,maxScore)

    //将一个map缓存到Redis,expireSecond秒后过期
    fun saveHashMap(key: String,values: Map<String,String>,expireSecond: Long?= null){
        redisTemplate.opsForHash<String,String>().putAll(key,values)
        expire(key,expireSecond)
    }

    //从redis获取一个map的所有values
    fun getHashValues(key: String): List<String>?= redisTemplate.opsForHash<String,String>().values(key)

    fun expire(key: String,expireSecond: Long?){
        expireSecond?.let {
            redisTemplate.expire(key,it,TimeUnit.SECONDS)
        }
    }
}

改为(未全部测试每个一个方法):

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Range
import org.springframework.data.redis.connection.RedisZSetCommands
import org.springframework.data.redis.core.ReactiveRedisTemplate
import org.springframework.stereotype.Repository
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import java.time.Duration


@Repository
class RedisData(
    @Autowired val redisTemplate: ReactiveRedisTemplate<String, String>
) {

    //缓存到Redis,expireSecond秒后过期
    fun saveString(key: String,value: String,expireSecond: Long? = null){
        expireSecond?.let {
            redisTemplate.opsForValue().set(key, value, Duration.ofSeconds(it))
        }?: redisTemplate.opsForValue().set(key,value)
    }

    //从Redis获取缓存值
    fun getString(key: String): Mono<String>? =redisTemplate.opsForValue().get(key)


    //将List缓存到Redis,expireSecond秒后过期
    fun saveList(key: String,values: Flux<String>,duration: Duration?= null) =
        values.flatMap { redisTemplate.opsForList().leftPush(key,it) }
            .flatMap { if (it>0) {expire(key,duration) ?: Mono.just(it)} else Mono.just(false) }

    //从Redis获取List中的元素,可以指定元素的范围
    fun getListValue(key: String,start: Long,end: Long): Flux<String> =
        redisTemplate.opsForList().range(key,start,end)

    //将Set缓存到Redis,expireSecond秒后过期
    fun saveSet(key: String,values: Array<String>,duration: Duration?= null){
        redisTemplate.opsForSet().add(key,*values)
        expire(key,duration)
    }

    //从Redis获取缓存的set集合
    fun getSetValues(key: String): Flux<String> = redisTemplate.opsForSet().members(key)

    //从Redis获取两个set中不相同的元素
    fun getSetDiff(key1:String, key2:String): Flux<String> = redisTemplate.opsForSet().difference(key1,key2)

    //将一组pair元素缓存到Redis,expireSecond秒后过期
    fun saveZset(key: String,values: Flux<Pair<String,Double>>,duration: Duration?= null) =
        values.flatMap { redisTemplate.opsForZSet().add(key,it.first,it.second) }
            .flatMap { if (it){expire(key,duration) ?: Mono.just(it)} else Mono.just(false) }

    //从redis获取在某个区间的值
    fun getZsetRangeByScore(key: String, minScore: Range<Double>, limit: RedisZSetCommands.Limit?=null): Flux<String> =
        limit?.let { redisTemplate.opsForZSet().rangeByScore(key,minScore, it) }
            ?: redisTemplate.opsForZSet().rangeByScore(key,minScore)

    //将一个map缓存到Redis,expireSecond秒后过期
    fun saveHashMap(key: String,values: Map<String,String>,duration: Duration?= null) =
        redisTemplate.opsForHash<String,String>().putAll(key,values)
            .flatMap { if (it){expire(key,duration) ?: Mono.just(it)} else Mono.just(false) }

    //从redis获取一个map的所有values
    fun getHashValues(key: String): Flux<String> = redisTemplate.opsForHash<String,String>().values(key)

    fun expire(key: String,duration: Duration?):Mono<Boolean>?= duration?.let { redisTemplate.expire(key,duration) }

}

业务中用到发现有问题,再重新编辑。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值