mirror of
https://gitee.com/youlaitech/youlai-mall.git
synced 2024-12-22 12:48:59 +08:00
refactor: pms工程结构优化,删除布隆过滤器
This commit is contained in:
parent
c366f29385
commit
8bc5ed4877
@ -1,50 +0,0 @@
|
||||
package com.youlai.mall.pms.component;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.youlai.mall.pms.utils.BloomFilterUtils;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
/**
|
||||
* @Author DaniR
|
||||
* @Description
|
||||
* @Date 2021/6/26 9:38
|
||||
**/
|
||||
public class BloomRedisService {
|
||||
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
private BloomFilterUtils bloomFilterUtils;
|
||||
|
||||
public void setBloomFilterUtils(BloomFilterUtils bloomFilterUtils) {
|
||||
this.bloomFilterUtils = bloomFilterUtils;
|
||||
}
|
||||
|
||||
public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
|
||||
this.redisTemplate = redisTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据给定的布隆过滤器添加值
|
||||
*/
|
||||
public <T> void addByBloomFilter(String key, T value) {
|
||||
Preconditions.checkArgument(bloomFilterUtils != null, "bloomFilterHelper不能为空");
|
||||
int[] offset = bloomFilterUtils.murmurHash(value);
|
||||
for (int i : offset) {
|
||||
redisTemplate.opsForValue().setBit(key, i, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据给定的布隆过滤器判断值是否存在
|
||||
*/
|
||||
public <T> boolean includeByBloomFilter(String key, T value) {
|
||||
Preconditions.checkArgument(bloomFilterUtils != null, "bloomFilterHelper不能为空");
|
||||
int[] offset = bloomFilterUtils.murmurHash(value);
|
||||
for (int i : offset) {
|
||||
if (!redisTemplate.opsForValue().getBit(key, i)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
package com.youlai.mall.pms.config;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.hash.Funnel;
|
||||
import com.youlai.mall.pms.common.constant.PmsConstants;
|
||||
import com.youlai.mall.pms.component.BloomRedisService;
|
||||
import com.youlai.mall.pms.pojo.entity.PmsSpu;
|
||||
import com.youlai.mall.pms.service.IPmsSpuService;
|
||||
import com.youlai.mall.pms.utils.BloomFilterUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author DaniR
|
||||
* @Description
|
||||
* @Date 2021/6/26 9:39
|
||||
**/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@AllArgsConstructor
|
||||
public class BloomFilterConfig implements InitializingBean {
|
||||
|
||||
private final IPmsSpuService pmsSpuServiced;
|
||||
private final RedisTemplate redisTemplate;
|
||||
|
||||
@Bean
|
||||
public BloomFilterUtils<String> initBloomFilterHelper() {
|
||||
return new BloomFilterUtils<>((Funnel<String>) (from, into) -> into.putString(from, Charsets.UTF_8)
|
||||
.putString(from, Charsets.UTF_8), 1000000, 0.01);
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public BloomRedisService bloomRedisService() {
|
||||
BloomRedisService bloomRedisService = new BloomRedisService();
|
||||
bloomRedisService.setBloomFilterUtils(initBloomFilterHelper());
|
||||
bloomRedisService.setRedisTemplate(redisTemplate);
|
||||
return bloomRedisService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
List<PmsSpu> list = pmsSpuServiced.list();
|
||||
log.info("加载产品到布隆过滤器当中,size:{}", list.size());
|
||||
if (!CollectionUtils.isEmpty(list)) {
|
||||
list.stream().filter(item -> item.getId() > 0).forEach(item -> {
|
||||
bloomRedisService().addByBloomFilter(PmsConstants.GOODS_BLOOM_FILTER, item.getId() + "");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package com.youlai.mall.pms.config;
|
||||
|
||||
import com.youlai.mall.pms.interceptor.BloomFilterInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* @Author DaniR
|
||||
* @Description
|
||||
* @Date 2021/6/26 9:39
|
||||
**/
|
||||
@Configuration
|
||||
public class InterceptorConfiguration implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
//注册拦截器
|
||||
registry.addInterceptor(bloomInterceptorHandler())
|
||||
.addPathPatterns("/app-api/v1/goods/**")
|
||||
.excludePathPatterns("/app-api/v1/goods/sku/**");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public BloomFilterInterceptor bloomInterceptorHandler() {
|
||||
return new BloomFilterInterceptor();
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.youlai.mall.pms.common.constant;
|
||||
package com.youlai.mall.pms.constant;
|
||||
|
||||
/**
|
||||
* 商品模块常量
|
@ -1,4 +1,4 @@
|
||||
package com.youlai.mall.pms.common.enums;
|
||||
package com.youlai.mall.pms.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
@ -1,55 +0,0 @@
|
||||
package com.youlai.mall.pms.interceptor;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.youlai.common.result.Result;
|
||||
import com.youlai.mall.pms.common.constant.PmsConstants;
|
||||
import com.youlai.mall.pms.component.BloomRedisService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* @author DaniR
|
||||
* @version 1.0
|
||||
* @description 布隆过滤器拦截器
|
||||
* @createDate 2021/6/23 20:50
|
||||
*/
|
||||
@Slf4j
|
||||
public class BloomFilterInterceptor implements HandlerInterceptor {
|
||||
|
||||
@Autowired
|
||||
private BloomRedisService bloomRedisService;
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
String currentUrl = request.getRequestURI();
|
||||
PathMatcher matcher = new AntPathMatcher();
|
||||
Map<String, String> pathVariable;
|
||||
try {
|
||||
pathVariable = matcher.extractUriTemplateVariables("/app-api/v1/goods/{id}", currentUrl);
|
||||
} catch (IllegalStateException e) {
|
||||
// 路径不匹配则放行
|
||||
return true;
|
||||
}
|
||||
if (bloomRedisService.includeByBloomFilter(PmsConstants.GOODS_BLOOM_FILTER, pathVariable.get("id"))) {
|
||||
return true;
|
||||
}
|
||||
|
||||
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
||||
response.setStatus(HttpStatus.BAD_REQUEST.value());
|
||||
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||
String result = new ObjectMapper().writeValueAsString(Result.failed("商品不存在!"));
|
||||
response.getWriter().print(result);
|
||||
return false;
|
||||
}
|
||||
}
|
@ -7,7 +7,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.youlai.common.web.exception.ApiException;
|
||||
import com.youlai.mall.pms.common.constant.PmsConstants;
|
||||
import com.youlai.mall.pms.constant.PmsConstants;
|
||||
import com.youlai.mall.pms.mapper.PmsSkuMapper;
|
||||
import com.youlai.mall.pms.pojo.dto.CheckPriceDTO;
|
||||
import com.youlai.mall.pms.pojo.dto.SkuInfoDTO;
|
||||
|
@ -10,8 +10,8 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.youlai.common.security.util.SecurityUtils;
|
||||
import com.youlai.mall.pms.common.constant.PmsConstants;
|
||||
import com.youlai.mall.pms.common.enums.AttributeTypeEnum;
|
||||
import com.youlai.mall.pms.constant.PmsConstants;
|
||||
import com.youlai.mall.pms.enums.AttributeTypeEnum;
|
||||
import com.youlai.mall.pms.converter.SpuAttributeConverter;
|
||||
import com.youlai.mall.pms.converter.SpuConverter;
|
||||
import com.youlai.mall.pms.mapper.PmsSpuMapper;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.youlai.mall.pms.utils;
|
||||
package com.youlai.mall.pms.util;
|
||||
|
||||
import com.google.common.hash.Funnel;
|
||||
import com.google.common.hash.Hashing;
|
Loading…
Reference in New Issue
Block a user