parent
93d5460eef
commit
3805b8b993
@ -1,4 +1,4 @@
|
||||
package org.opsli.common.annotation.limiter;
|
||||
package org.opsli.common.annotation;
|
||||
|
||||
|
||||
import org.opsli.common.enums.AlertType;
|
@ -0,0 +1,81 @@
|
||||
/**
|
||||
* Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of
|
||||
* the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
package org.opsli.core.aspect;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.opsli.common.annotation.SearchHis;
|
||||
import org.opsli.core.utils.SearchHisUtil;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import static org.opsli.common.constants.OrderConstants.SEARCH_HIS_AOP_SORT;
|
||||
|
||||
/**
|
||||
* 搜索历史 AOP
|
||||
*
|
||||
* @author 周鹏程
|
||||
* @date 2020-09-16
|
||||
*/
|
||||
@Slf4j
|
||||
@Order(SEARCH_HIS_AOP_SORT)
|
||||
@Aspect
|
||||
@Component
|
||||
public class SearchHisAop {
|
||||
|
||||
|
||||
@Pointcut("@annotation(org.opsli.common.annotation.SearchHis)")
|
||||
public void requestMapping() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 限流
|
||||
* @param point
|
||||
*/
|
||||
@Before("requestMapping()")
|
||||
public void limiterHandle(JoinPoint point){
|
||||
try {
|
||||
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
|
||||
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
|
||||
MethodSignature signature = (MethodSignature) point.getSignature();
|
||||
Method method = signature.getMethod();
|
||||
if(sra != null) {
|
||||
HttpServletRequest request = sra.getRequest();
|
||||
SearchHis searchHis = method.getAnnotation(SearchHis.class);
|
||||
if(searchHis != null){
|
||||
String[] keys = searchHis.keys();
|
||||
|
||||
// 存入缓存
|
||||
SearchHisUtil.putSearchHis(request, Convert.toList(String.class, keys));
|
||||
}
|
||||
}
|
||||
}catch (Exception e){
|
||||
log.error(e.getMessage(),e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,129 @@
|
||||
/**
|
||||
* Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of
|
||||
* the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
package org.opsli.core.utils;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.opsli.api.wrapper.system.user.UserModel;
|
||||
import org.opsli.common.constants.CacheConstants;
|
||||
import org.opsli.common.utils.Props;
|
||||
import org.opsli.plugins.redis.RedisLockPlugins;
|
||||
import org.opsli.plugins.redis.RedisPlugin;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.opsli.common.constants.OrderConstants.UTIL_ORDER;
|
||||
|
||||
/**
|
||||
* @Author: 周鹏程
|
||||
* @CreateTime: 2020-09-22 11:17
|
||||
* @Description: 搜索历史工具类
|
||||
*/
|
||||
@Slf4j
|
||||
@Order(UTIL_ORDER)
|
||||
@Component
|
||||
@Lazy(false)
|
||||
@AutoConfigureAfter({RedisPlugin.class , RedisLockPlugins.class})
|
||||
public class SearchHisUtil {
|
||||
|
||||
|
||||
/** 搜索历史缓存数据KEY */
|
||||
private static final int DEFAULT_COUNT = 10;
|
||||
|
||||
/**
|
||||
* 热点数据前缀
|
||||
*/
|
||||
public static final String PREFIX_NAME;
|
||||
private static final String CACHE_PREFIX = "his:username:";
|
||||
|
||||
/** Redis插件 */
|
||||
private static RedisPlugin redisPlugin;
|
||||
|
||||
static {
|
||||
Props props = new Props("application.yaml");
|
||||
PREFIX_NAME = props.getStr("spring.cache-conf.prefix", CacheConstants.PREFIX_NAME) + ":";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获得搜索历史记录
|
||||
* @param key 类型
|
||||
* @param count 获取数量
|
||||
*/
|
||||
public static Set<Object> getSearchHis(HttpServletRequest request, String key, Integer count) {
|
||||
if(request == null || StringUtils.isEmpty(key)){
|
||||
return null;
|
||||
}
|
||||
|
||||
if(count == null){
|
||||
count = DEFAULT_COUNT;
|
||||
}
|
||||
|
||||
// 获得当前用户
|
||||
UserModel user = UserUtil.getUser();
|
||||
|
||||
String cacheKey = PREFIX_NAME + CACHE_PREFIX + user.getUsername() + ":" + key;
|
||||
|
||||
return redisPlugin.zReverseRange(cacheKey, 0, count - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 存放搜索历史记录
|
||||
* @param request
|
||||
* @param keys 搜索key
|
||||
*/
|
||||
public static void putSearchHis(HttpServletRequest request, List<String> keys) {
|
||||
if(request == null || CollUtil.isEmpty(keys)){
|
||||
return;
|
||||
}
|
||||
|
||||
// 获得当前用户
|
||||
UserModel user = UserUtil.getUser();
|
||||
|
||||
Map<String, String[]> parameterMap = request.getParameterMap();
|
||||
for (String key : keys) {
|
||||
String[] values = parameterMap.get(key);
|
||||
if(values == null || values.length == 0){
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
String cacheKey = PREFIX_NAME + CACHE_PREFIX + user.getUsername() + ":" + key;
|
||||
String val = values[0];
|
||||
|
||||
// 记录
|
||||
redisPlugin.zIncrementScore(cacheKey, val, 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ===================================
|
||||
|
||||
@Autowired
|
||||
public void setRedisPlugin(RedisPlugin redisPlugin) {
|
||||
SearchHisUtil.redisPlugin = redisPlugin;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
/**
|
||||
* Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of
|
||||
* the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
package org.opsli.modulars.tools.searchhis.web;
|
||||
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.opsli.api.base.result.ResultVo;
|
||||
import org.opsli.common.annotation.ApiRestController;
|
||||
import org.opsli.common.annotation.Limiter;
|
||||
import org.opsli.common.annotation.SearchHis;
|
||||
import org.opsli.core.utils.SearchHisUtil;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 搜索历史记录
|
||||
*
|
||||
* @author parker
|
||||
* @date 2020-05-23 13:30
|
||||
**
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@ApiRestController("/searchhis")
|
||||
public class SearchHisRestController {
|
||||
|
||||
/**
|
||||
* 获得搜索历史记录
|
||||
*/
|
||||
@Limiter
|
||||
@ApiOperation(value = "获得搜索历史记录", notes = "获得搜索历史记录")
|
||||
@PostMapping("/getSearchHis")
|
||||
public ResultVo<?> getSearchHis(String key, Integer count, HttpServletRequest request){
|
||||
|
||||
Set<Object> searchHis = SearchHisUtil.getSearchHis(request, key, count);
|
||||
|
||||
return ResultVo.success(searchHis);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得搜索历史记录
|
||||
*/
|
||||
@Limiter
|
||||
@SearchHis(keys = {"test"})
|
||||
@ApiOperation(value = "测试存入搜索历史记录", notes = "测试存入搜索历史记录")
|
||||
@PostMapping("/testPutSearchHis")
|
||||
public void getSearchHis(String test, HttpServletRequest request){
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue