-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6be0135
commit 4bb2aa4
Showing
14 changed files
with
91 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/* | ||
* Copyright (c) 2020. [email protected] All Rights Reserved. | ||
* 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 | ||
* 类名称:ZhRedisToDbApplication.java | ||
* 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 | ||
* 类名称:ZhRedisBatchApplication.java | ||
* 创建人:张晗 | ||
* 联系方式:[email protected] | ||
* 开源地址: 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); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/config/RedisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* | ||
* Copyright (c) 2020. [email protected] All Rights Reserved. | ||
* 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 | ||
* 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 | ||
* 类名称:RedisConfig.java | ||
* 创建人:张晗 | ||
* 联系方式:[email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/* | ||
* Copyright (c) 2020. [email protected] All Rights Reserved. | ||
* 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 | ||
* 类名称:ArticleCountController.java | ||
* 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 | ||
* 类名称:BatchRedisController.java | ||
* 创建人:张晗 | ||
* 联系方式:[email protected] | ||
* 开源地址: https://github.com/dangnianchuntian/springboot | ||
|
@@ -11,41 +11,52 @@ | |
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; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
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 { | ||
|
||
@Autowired | ||
private BatchRedisService batchRedisService; | ||
|
||
/** | ||
* 记录用户访问记录 | ||
* Redis批量Set | ||
*/ | ||
@RequestMapping(value = "/post/multi/set", method = RequestMethod.POST) | ||
public Object postMultiSet(@RequestBody @Validated PostMultiSetRequest postMultiSetRequest) { | ||
return batchRedisService.postMultiSet(postMultiSetRequest); | ||
} | ||
|
||
/** | ||
* 批量将缓存中的数据同步到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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/* | ||
* Copyright (c) 2020. [email protected] All Rights Reserved. | ||
* 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 | ||
* 类名称:PostArticleViewsRequest.java | ||
* 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 | ||
* 类名称:ListMultiGetRequest.java | ||
* 创建人:张晗 | ||
* 联系方式:[email protected] | ||
* 开源地址: 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 | ||
|
27 changes: 27 additions & 0 deletions
27
...ch/src/main/java/com/zhanghan/zhredisbatch/controller/request/PostMultiDeleteRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (c) 2020. [email protected] All Rights Reserved. | ||
* 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 | ||
* 类名称:PostMultiDeleteRequest.java | ||
* 创建人:张晗 | ||
* 联系方式:[email protected] | ||
* 开源地址: 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<String> keyList; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/* | ||
* Copyright (c) 2020. [email protected] All Rights Reserved. | ||
* 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 | ||
* 类名称:PostArticleViewsRequest.java | ||
* 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 | ||
* 类名称:PostMultiSetRequest.java | ||
* 创建人:张晗 | ||
* 联系方式:[email protected] | ||
* 开源地址: 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 | ||
|
4 changes: 2 additions & 2 deletions
4
...tch/src/main/java/com/zhanghan/zhredisbatch/controller/response/ListMultiGetResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/* | ||
* Copyright (c) 2020. [email protected] All Rights Reserved. | ||
* 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 | ||
* 类名称:PostArticleViewsRequest.java | ||
* 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 | ||
* 类名称:ListMultiGetResponse.java | ||
* 创建人:张晗 | ||
* 联系方式:[email protected] | ||
* 开源地址: https://github.com/dangnianchuntian/springboot | ||
|
4 changes: 2 additions & 2 deletions
4
zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/dto/BatchRedisDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/* | ||
* Copyright (c) 2020. [email protected] All Rights Reserved. | ||
* 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 | ||
* 类名称:PostArticleViewsRequest.java | ||
* 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 | ||
* 类名称:BatchRedisDto.java | ||
* 创建人:张晗 | ||
* 联系方式:[email protected] | ||
* 开源地址: https://github.com/dangnianchuntian/springboot | ||
|
2 changes: 1 addition & 1 deletion
2
zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/filter/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* | ||
* Copyright (c) 2020. [email protected] All Rights Reserved. | ||
* 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 | ||
* 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 | ||
* 类名称:GlobalExceptionHandler.java | ||
* 创建人:张晗 | ||
* 联系方式:[email protected] | ||
|
2 changes: 1 addition & 1 deletion
2
zh-redis-batch/src/main/java/com/zhanghan/zhredisbatch/properties/RedisProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* | ||
* Copyright (c) 2020. [email protected] All Rights Reserved. | ||
* 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 | ||
* 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 | ||
* 类名称:RedisProperties.java | ||
* 创建人:张晗 | ||
* 联系方式:[email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/* | ||
* Copyright (c) 2020. [email protected] All Rights Reserved. | ||
* 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 | ||
* 类名称:ArticleCountService.java | ||
* 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 | ||
* 类名称:BatchRedisService.java | ||
* 创建人:张晗 | ||
* 联系方式:[email protected] | ||
* 开源地址: 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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/* | ||
* Copyright (c) 2020. [email protected] All Rights Reserved. | ||
* 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 | ||
* 类名称:ArticleCountServiceImpl.java | ||
* 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 | ||
* 类名称:BatchRedisServiceImpl.java | ||
* 创建人:张晗 | ||
* 联系方式:[email protected] | ||
* 开源地址: 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<String, String> 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<BatchRedisDto> 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<BatchRedisDto> 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(); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* | ||
* Copyright (c) 2020. [email protected] All Rights Reserved. | ||
* 项目名称:Spring Boot实战解决高并发数据入库: Redis 缓存+MySQL 批量入库 | ||
* 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升 | ||
* 类名称:WrapMapper.java | ||
* 创建人:张晗 | ||
* 联系方式:[email protected] | ||
|
@@ -32,7 +32,6 @@ private WrapMapper() { | |
* @param code the code | ||
* @param message the message | ||
* @param o the o | ||
* | ||
* @return the wrapper | ||
*/ | ||
public static <E> Wrapper<E> wrap(int code, String message, E o) { | ||
|
@@ -45,7 +44,6 @@ public static <E> Wrapper<E> wrap(int code, String message, E o) { | |
* @param <E> the element type | ||
* @param code the code | ||
* @param message the message | ||
* | ||
* @return the wrapper | ||
*/ | ||
public static <E> Wrapper<E> wrap(int code, String message) { | ||
|
@@ -57,7 +55,6 @@ public static <E> Wrapper<E> wrap(int code, String message) { | |
* | ||
* @param <E> the element type | ||
* @param code the code | ||
* | ||
* @return the wrapper | ||
*/ | ||
public static <E> Wrapper<E> wrap(int code) { | ||
|
@@ -69,7 +66,6 @@ public static <E> Wrapper<E> wrap(int code) { | |
* | ||
* @param <E> the element type | ||
* @param e the e | ||
* | ||
* @return the wrapper | ||
*/ | ||
public static <E> Wrapper<E> wrap(Exception e) { | ||
|
@@ -81,7 +77,6 @@ public static <E> Wrapper<E> wrap(Exception e) { | |
* | ||
* @param <E> the element type | ||
* @param wrapper the wrapper | ||
* | ||
* @return the e | ||
*/ | ||
public static <E> E unWrap(Wrapper<E> wrapper) { | ||
|
@@ -92,7 +87,6 @@ public static <E> E unWrap(Wrapper<E> wrapper) { | |
* Wrap ERROR. code=1 | ||
* | ||
* @param <E> the element type | ||
* | ||
* @return the wrapper | ||
*/ | ||
public static <E> Wrapper<E> error() { | ||
|
@@ -104,7 +98,6 @@ public static <E> Wrapper<E> error() { | |
* | ||
* @param <E> the type parameter | ||
* @param message the message | ||
* | ||
* @return the wrapper | ||
*/ | ||
public static <E> Wrapper<E> error(String message) { | ||
|
@@ -115,7 +108,6 @@ public static <E> Wrapper<E> error(String message) { | |
* Wrap SUCCESS. code=0 | ||
* | ||
* @param <E> the element type | ||
* | ||
* @return the wrapper | ||
*/ | ||
public static <E> Wrapper<E> ok() { | ||
|
@@ -127,7 +119,6 @@ public static <E> Wrapper<E> ok() { | |
* | ||
* @param <E> the type parameter | ||
* @param o the o | ||
* | ||
* @return the wrapper | ||
*/ | ||
public static <E> Wrapper<E> ok(E o) { | ||
|
Oops, something went wrong.