用户查询
# 用户信息查询
根据用户名查询用户信息:
@GetMapping("/api/shortlink/v1/user/{username}")
public Result<UserRespDTO> getUserByUsername(@PathVariable("username") String username) {
return Results.success(userService.getUserByUsername(username));
}
1
2
3
4
2
3
4
@Override
public UserRespDTO getUserByUsername(String username) {
LambdaQueryWrapper<UserDO> queryWrapper = Wrappers.lambdaQuery(UserDO.class)
.eq(UserDO::getUsername, username);
UserDO userDO = baseMapper.selectOne(queryWrapper);
if(userDO == null){
throw new ClientException(UserErrorCodeEnum.USER_NULL);
}
UserRespDTO result = new UserRespDTO();
BeanUtils.copyProperties(userDO,result);
return result;
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
根据用户名查询未脱敏用户信息:
只需要重新构建一个不加序列化器的RespDTO,其它逻辑一样;
# 判断用户存在
布隆过滤器
引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
</dependency>
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
配置文件配置Redis;
配置类:
@Configuration
public class RBloomFilterConfiguration {
/**
* 防止用户注册查询数据库的布隆过滤器
*/
@Bean
public RBloomFilter<String> userRegisterCachePenetrationBloomFilter(RedissonClient redissonClient) {
RBloomFilter<String> cachePenetrationBloomFilter = redissonClient.getBloomFilter("userRegisterCachePenetrationBloomFilter");
cachePenetrationBloomFilter.tryInit(100000000L, 0.001);
return cachePenetrationBloomFilter;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
接口:
@GetMapping("/api/short-link/admin/v1/user/has-username")
public Result<Boolean> hasUsername(@RequestParam("username") String username) {
return Results.success(userService.hasUsername(username));
}
1
2
3
4
2
3
4
private final RBloomFilter<String> userRegisterCachePenetrationBloomFilter;
// ...
@Override
public Boolean hasUsername(String username) {
return !userRegisterCachePenetrationBloomFilter.contains(username);
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8