diff --git a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/ZhRedisBatchApplication.java b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/ZhRedisBatchApplication.java index f4870be..fec89e0 100644 --- a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/ZhRedisBatchApplication.java +++ b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/ZhRedisBatchApplication.java @@ -1,7 +1,7 @@ /* * Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved. - * 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 - * 类名称:ZhRedisToDbApplication.java + * 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 + * 类名称:ZhRedisBatchApplication.java * 创建人:张晗 * 联系方式:zhanghan_java@163.com * 开源地址: https://github.com/dangnianchuntian/springboot @@ -16,8 +16,8 @@ @SpringBootApplication public class ZhRedisBatchApplication { - public static void main(String[] args) { - SpringApplication.run(ZhRedisBatchApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(ZhRedisBatchApplication.class, args); + } } diff --git a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/config/RedisConfig.java b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/config/RedisConfig.java index 892ea64..7c41c46 100644 --- a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/config/RedisConfig.java +++ b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/config/RedisConfig.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved. - * 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 + * 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 * 类名称:RedisConfig.java * 创建人:张晗 * 联系方式:zhanghan_java@163.com diff --git a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/controller/BatchRedisController.java b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/controller/BatchRedisController.java index d9e803f..837c439 100644 --- a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/controller/BatchRedisController.java +++ b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/controller/BatchRedisController.java @@ -1,7 +1,7 @@ /* * Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved. - * 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 - * 类名称:ArticleCountController.java + * 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 + * 类名称:BatchRedisController.java * 创建人:张晗 * 联系方式:zhanghan_java@163.com * 开源地址: https://github.com/dangnianchuntian/springboot @@ -11,6 +11,9 @@ package com.zhanghan.zhredisbatch.controller; import com.zhanghan.zhredisbatch.controller.request.ListMultiGetRequest; +import com.zhanghan.zhredisbatch.controller.request.PostMultiDeleteRequest; +import com.zhanghan.zhredisbatch.controller.request.PostMultiSetRequest; +import com.zhanghan.zhredisbatch.service.BatchRedisService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestBody; @@ -18,9 +21,6 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; -import com.zhanghan.zhredisbatch.controller.request.PostMultiSetRequest; -import com.zhanghan.zhredisbatch.service.BatchRedisService; - @RestController public class BatchRedisController { @@ -28,7 +28,7 @@ public class BatchRedisController { private BatchRedisService batchRedisService; /** - * 记录用户访问记录 + * Redis批量Set */ @RequestMapping(value = "/post/multi/set", method = RequestMethod.POST) public Object postMultiSet(@RequestBody @Validated PostMultiSetRequest postMultiSetRequest) { @@ -36,16 +36,27 @@ public Object postMultiSet(@RequestBody @Validated PostMultiSetRequest postMulti } /** - * 批量将缓存中的数据同步到MySQL(模拟定时任务操作) + * Redis批量Get */ @RequestMapping(value = "/list/multi/get", method = RequestMethod.POST) public Object listMultiGet(@RequestBody @Validated ListMultiGetRequest listMultiGetRequest) { return batchRedisService.listMultiGet(listMultiGetRequest); } + /** + * Redis批量Set且设置失效时间 + */ @RequestMapping(value = "/post/multi/set/expire", method = RequestMethod.POST) public Object postMultiSetAndExpire(@RequestBody @Validated PostMultiSetRequest postMultiSetRequest) { return batchRedisService.postMultiSetAndExpire(postMultiSetRequest); } + /** + * Redis批量Delete + */ + @RequestMapping(value = "/post/multi/delete", method = RequestMethod.POST) + public Object postMultiDelete(@RequestBody @Validated PostMultiDeleteRequest postMultiDeleteRequest) { + return batchRedisService.postMultiDelete(postMultiDeleteRequest); + } + } diff --git a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/controller/request/ListMultiGetRequest.java b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/controller/request/ListMultiGetRequest.java index b17fa0a..11a6371 100644 --- a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/controller/request/ListMultiGetRequest.java +++ b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/controller/request/ListMultiGetRequest.java @@ -1,7 +1,7 @@ /* * Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved. - * 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 - * 类名称:PostArticleViewsRequest.java + * 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 + * 类名称:ListMultiGetRequest.java * 创建人:张晗 * 联系方式:zhanghan_java@163.com * 开源地址: https://github.com/dangnianchuntian/springboot @@ -13,10 +13,12 @@ import com.sun.istack.internal.NotNull; import lombok.Data; +import lombok.NoArgsConstructor; import java.util.List; @Data +@NoArgsConstructor public class ListMultiGetRequest { @NotNull diff --git a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/controller/request/PostMultiDeleteRequest.java b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/controller/request/PostMultiDeleteRequest.java new file mode 100644 index 0000000..4f5af4a --- /dev/null +++ b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/controller/request/PostMultiDeleteRequest.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved. + * 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 + * 类名称:PostMultiDeleteRequest.java + * 创建人:张晗 + * 联系方式:zhanghan_java@163.com + * 开源地址: https://github.com/dangnianchuntian/springboot + * 博客地址: https://zhanghan.blog.csdn.net + */ + +package com.zhanghan.zhredisbatch.controller.request; + + +import com.sun.istack.internal.NotNull; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@NoArgsConstructor +public class PostMultiDeleteRequest { + + @NotNull + private List keyList; + +} diff --git a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/controller/request/PostMultiSetRequest.java b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/controller/request/PostMultiSetRequest.java index efaf7a2..d1560b8 100644 --- a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/controller/request/PostMultiSetRequest.java +++ b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/controller/request/PostMultiSetRequest.java @@ -1,7 +1,7 @@ /* * Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved. - * 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 - * 类名称:PostArticleViewsRequest.java + * 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 + * 类名称:PostMultiSetRequest.java * 创建人:张晗 * 联系方式:zhanghan_java@163.com * 开源地址: https://github.com/dangnianchuntian/springboot @@ -14,10 +14,12 @@ import com.sun.istack.internal.NotNull; import com.zhanghan.zhredisbatch.dto.BatchRedisDto; import lombok.Data; +import lombok.NoArgsConstructor; import java.util.List; @Data +@NoArgsConstructor public class PostMultiSetRequest { @NotNull diff --git a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/controller/response/ListMultiGetResponse.java b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/controller/response/ListMultiGetResponse.java index df49d64..91c93b4 100644 --- a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/controller/response/ListMultiGetResponse.java +++ b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/controller/response/ListMultiGetResponse.java @@ -1,7 +1,7 @@ /* * Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved. - * 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 - * 类名称:PostArticleViewsRequest.java + * 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 + * 类名称:ListMultiGetResponse.java * 创建人:张晗 * 联系方式:zhanghan_java@163.com * 开源地址: https://github.com/dangnianchuntian/springboot diff --git a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/dto/BatchRedisDto.java b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/dto/BatchRedisDto.java index a16e037..09c0d2f 100644 --- a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/dto/BatchRedisDto.java +++ b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/dto/BatchRedisDto.java @@ -1,7 +1,7 @@ /* * Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved. - * 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 - * 类名称:PostArticleViewsRequest.java + * 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 + * 类名称:BatchRedisDto.java * 创建人:张晗 * 联系方式:zhanghan_java@163.com * 开源地址: https://github.com/dangnianchuntian/springboot diff --git a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/filter/GlobalExceptionHandler.java b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/filter/GlobalExceptionHandler.java index 74e6982..9b938ee 100644 --- a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/filter/GlobalExceptionHandler.java +++ b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/filter/GlobalExceptionHandler.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved. - * 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 + * 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 * 类名称:GlobalExceptionHandler.java * 创建人:张晗 * 联系方式:zhanghan_java@163.com diff --git a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/properties/RedisProperties.java b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/properties/RedisProperties.java index 195bce4..d9c7382 100644 --- a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/properties/RedisProperties.java +++ b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/properties/RedisProperties.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved. - * 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 + * 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 * 类名称:RedisProperties.java * 创建人:张晗 * 联系方式:zhanghan_java@163.com diff --git a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/service/BatchRedisService.java b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/service/BatchRedisService.java index 19c38a0..eff0dff 100644 --- a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/service/BatchRedisService.java +++ b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/service/BatchRedisService.java @@ -1,7 +1,7 @@ /* * Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved. - * 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 - * 类名称:ArticleCountService.java + * 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 + * 类名称:BatchRedisService.java * 创建人:张晗 * 联系方式:zhanghan_java@163.com * 开源地址: https://github.com/dangnianchuntian/springboot @@ -12,6 +12,7 @@ import com.zhanghan.zhredisbatch.controller.request.ListMultiGetRequest; +import com.zhanghan.zhredisbatch.controller.request.PostMultiDeleteRequest; import com.zhanghan.zhredisbatch.controller.request.PostMultiSetRequest; public interface BatchRedisService { @@ -23,4 +24,6 @@ public interface BatchRedisService { Object postMultiSetAndExpire(PostMultiSetRequest postMultiSetRequest); + Object postMultiDelete(PostMultiDeleteRequest postMultiDeleteRequest); + } diff --git a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/service/impl/BatchRedisServiceImpl.java b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/service/impl/BatchRedisServiceImpl.java index 0ee9f93..e2d24ec 100644 --- a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/service/impl/BatchRedisServiceImpl.java +++ b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/service/impl/BatchRedisServiceImpl.java @@ -1,7 +1,7 @@ /* * Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved. - * 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 - * 类名称:ArticleCountServiceImpl.java + * 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 + * 类名称:BatchRedisServiceImpl.java * 创建人:张晗 * 联系方式:zhanghan_java@163.com * 开源地址: https://github.com/dangnianchuntian/springboot @@ -11,6 +11,7 @@ package com.zhanghan.zhredisbatch.service.impl; import com.zhanghan.zhredisbatch.controller.request.ListMultiGetRequest; +import com.zhanghan.zhredisbatch.controller.request.PostMultiDeleteRequest; import com.zhanghan.zhredisbatch.controller.request.PostMultiSetRequest; import com.zhanghan.zhredisbatch.controller.response.ListMultiGetResponse; import com.zhanghan.zhredisbatch.dto.BatchRedisDto; @@ -43,7 +44,7 @@ public class BatchRedisServiceImpl implements BatchRedisService { private RedisTemplate strRedisTemplate; /** - * 记录用户访问记录 + * Redis批量Set */ @Override public Object postMultiSet(PostMultiSetRequest postMultiSetRequest) { @@ -58,7 +59,7 @@ public Object postMultiSet(PostMultiSetRequest postMultiSetRequest) { } /** - * 批量将缓存中的数据同步到MySQL + * Redis批量Get */ @Override public Object listMultiGet(ListMultiGetRequest listMultiGetRequest) { @@ -67,12 +68,12 @@ public Object listMultiGet(ListMultiGetRequest listMultiGetRequest) { List batchRedisDtoList = new ArrayList<>(); ListMultiGetResponse listMultiGetResponse = new ListMultiGetResponse(); - BatchRedisDto batchRedisDto = new BatchRedisDto(); for (int i = 0; i < valueList.size(); i++) { String value = valueList.get(i); if (StringUtils.isEmpty(value)) { continue; } + BatchRedisDto batchRedisDto = new BatchRedisDto(); batchRedisDto.setRedisKey(keyList.get(i)); batchRedisDto.setRedisValue(value); batchRedisDtoList.add(batchRedisDto); @@ -83,6 +84,9 @@ public Object listMultiGet(ListMultiGetRequest listMultiGetRequest) { return WrapMapper.ok(listMultiGetResponse); } + /** + * Redis批量Set且设置失效时间 + */ @Override public Object postMultiSetAndExpire(PostMultiSetRequest postMultiSetRequest) { List batchRedisDtoList = postMultiSetRequest.getBatchRedisDtoList(); @@ -98,7 +102,15 @@ public String doInRedis(RedisConnection connection) throws DataAccessException { } }); + return WrapMapper.ok(); + } + /** + * Redis批量Delete + */ + @Override + public Object postMultiDelete(PostMultiDeleteRequest postMultiDeleteRequest) { + strRedisTemplate.delete(postMultiDeleteRequest.getKeyList()); return WrapMapper.ok(); } diff --git a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/util/wrapper/WrapMapper.java b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/util/wrapper/WrapMapper.java index 315a929..f825bae 100644 --- a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/util/wrapper/WrapMapper.java +++ b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/util/wrapper/WrapMapper.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved. - * 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 + * 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 * 类名称:WrapMapper.java * 创建人:张晗 * 联系方式:zhanghan_java@163.com @@ -32,7 +32,6 @@ private WrapMapper() { * @param code the code * @param message the message * @param o the o - * * @return the wrapper */ public static Wrapper wrap(int code, String message, E o) { @@ -45,7 +44,6 @@ public static Wrapper wrap(int code, String message, E o) { * @param the element type * @param code the code * @param message the message - * * @return the wrapper */ public static Wrapper wrap(int code, String message) { @@ -57,7 +55,6 @@ public static Wrapper wrap(int code, String message) { * * @param the element type * @param code the code - * * @return the wrapper */ public static Wrapper wrap(int code) { @@ -69,7 +66,6 @@ public static Wrapper wrap(int code) { * * @param the element type * @param e the e - * * @return the wrapper */ public static Wrapper wrap(Exception e) { @@ -81,7 +77,6 @@ public static Wrapper wrap(Exception e) { * * @param the element type * @param wrapper the wrapper - * * @return the e */ public static E unWrap(Wrapper wrapper) { @@ -92,7 +87,6 @@ public static E unWrap(Wrapper wrapper) { * Wrap ERROR. code=1 * * @param the element type - * * @return the wrapper */ public static Wrapper error() { @@ -104,7 +98,6 @@ public static Wrapper error() { * * @param the type parameter * @param message the message - * * @return the wrapper */ public static Wrapper error(String message) { @@ -115,7 +108,6 @@ public static Wrapper error(String message) { * Wrap SUCCESS. code=0 * * @param the element type - * * @return the wrapper */ public static Wrapper ok() { @@ -127,7 +119,6 @@ public static Wrapper ok() { * * @param the type parameter * @param o the o - * * @return the wrapper */ public static Wrapper ok(E o) { diff --git a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/util/wrapper/Wrapper.java b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/util/wrapper/Wrapper.java index 7a6c969..ba0d6e5 100644 --- a/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/util/wrapper/Wrapper.java +++ b/zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/util/wrapper/Wrapper.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved. - * 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 + * 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 * 类名称:Wrapper.java * 创建人:张晗 * 联系方式:zhanghan_java@163.com @@ -10,13 +10,12 @@ package com.zhanghan.zhredisbatch.util.wrapper; -import java.io.Serializable; - import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.databind.annotation.JsonSerialize; - import lombok.Data; +import java.io.Serializable; + /** * The class Wrapper.