Skip to content

Commit

Permalink
format batch redis project
Browse files Browse the repository at this point in the history
  • Loading branch information
dangnianchuntian committed Aug 27, 2020
1 parent 6be0135 commit 4bb2aa4
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 44 deletions.
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
Expand All @@ -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);
}

}
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]
Expand Down
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
Expand All @@ -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);
}

}
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
Expand All @@ -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
Expand Down
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;

}
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
Expand All @@ -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
Expand Down
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
Expand Down
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
Expand Down
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]
Expand Down
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]
Expand Down
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
Expand All @@ -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 {
Expand All @@ -23,4 +24,6 @@ public interface BatchRedisService {

Object postMultiSetAndExpire(PostMultiSetRequest postMultiSetRequest);

Object postMultiDelete(PostMultiDeleteRequest postMultiDeleteRequest);

}
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
Expand All @@ -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;
Expand Down Expand Up @@ -43,7 +44,7 @@ public class BatchRedisServiceImpl implements BatchRedisService {
private RedisTemplate<String, String> strRedisTemplate;

/**
* 记录用户访问记录
* Redis批量Set
*/
@Override
public Object postMultiSet(PostMultiSetRequest postMultiSetRequest) {
Expand All @@ -58,7 +59,7 @@ public Object postMultiSet(PostMultiSetRequest postMultiSetRequest) {
}

/**
* 批量将缓存中的数据同步到MySQL
* Redis批量Get
*/
@Override
public Object listMultiGet(ListMultiGetRequest listMultiGetRequest) {
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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();
}

Expand Down
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]
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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() {
Expand All @@ -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) {
Expand All @@ -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() {
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit 4bb2aa4

Please sign in to comment.