feat:实现异步缓存支持
This commit is contained in:
parent
b7fdbba498
commit
5d70081603
@ -3,9 +3,7 @@ package com.flyfish.framework.service;
|
|||||||
import com.flyfish.framework.domain.po.User;
|
import com.flyfish.framework.domain.po.User;
|
||||||
import com.flyfish.framework.repository.ReactiveUserRepository;
|
import com.flyfish.framework.repository.ReactiveUserRepository;
|
||||||
import com.flyfish.framework.service.impl.BaseReactiveServiceImpl;
|
import com.flyfish.framework.service.impl.BaseReactiveServiceImpl;
|
||||||
import com.flyfish.framework.utils.RedisOperations;
|
import com.flyfish.framework.utils.ReactiveRedisOperations;
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
|
||||||
import org.springframework.data.redis.core.ReactiveRedisTemplate;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
@ -20,7 +18,7 @@ import javax.annotation.Resource;
|
|||||||
public class ReactiveUserService extends BaseReactiveServiceImpl<User> {
|
public class ReactiveUserService extends BaseReactiveServiceImpl<User> {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private RedisOperations redisOperations;
|
private ReactiveRedisOperations reactiveRedisOperations;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取用户数据
|
* 获取用户数据
|
||||||
@ -28,14 +26,18 @@ public class ReactiveUserService extends BaseReactiveServiceImpl<User> {
|
|||||||
* @param username 用户
|
* @param username 用户
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Cacheable
|
|
||||||
public Mono<User> findByUsername(String username) {
|
public Mono<User> findByUsername(String username) {
|
||||||
if (redisOperations.hasKey(getCacheKey(username))) {
|
String key = getCacheKey(username);
|
||||||
return
|
return reactiveRedisOperations.hasKey(key).flatMap(exists -> exists ? reactiveRedisOperations.get(key) :
|
||||||
}
|
((ReactiveUserRepository) repository).findByUsername(username));
|
||||||
return ((ReactiveUserRepository) repository).findByUsername(username);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取缓存键
|
||||||
|
*
|
||||||
|
* @param username 用户名
|
||||||
|
* @return 结果
|
||||||
|
*/
|
||||||
private String getCacheKey(String username) {
|
private String getCacheKey(String username) {
|
||||||
return "user-" + username;
|
return "user-" + username;
|
||||||
}
|
}
|
||||||
|
@ -43,8 +43,8 @@ public class ReactiveRedisConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ReactiveRedisOperations redisOperations(ReactiveStringRedisTemplate reactiveStringRedisTemplate,
|
public ReactiveRedisOperations reactiveRedisOperations(ReactiveStringRedisTemplate reactiveStringRedisTemplate,
|
||||||
ReactiveRedisTemplate<String, Object> reactiveRedisTemplate) {
|
ReactiveRedisTemplate<String, Object> reactiveRedisTemplate) {
|
||||||
return new ReactiveRedisOperations(reactiveRedisTemplate, reactiveStringRedisTemplate);
|
return new ReactiveRedisOperations(reactiveRedisTemplate, reactiveStringRedisTemplate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user