原来获取方式(部分示例):
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) }
}
业务中用到发现有问题,再重新编辑。