和get redis_Redis批量操作轻松实现百倍性能提升

前言

最近在做业务的时候,需要批量操作Redis,虽然Redis的速度非常快,但是for循环操作Redis还是会有问题,在之前的基础上又对批量操作Redis进行了汇总;

批量操作Redis:

批量Set 批量Get 批量Set且设置过期时间 批量Delete

批量操作效果&技术实现方案

  • 之前本地测试操作100个Key值,批量操作是单个操作的快122倍 (参考: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 strRedisTemplate;      /**       * Redis批量Set       */      @Override      public Object postMultiSet(PostMultiSetRequest postMultiSetRequest) {          Map batchSetMap = new HashMap<>();          List 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 keyList = listMultiGetRequest.getKeyList();          List valueList = strRedisTemplate.opsForValue().multiGet(keyList);          List batchRedisDtoList = new ArrayList<>();          ListMultiGetResponse listMultiGetResponse = new ListMultiGetResponse();          for (int i = 0; i  batchRedisDtoList = postMultiSetRequest.getBatchRedisDtoList();          strRedisTemplate.executePipelined(new RedisCallback() {              @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
46a23de5ef7c2b7ae930e1fc5080a024.png
  • 查看Redis结果
ab9f4dcbdcbac8daec6885c27f5bcc8d.png
  • 批量Get进行请求
a18de1c95b0e99b0e7a845d1c20bab71.png
  • 批量Set且设置失效时间
a2be5447982b40b0015048ae4ae6d26d.png
  • 查看批量Set设置失效时间
448930eaa1cf4383203c8b7ce7930061.png
  • 批量Delete
5adbf1686a9934018d2b781254be4d55.png
  • 查看批量Delete的结果
79a0538471aef68c7c9c23a2334ab201.png

总结

  • 亮点:批量操作Redis,轻松实现百倍性能提升
  • 注意点:批量Set ,redisTemplate的multiSet不支持设置超时时间,需要用executePipelined去实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值