Skip to content

Commit

Permalink
perf: 集成 Spring Cache,优化查询用户昵称性能
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Aug 15, 2023
1 parent 6a35c00 commit b23b00d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.redisson.codec.JsonJacksonCodec;
import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand All @@ -33,6 +34,7 @@
* @since 2022/12/28 23:17
*/
@Slf4j
@EnableCaching
@Configuration
@RequiredArgsConstructor
public class RedissonConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public class CacheConsts {
* 限流键前缀
*/
public static final String LIMIT_KEY_PREFIX = "LIMIT";

/**
* 用户缓存键前缀
*/
public static final String USER_KEY_PREFIX = "USER";
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import lombok.RequiredArgsConstructor;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -38,6 +40,7 @@

import top.charles7c.cnadmin.common.base.BaseServiceImpl;
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.StringConsts;
import top.charles7c.cnadmin.common.constant.SysConsts;
Expand Down Expand Up @@ -70,6 +73,7 @@
*/
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = CacheConsts.USER_KEY_PREFIX)
public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserVO, UserDetailVO, UserQuery, UserRequest>
implements UserService, CommonUserService {

Expand Down Expand Up @@ -247,6 +251,7 @@ public Long countByDeptIds(List<Long> deptIds) {
}

@Override
@Cacheable(key = "#id")
public String getNicknameById(Long id) {
return baseMapper.selectNicknameById(id);
}
Expand Down
8 changes: 8 additions & 0 deletions continew-admin-webapi/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ spring:
deserialization:
# 允许反序列化不存在的属性
FAIL_ON_UNKNOWN_PROPERTIES: false
## 缓存配置(@Cacheable)
cache:
type: redis
redis:
# 缓存过期时长(单位:毫秒,默认 -1,表示永不过期)
time-to-live: 7200000
# 是否允许缓存空值(默认 true,表示允许,可以解决缓存穿透问题)
cache-null-values: true

--- ### 健康检查配置
management.health:
Expand Down

0 comments on commit b23b00d

Please sign in to comment.