From f7331145581e5098c46f8db9dcaaa9a8b396113f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Thu, 17 Aug 2023 09:52:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BB=A3=E7=A0=81=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BookController.java | 28 +++++- .../service/impl/UserServiceImpl.java | 3 +- .../kwan/springbootkwan/utils/DateUtils.java | 1 - .../utils/GoodsRankService.java | 90 ----------------- .../springbootkwan/utils/GoodsRankUtil.java | 96 +++++++++++++++++++ .../utils/OpenAIChatGptUtil.java | 38 +------- .../utils/SensitiveWordUtil.java | 8 ++ .../springbootkwan/utils/ThreadPoolUntil.java | 25 +++++ 8 files changed, 156 insertions(+), 133 deletions(-) delete mode 100644 src/main/java/com/kwan/springbootkwan/utils/GoodsRankService.java create mode 100644 src/main/java/com/kwan/springbootkwan/utils/GoodsRankUtil.java create mode 100644 src/main/java/com/kwan/springbootkwan/utils/ThreadPoolUntil.java diff --git a/src/main/java/com/kwan/springbootkwan/controller/BookController.java b/src/main/java/com/kwan/springbootkwan/controller/BookController.java index 89e0298..35ad17c 100644 --- a/src/main/java/com/kwan/springbootkwan/controller/BookController.java +++ b/src/main/java/com/kwan/springbootkwan/controller/BookController.java @@ -1,8 +1,14 @@ package com.kwan.springbootkwan.controller; +import com.kwan.springbootkwan.utils.ThreadPoolUntil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.context.request.async.DeferredResult; /** * 方法上跨域配置 @@ -11,10 +17,26 @@ import org.springframework.web.bind.annotation.*; * @version : 2.2.0 * @date : 2022/12/19 16:10 */ -@Api(description = "书籍信息", tags = "BookController") +@Api(tags = "BookController") @RestController public class BookController { + + @ApiOperation(value = "异步添加书籍", notes = "异步添加书籍") + @CrossOrigin(maxAge = 1800, allowedHeaders = "*") + @PostMapping("addBookAsyn") + public DeferredResult addBookAsyn(String name) { + DeferredResult deferredResult = new DeferredResult<>(10000L); + // 设置超时处理 + deferredResult.onTimeout(() -> deferredResult.setErrorResult("请求超时")); + // 设置错误处理 + deferredResult.onError((Throwable t) -> deferredResult.setErrorResult("系统错误")); + // 创建任务 + Runnable task = () -> deferredResult.setResult("receive:" + name); + ThreadPoolUntil.executeTask(task); + return deferredResult; + } + @ApiOperation(value = "添加书籍", notes = "添加书籍") @CrossOrigin(value = "http://localhost:8081", maxAge = 1800, allowedHeaders = "*") @PostMapping("addBook") @@ -28,4 +50,4 @@ public class BookController { public String deleteBook(@PathVariable(value = "id") String id) { return "receive:" + id; } -} +} \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/service/impl/UserServiceImpl.java b/src/main/java/com/kwan/springbootkwan/service/impl/UserServiceImpl.java index 7cfe747..300666a 100644 --- a/src/main/java/com/kwan/springbootkwan/service/impl/UserServiceImpl.java +++ b/src/main/java/com/kwan/springbootkwan/service/impl/UserServiceImpl.java @@ -7,7 +7,6 @@ import com.kwan.springbootkwan.entity.User; import com.kwan.springbootkwan.mapper.UserMapper; import com.kwan.springbootkwan.service.IPersonService; import com.kwan.springbootkwan.service.IUserService; -import io.seata.spring.annotation.GlobalTransactional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -43,7 +42,7 @@ public class UserServiceImpl extends ServiceImpl implements IU } @DS(value = "kwan-ds") - @GlobalTransactional +// @GlobalTransactional @Override public User saveUser(User user) { userMapper.insert(user); diff --git a/src/main/java/com/kwan/springbootkwan/utils/DateUtils.java b/src/main/java/com/kwan/springbootkwan/utils/DateUtils.java index 7eb9c9f..487068a 100644 --- a/src/main/java/com/kwan/springbootkwan/utils/DateUtils.java +++ b/src/main/java/com/kwan/springbootkwan/utils/DateUtils.java @@ -17,7 +17,6 @@ import java.util.List; * @version : 2.2.0 * @date : 2023/7/21 23:29 */ - @Slf4j public class DateUtils { diff --git a/src/main/java/com/kwan/springbootkwan/utils/GoodsRankService.java b/src/main/java/com/kwan/springbootkwan/utils/GoodsRankService.java deleted file mode 100644 index b96dd35..0000000 --- a/src/main/java/com/kwan/springbootkwan/utils/GoodsRankService.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.kwan.springbootkwan.utils; - -import redis.clients.jedis.Jedis; -import redis.clients.jedis.Tuple; - -import java.util.Set; - -/** - * 商品排序 - * - * @author : qinyingjie - * @version : 2.2.0 - * @date : 2022/12/22 10:01 - */ -public class GoodsRankService { - - private static Jedis jedis; - - static { - jedis = new Jedis("120.79.36.53", 6379); - } - - /** - * 排序的Key - */ - private static String KEY = "goods"; - - public static void removeOne() { - /** - * zrem——移除某个商品 - * 命令格式:***zrem 排行榜名称 商品标识*** 时间复杂度:O(log(N)) - */ - Long aLong = jedis.zrem(KEY, "god-10"); - } - - public static void addScore() { - /** - * zincrby——增减商品分数 - * 命令格式:***zincrby 排行榜名称 分数增量 商品标识*** 时间复杂度:O(log(N)) - * 有的排行榜是在变更时重新设置商品的分数,而还有的排行榜则是以增量方式修改商品分数,增量可正可负。如果执行zincrby时商品尚不在排行榜中,则认为其原始分数为0,相当于执行zdd。 - */ - Double zincrby = jedis.zincrby(KEY, 1, "god-10"); - System.out.println(zincrby); - } - - public static void getNum() { - /** - * zrevrank——查看商品的排名 - * 命令格式:***zrevrank 排行榜名称 商品标识*** 时间复杂度:O(log(N)) - * 与zrevrange类似,zrevrank是以分数由高到低的排序返回商品排名(实际返回的是以0开始的索引),对应的zrank则是以分数由低到高的排序返回排名。 - */ - Long aLong = jedis.zrevrank(KEY, "god-10"); - System.out.println(aLong); - Long aLong2 = jedis.zrevrank(KEY, "god-1000"); - System.out.println(aLong2); - } - - public static void getRange() { - /** - * zrevrange——按名次查看排行榜 - * 命令格式:***zrevrange 排行榜名称 起始位置 结束位置 [withscores]*** 时间复杂度:O(log(N)+M) - * 由于排行榜一般是按照分数由高到低排序的,所以我们使用zrevrange,而命令zrange是按照分数由低到高排序。 - * 起始位置和结束位置都是以0开始的索引,且都包含在内。如果结束位置为-1则查看范围为整个排行榜。 - * 带上withscores则会返回商品分数。 - */ - Set zrevrange = jedis.zrevrange(KEY, 2, 5); - zrevrange.forEach(System.out::println); - Set set = jedis.zrangeWithScores(KEY, 2, 5); - set.forEach(System.out::println); - } - - public static void getData() { - /** - * zscore——查看商品分数 - * 命令格式:***zscore 排行榜名称 商品标识*** 时间复杂度:O(1) - */ - Double zscore = jedis.zscore(KEY, "go-20"); - System.out.println(zscore); - } - - public static void setData() { - for (int i = 0; i < 100; i++) { - /** - * zadd——设置商品分数 - * 命令格式:***zadd 排行榜名称 分数 商品名字 时间复杂度:O(log(N)) - */ - jedis.zadd(KEY, i, "go-" + i); - } - } -} \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/utils/GoodsRankUtil.java b/src/main/java/com/kwan/springbootkwan/utils/GoodsRankUtil.java new file mode 100644 index 0000000..a3cefa0 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/utils/GoodsRankUtil.java @@ -0,0 +1,96 @@ +package com.kwan.springbootkwan.utils; + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.Tuple; + +import java.util.Set; + +/** + * redis商品排序工具类 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/12/22 10:01 + */ +public class GoodsRankUtil { + + private static Jedis jedis; + + static { + jedis = new Jedis("120.79.36.53", 6379); + } + + /** + * 排序的Key + */ + private static String KEY = "goods"; + + /** + * zrem——移除某个商品 + * 命令格式:***zrem 排行榜名称 商品标识*** 时间复杂度:O(log(N)) + */ + public static void removeOne() { + + Long aLong = jedis.zrem(KEY, "god-10"); + } + + /** + * zincrby——增减商品分数 + * 命令格式:***zincrby 排行榜名称 分数增量 商品标识*** 时间复杂度:O(log(N)) + * 有的排行榜是在变更时重新设置商品的分数,而还有的排行榜则是以增量方式修改商品分数,增量可正可负。如果执行zincrby时商品尚不在排行榜中,则认为其原始分数为0,相当于执行zdd。 + */ + public static void addScore() { + + Double zincrby = jedis.zincrby(KEY, 1, "god-10"); + System.out.println(zincrby); + } + + /** + * zrevrank——查看商品的排名 + * 命令格式:***zrevrank 排行榜名称 商品标识*** 时间复杂度:O(log(N)) + * 与zrevrange类似,zrevrank是以分数由高到低的排序返回商品排名(实际返回的是以0开始的索引),对应的zrank则是以分数由低到高的排序返回排名。 + */ + public static void getNum() { + + Long aLong = jedis.zrevrank(KEY, "god-10"); + System.out.println(aLong); + Long aLong2 = jedis.zrevrank(KEY, "god-1000"); + System.out.println(aLong2); + } + + /** + * zrevrange——按名次查看排行榜 + * 命令格式:***zrevrange 排行榜名称 起始位置 结束位置 [withscores]*** 时间复杂度:O(log(N)+M) + * 由于排行榜一般是按照分数由高到低排序的,所以我们使用zrevrange,而命令zrange是按照分数由低到高排序。 + * 起始位置和结束位置都是以0开始的索引,且都包含在内。如果结束位置为-1则查看范围为整个排行榜。 + * 带上withscores则会返回商品分数。 + */ + public static void getRange() { + + Set zrevrange = jedis.zrevrange(KEY, 2, 5); + zrevrange.forEach(System.out::println); + Set set = jedis.zrangeWithScores(KEY, 2, 5); + set.forEach(System.out::println); + } + + /** + * zscore——查看商品分数 + * 命令格式:***zscore 排行榜名称 商品标识*** 时间复杂度:O(1) + */ + public static void getData() { + + Double zscore = jedis.zscore(KEY, "go-20"); + System.out.println(zscore); + } + + /** + * zadd——设置商品分数 + * 命令格式:***zadd 排行榜名称 分数 商品名字 时间复杂度:O(log(N)) + */ + public static void setData() { + for (int i = 0; i < 100; i++) { + + jedis.zadd(KEY, i, "go-" + i); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/utils/OpenAIChatGptUtil.java b/src/main/java/com/kwan/springbootkwan/utils/OpenAIChatGptUtil.java index ce33195..3733db3 100644 --- a/src/main/java/com/kwan/springbootkwan/utils/OpenAIChatGptUtil.java +++ b/src/main/java/com/kwan/springbootkwan/utils/OpenAIChatGptUtil.java @@ -14,7 +14,7 @@ import java.util.List; import java.util.Map; /** - * 人工智能chatgpt-api + * chatgpt-api * * @author : qinyingjie * @version : 2.2.0 @@ -34,7 +34,6 @@ public class OpenAIChatGptUtil { */ final static private String Authorization = "Bearer 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"; - public static void main(String[] args) { String question = "默写静夜思"; //这里是我根据模型请求封装的模型实体 @@ -61,39 +60,4 @@ public class OpenAIChatGptUtil { } System.out.println("成功"); } - -// public static CompletionChatResponse chatCompletion(String prompt) {//prompt即要将要发送至gtp的内容 -// //这里我把用户发送和机器人返回的20条内容内容存在了redis中,用于关联对话上下文场景,如果不需要,注释掉即可,直接new一个ArrayList -// List messages = getUserCacheMessages(); -// int size = messages.size(); -// -// if (size > 19) { -// messages.remove(0); -// } -// -// messages.add(message); -// setUserCacheMessages(messages);//把新的放到redis -// completionChatRequest.setMessages(messages); -// post.body(JSON.toJSONString(completionChatRequest)); -// String body = post.execute().body(); -// CompletionChatResponse completionChatResponse = JSON.parseObject(body, CompletionChatResponse.class); -// return completionChatResponse; -// } -// -// public static List getUserCacheMessages(){ -// RedisCache redisCache = SpringUtils.getBean(RedisCache.class); -// List cacheMessages = redisCache.getCacheObject(JwtTokenUtil.getUserId + ":messages"); -// if (StringUtils.isEmpty(cacheMessages)){ -// cacheMessages= new ArrayList<>(); -// } -// return cacheMessages; -// } -// -// public static void setUserCacheMessages(List messages){ -// while(messages.size()>20){ -// messages.remove(0); -// } -// RedisCache redisCache = SpringUtils.getBean(RedisCache.class); -// redisCache.setCacheObject(JwtTokenUtil.getUserId + ":messages", messages, 30, TimeUnit.MINUTES); -// } } diff --git a/src/main/java/com/kwan/springbootkwan/utils/SensitiveWordUtil.java b/src/main/java/com/kwan/springbootkwan/utils/SensitiveWordUtil.java index 7bea1de..62a6edd 100644 --- a/src/main/java/com/kwan/springbootkwan/utils/SensitiveWordUtil.java +++ b/src/main/java/com/kwan/springbootkwan/utils/SensitiveWordUtil.java @@ -6,6 +6,14 @@ import org.springframework.stereotype.Component; import java.util.List; + +/** + * 敏感词过滤工具类 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/8/17 09:41 + */ @Component public class SensitiveWordUtil { diff --git a/src/main/java/com/kwan/springbootkwan/utils/ThreadPoolUntil.java b/src/main/java/com/kwan/springbootkwan/utils/ThreadPoolUntil.java new file mode 100644 index 0000000..25f9f64 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/utils/ThreadPoolUntil.java @@ -0,0 +1,25 @@ +package com.kwan.springbootkwan.utils; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + + +/** + * 线程池工具类 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/8/17 09:38 + */ +public class ThreadPoolUntil { + private static final int THREAD_POOL_SIZE = 10; + private static ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE); + + public static void executeTask(Runnable task) { + executorService.submit(task); + } + + public static void shutdown() { + executorService.shutdown(); + } +} \ No newline at end of file -- GitLab