![51df004ad6e7b99926f8fcb3889c25e4.png](https://i-blog.csdnimg.cn/blog_migrate/5432ecaa0c8cda1e6899b1d9f5b2d775.jpeg)
前言
最近在做业务的时候,需要批量操作Redis,虽然Redis的速度非常快,但是for循环操作Redis还是会有问题,在之前的基础上又对批量操作Redis进行了汇总;
批量操作Redis:
- 批量Set
- 批量Get
- 批量Set且设置过期时间
- 批量Delete
批量操作效果&技术实现方案
- 之前本地测试操作100个Key值,批量操作是单个操作的快122倍(《SpringBoot实战(六):Redis Pipeline 轻松实现百倍性能提升》)
- 技术实现方案
- SpringBoot
- Redis
代码实现
- 完整代码(GitHub,欢迎大家Star,Fork,Watch)https://github.com/dangnianchuntian/springboot
- 主要代码展示
- Controller
/*
* Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved.
* 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升
* 类名称:BatchRedisController.java
* 创建人:张晗
* 联系方式:zhanghan_java@163.com
* 开源地址: https://github.com/dangnianchuntian/springboot
* 博客地址: https://zhanghan.blog.csdn.net
*/
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;
@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);
}
/**
* 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);
}
}
- service
/*
* Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved.
* 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升
* 类名称:BatchRedisServiceImpl.java
* 创建人:张晗
* 联系方式:zhanghan_java@163.com
* 开源地址: https://github.com/dangnianchuntian/springboot
* 博客地址: https://zhanghan.blog.csdn.net
*/
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;
import com.zhanghan.zhredisbatch.service.BatchRedisService;
import com.zhanghan.zhredisbatch.util.wrapper.WrapMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Service
public class BatchRedisServiceImpl implements BatchRedisService {
private static Logger logger = LoggerFactory.getLogger(BatchRedisServiceImpl.class);
@Autowired
private RedisTemplate<String, String> strRedisTemplate;
/**
* Redis批量Set
*/
@Override
public Object postMultiSet(PostMultiSetRequest postMultiSetRequest) {
Map<String, String> batchSetMap = new HashMap<>();
List<BatchRedisDto> batchRedisDtoList = postMultiSetRequest.getBatchRedisDtoList();
for (BatchRedisDto batchRedisDto : batchRedisDtoList) {
batchSetMap.put(batchRedisDto.getRedisKey(), batchRedisDto.getRedisValue());
}
strRedisTemplate.opsForValue().multiSet(batchSetMap);
return WrapMapper.ok();
}
/**
* Redis批量Get
*/
@Override
public Object listMultiGet(ListMultiGetRequest listMultiGetRequest) {
List<String> keyList = listMultiGetRequest.getKeyList();
List<String> valueList = strRedisTemplate.opsForValue().multiGet(keyList);
List<BatchRedisDto> batchRedisDtoList = new ArrayList<>();
ListMultiGetResponse listMultiGetResponse = new ListMultiGetResponse();
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);
}
listMultiGetResponse.setBatchRedisDtoList(batchRedisDtoList);
return WrapMapper.ok(listMultiGetResponse);
}
/**
* Redis批量Set且设置失效时间
*/
@Override
public Object postMultiSetAndExpire(PostMultiSetRequest postMultiSetRequest) {
List<BatchRedisDto> batchRedisDtoList = postMultiSetRequest.getBatchRedisDtoList();
strRedisTemplate.executePipelined(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
for (BatchRedisDto batchRedisDto : batchRedisDtoList) {
String key = batchRedisDto.getRedisKey();
String value = batchRedisDto.getRedisValue();
connection.set(key.getBytes(), value.getBytes(), Expiration.from(1, TimeUnit.DAYS), RedisStringCommands.SetOption.UPSERT);
}
return null;
}
});
return WrapMapper.ok();
}
/**
* Redis批量Delete
*/
@Override
public Object postMultiDelete(PostMultiDeleteRequest postMultiDeleteRequest) {
strRedisTemplate.delete(postMultiDeleteRequest.getKeyList());
return WrapMapper.ok();
}
}
测试
- 批量Set
- 进行请求
![c50f8435ad6c942305b7eb12ee70a3ba.png](https://i-blog.csdnimg.cn/blog_migrate/05eaebfc6e331c3e7913945d3b8d9e95.jpeg)
- 查看Redis结果
![27d5270d09f2664184c9406389062161.png](https://i-blog.csdnimg.cn/blog_migrate/2f617b875fcb27d4492a6a22288f8a08.png)
- 批量Get
- 进行请求
![38e79e031b343eaa426cf2bfeb55ad90.png](https://i-blog.csdnimg.cn/blog_migrate/24e0d0b6e8ae5a4ab5c8300c5c125e58.jpeg)
- 批量Set且设置失效时间
- 进行请求
![2219c73ee8dc1aa91b1afaa0d90f1a6c.png](https://i-blog.csdnimg.cn/blog_migrate/7c8f385e6503b8675fb2565502d2e1c1.jpeg)
-
- 查看结果
![786d72779c25615e9f9e023acc1f726d.png](https://i-blog.csdnimg.cn/blog_migrate/0fca0ef7fcbadb8f166703371f7fdfd0.jpeg)
- 批量Delete
- 进行请求
![c56a07e4fc1b7de16198af4a67769550.png](https://i-blog.csdnimg.cn/blog_migrate/bd50cc21d9ddf6a0bb9d7eb4538027d6.jpeg)
-
- 查看结果
![49f96fa27b818ac0e515c327580ab1a2.png](https://i-blog.csdnimg.cn/blog_migrate/c88fef5c841fa959723e6baeace4ed10.jpeg)
总结
- 亮点:批量操作Redis,轻松实现百倍性能提升
- 注意点:批量Set ,redisTemplate的multiSet不支持设置超时时间,需要用executePipelined去实现