perf: 集成 Spring Cache,优化查询用户昵称性能
This commit is contained in:
parent
6a35c00b7a
commit
b23b00d02a
@ -21,6 +21,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
|
|
||||||
import org.redisson.codec.JsonJacksonCodec;
|
import org.redisson.codec.JsonJacksonCodec;
|
||||||
import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer;
|
import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer;
|
||||||
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
@ -33,6 +34,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
* @since 2022/12/28 23:17
|
* @since 2022/12/28 23:17
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@EnableCaching
|
||||||
@Configuration
|
@Configuration
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class RedissonConfiguration {
|
public class RedissonConfiguration {
|
||||||
|
@ -42,4 +42,9 @@ public class CacheConsts {
|
|||||||
* 限流键前缀
|
* 限流键前缀
|
||||||
*/
|
*/
|
||||||
public static final String LIMIT_KEY_PREFIX = "LIMIT";
|
public static final String LIMIT_KEY_PREFIX = "LIMIT";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户缓存键前缀
|
||||||
|
*/
|
||||||
|
public static final String USER_KEY_PREFIX = "USER";
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,8 @@ import javax.annotation.Resource;
|
|||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
import org.springframework.cache.annotation.CacheConfig;
|
||||||
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
@ -38,6 +40,7 @@ import cn.hutool.core.util.StrUtil;
|
|||||||
|
|
||||||
import top.charles7c.cnadmin.common.base.BaseServiceImpl;
|
import top.charles7c.cnadmin.common.base.BaseServiceImpl;
|
||||||
import top.charles7c.cnadmin.common.config.properties.LocalStorageProperties;
|
import top.charles7c.cnadmin.common.config.properties.LocalStorageProperties;
|
||||||
|
import top.charles7c.cnadmin.common.constant.CacheConsts;
|
||||||
import top.charles7c.cnadmin.common.constant.FileConsts;
|
import top.charles7c.cnadmin.common.constant.FileConsts;
|
||||||
import top.charles7c.cnadmin.common.constant.StringConsts;
|
import top.charles7c.cnadmin.common.constant.StringConsts;
|
||||||
import top.charles7c.cnadmin.common.constant.SysConsts;
|
import top.charles7c.cnadmin.common.constant.SysConsts;
|
||||||
@ -70,6 +73,7 @@ import top.charles7c.cnadmin.system.service.UserService;
|
|||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
|
@CacheConfig(cacheNames = CacheConsts.USER_KEY_PREFIX)
|
||||||
public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserVO, UserDetailVO, UserQuery, UserRequest>
|
public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserVO, UserDetailVO, UserQuery, UserRequest>
|
||||||
implements UserService, CommonUserService {
|
implements UserService, CommonUserService {
|
||||||
|
|
||||||
@ -247,6 +251,7 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserVO,
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@Cacheable(key = "#id")
|
||||||
public String getNicknameById(Long id) {
|
public String getNicknameById(Long id) {
|
||||||
return baseMapper.selectNicknameById(id);
|
return baseMapper.selectNicknameById(id);
|
||||||
}
|
}
|
||||||
|
@ -190,6 +190,14 @@ spring:
|
|||||||
deserialization:
|
deserialization:
|
||||||
# 允许反序列化不存在的属性
|
# 允许反序列化不存在的属性
|
||||||
FAIL_ON_UNKNOWN_PROPERTIES: false
|
FAIL_ON_UNKNOWN_PROPERTIES: false
|
||||||
|
## 缓存配置(@Cacheable)
|
||||||
|
cache:
|
||||||
|
type: redis
|
||||||
|
redis:
|
||||||
|
# 缓存过期时长(单位:毫秒,默认 -1,表示永不过期)
|
||||||
|
time-to-live: 7200000
|
||||||
|
# 是否允许缓存空值(默认 true,表示允许,可以解决缓存穿透问题)
|
||||||
|
cache-null-values: true
|
||||||
|
|
||||||
--- ### 健康检查配置
|
--- ### 健康检查配置
|
||||||
management.health:
|
management.health:
|
||||||
|
Loading…
Reference in New Issue
Block a user