feat:实现异步缓存支持

This commit is contained in:
wangyu 2021-12-02 20:49:33 +08:00
parent b7fdbba498
commit 5d70081603
2 changed files with 13 additions and 11 deletions

View File

@ -3,9 +3,7 @@ package com.flyfish.framework.service;
import com.flyfish.framework.domain.po.User;
import com.flyfish.framework.repository.ReactiveUserRepository;
import com.flyfish.framework.service.impl.BaseReactiveServiceImpl;
import com.flyfish.framework.utils.RedisOperations;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import com.flyfish.framework.utils.ReactiveRedisOperations;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
@ -20,7 +18,7 @@ import javax.annotation.Resource;
public class ReactiveUserService extends BaseReactiveServiceImpl<User> {
@Resource
private RedisOperations redisOperations;
private ReactiveRedisOperations reactiveRedisOperations;
/**
* 获取用户数据
@ -28,14 +26,18 @@ public class ReactiveUserService extends BaseReactiveServiceImpl<User> {
* @param username 用户
* @return 结果
*/
@Cacheable
public Mono<User> findByUsername(String username) {
if (redisOperations.hasKey(getCacheKey(username))) {
return
}
return ((ReactiveUserRepository) repository).findByUsername(username);
String key = getCacheKey(username);
return reactiveRedisOperations.hasKey(key).flatMap(exists -> exists ? reactiveRedisOperations.get(key) :
((ReactiveUserRepository) repository).findByUsername(username));
}
/**
* 获取缓存键
*
* @param username 用户名
* @return 结果
*/
private String getCacheKey(String username) {
return "user-" + username;
}

View File

@ -43,8 +43,8 @@ public class ReactiveRedisConfig {
}
@Bean
public ReactiveRedisOperations redisOperations(ReactiveStringRedisTemplate reactiveStringRedisTemplate,
ReactiveRedisTemplate<String, Object> reactiveRedisTemplate) {
public ReactiveRedisOperations reactiveRedisOperations(ReactiveStringRedisTemplate reactiveStringRedisTemplate,
ReactiveRedisTemplate<String, Object> reactiveRedisTemplate) {
return new ReactiveRedisOperations(reactiveRedisTemplate, reactiveStringRedisTemplate);
}